http2 now passes all tests
authorRyan Dahl <ry@tinyclouds.org>
Thu, 18 Mar 2010 22:49:42 +0000 (15:49 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Thu, 18 Mar 2010 22:49:42 +0000 (15:49 -0700)
benchmark/http_simple.js
lib/http2.js
test/simple/test-http-proxy.js

index 2f80b43..6d21c38 100644 (file)
@@ -1,7 +1,7 @@
 path = require("path");
 
-var puts = require("../lib/sys").puts;
-http = require("../lib/http2");
+var puts = require("sys").puts;
+http = require("http2");
 
 fixed = ""
 for (var i = 0; i < 20*1024; i++) {
index 995a585..a8d09d3 100644 (file)
@@ -230,7 +230,7 @@ exports.OutgoingMessage = OutgoingMessage;
 OutgoingMessage.prototype._send = function (data, encoding) {
   var length = this.output.length;
 
-  if (length === 0) {
+  if (length === 0 || typeof data != 'string') {
     this.output.push(data);
     encoding = encoding || "ascii";
     this.outputEncodings.push(encoding);
@@ -242,11 +242,7 @@ OutgoingMessage.prototype._send = function (data, encoding) {
 
   if ((lastEncoding === encoding) ||
       (!encoding && data.constructor === lastData.constructor)) {
-    if (lastData.constructor === String) {
-      this.output[length-1] = lastData + data;
-    } else {
-      this.output[length-1] = lastData.concat(data);
-    }
+    this.output[length-1] = lastData + data;
     return;
   }
 
@@ -332,7 +328,11 @@ OutgoingMessage.prototype.write = function (chunk, encoding) {
 
   encoding = encoding || "ascii";
   if (this.chunked_encoding) {
-    this._send(process._byteLength(chunk, encoding).toString(16));
+    if (typeof chunk == 'string') {
+      this._send(process._byteLength(chunk, encoding).toString(16));
+    } else {
+      this._send(chunk.length.toString(16));
+    }
     this._send(CRLF);
     this._send(chunk, encoding);
     this._send(CRLF);
@@ -531,21 +531,20 @@ function Client ( ) {
 
   self._reconnect = function () {
     if (self.readyState != "opening") {
-      //sys.debug("HTTP CLIENT: reconnecting readyState = " + self.readyState);
+      sys.debug("HTTP CLIENT: reconnecting readyState = " + self.readyState);
       self.connect(self.port, self.host);
     }
   };
 
   self._pushRequest = function (req) {
     req.addListener("flush", function () {
-        /*
       if (self.readyState == "closed") {
-        //sys.debug("HTTP CLIENT request flush. reconnect.  readyState = " + self.readyState);
+        sys.debug("HTTP CLIENT request flush. reconnect.  readyState = " + self.readyState);
         self._reconnect();
         return;
       }
-        */
-      //sys.debug("self flush  readyState = " + self.readyState);
+
+      sys.debug("self flush  readyState = " + self.readyState);
       if (req == currentRequest) flushMessageQueue(self, [req]);
     });
     requests.push(req);
@@ -557,7 +556,8 @@ function Client ( ) {
 
   self.addListener("connect", function () {
     parser.reinitialize('response');
-    currentRequest = requests.shift();
+    sys.puts('requests: ' + sys.inspect(requests));
+    currentRequest = requests.shift()
     currentRequest.flush();
   });
 
@@ -575,7 +575,7 @@ function Client ( ) {
       return;
     }
 
-    //sys.debug("HTTP CLIENT onClose. readyState = " + self.readyState);
+    sys.debug("HTTP CLIENT onClose. readyState = " + self.readyState);
 
     // If there are more requests to handle, reconnect.
     if (requests.length > 0) {
@@ -602,7 +602,6 @@ exports.createClient = function (port, host) {
   var c = new Client;
   c.port = port;
   c.host = host;
-  c.connect(port, host);
   return c;
 }
 
index ebdf7af..5d57ba5 100644 (file)
@@ -6,12 +6,12 @@ var PROXY_PORT = PORT;
 var BACKEND_PORT = PORT+1;
 
 var backend = http.createServer(function (req, res) {
-  // debug("backend");
+  debug("backend request");
   res.writeHead(200, {"content-type": "text/plain"});
   res.write("hello world\n");
   res.close();
 });
-// debug("listen backend")
+debug("listen backend")
 backend.listen(BACKEND_PORT);
 
 var proxy_client = http.createClient(BACKEND_PORT);
@@ -25,31 +25,40 @@ var proxy = http.createServer(function (req, res) {
     });
     proxy_res.addListener("end", function() {
       res.close();
-      // debug("proxy res");
+      debug("proxy res");
     });
   });
   proxy_req.close();
 });
-// debug("listen proxy")
+debug("listen proxy")
 proxy.listen(PROXY_PORT);
 
 var body = "";
 
-var client = http.createClient(PROXY_PORT);
-var req = client.request("/test");
-// debug("client req")
-req.addListener('response', function (res) {
-  // debug("got res");
-  assert.equal(200, res.statusCode);
-  res.setBodyEncoding("utf8");
-  res.addListener('data', function (chunk) { body += chunk; });
-  res.addListener('end', function () {
-    proxy.close();
-    backend.close();
-    // debug("closed both");
+nlistening = 0;
+function startReq () {
+  nlistening++;
+  if (nlistening < 2) return;
+
+  var client = http.createClient(PROXY_PORT);
+  var req = client.request("/test");
+  debug("client req")
+  req.addListener('response', function (res) {
+    debug("got res");
+    assert.equal(200, res.statusCode);
+    res.setBodyEncoding("utf8");
+    res.addListener('data', function (chunk) { body += chunk; });
+    res.addListener('end', function () {
+      proxy.close();
+      backend.close();
+       debug("closed both");
+    });
   });
-});
-req.close();
+  req.close();
+}
+
+proxy.addListener('listening', startReq);
+backend.addListener('listening', startReq);
 
 process.addListener("exit", function () {
   assert.equal(body, "hello world\n");