From 1762abcecefb41fd68692b12ceba441aaa33c7c3 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 18 Mar 2010 15:49:42 -0700 Subject: [PATCH] http2 now passes all tests --- benchmark/http_simple.js | 4 +-- lib/http2.js | 29 +++++++++++----------- test/simple/test-http-proxy.js | 45 ++++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/benchmark/http_simple.js b/benchmark/http_simple.js index 2f80b43ff6..6d21c38eb5 100644 --- a/benchmark/http_simple.js +++ b/benchmark/http_simple.js @@ -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++) { diff --git a/lib/http2.js b/lib/http2.js index 995a585e00..a8d09d3ea8 100644 --- a/lib/http2.js +++ b/lib/http2.js @@ -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; } diff --git a/test/simple/test-http-proxy.js b/test/simple/test-http-proxy.js index ebdf7af10f..5d57ba5310 100644 --- a/test/simple/test-http-proxy.js +++ b/test/simple/test-http-proxy.js @@ -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");