Skip to content

Commit 1762abc

Browse files
committed
http2 now passes all tests
1 parent 916e057 commit 1762abc

3 files changed

Lines changed: 43 additions & 35 deletions

File tree

benchmark/http_simple.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
path = require("path");
22

3-
var puts = require("../lib/sys").puts;
4-
http = require("../lib/http2");
3+
var puts = require("sys").puts;
4+
http = require("http2");
55

66
fixed = ""
77
for (var i = 0; i < 20*1024; i++) {

lib/http2.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ exports.OutgoingMessage = OutgoingMessage;
230230
OutgoingMessage.prototype._send = function (data, encoding) {
231231
var length = this.output.length;
232232

233-
if (length === 0) {
233+
if (length === 0 || typeof data != 'string') {
234234
this.output.push(data);
235235
encoding = encoding || "ascii";
236236
this.outputEncodings.push(encoding);
@@ -242,11 +242,7 @@ OutgoingMessage.prototype._send = function (data, encoding) {
242242

243243
if ((lastEncoding === encoding) ||
244244
(!encoding && data.constructor === lastData.constructor)) {
245-
if (lastData.constructor === String) {
246-
this.output[length-1] = lastData + data;
247-
} else {
248-
this.output[length-1] = lastData.concat(data);
249-
}
245+
this.output[length-1] = lastData + data;
250246
return;
251247
}
252248

@@ -332,7 +328,11 @@ OutgoingMessage.prototype.write = function (chunk, encoding) {
332328

333329
encoding = encoding || "ascii";
334330
if (this.chunked_encoding) {
335-
this._send(process._byteLength(chunk, encoding).toString(16));
331+
if (typeof chunk == 'string') {
332+
this._send(process._byteLength(chunk, encoding).toString(16));
333+
} else {
334+
this._send(chunk.length.toString(16));
335+
}
336336
this._send(CRLF);
337337
this._send(chunk, encoding);
338338
this._send(CRLF);
@@ -531,21 +531,20 @@ function Client ( ) {
531531

532532
self._reconnect = function () {
533533
if (self.readyState != "opening") {
534-
//sys.debug("HTTP CLIENT: reconnecting readyState = " + self.readyState);
534+
sys.debug("HTTP CLIENT: reconnecting readyState = " + self.readyState);
535535
self.connect(self.port, self.host);
536536
}
537537
};
538538

539539
self._pushRequest = function (req) {
540540
req.addListener("flush", function () {
541-
/*
542541
if (self.readyState == "closed") {
543-
//sys.debug("HTTP CLIENT request flush. reconnect. readyState = " + self.readyState);
542+
sys.debug("HTTP CLIENT request flush. reconnect. readyState = " + self.readyState);
544543
self._reconnect();
545544
return;
546545
}
547-
*/
548-
//sys.debug("self flush readyState = " + self.readyState);
546+
547+
sys.debug("self flush readyState = " + self.readyState);
549548
if (req == currentRequest) flushMessageQueue(self, [req]);
550549
});
551550
requests.push(req);
@@ -557,7 +556,8 @@ function Client ( ) {
557556

558557
self.addListener("connect", function () {
559558
parser.reinitialize('response');
560-
currentRequest = requests.shift();
559+
sys.puts('requests: ' + sys.inspect(requests));
560+
currentRequest = requests.shift()
561561
currentRequest.flush();
562562
});
563563

@@ -575,7 +575,7 @@ function Client ( ) {
575575
return;
576576
}
577577

578-
//sys.debug("HTTP CLIENT onClose. readyState = " + self.readyState);
578+
sys.debug("HTTP CLIENT onClose. readyState = " + self.readyState);
579579

580580
// If there are more requests to handle, reconnect.
581581
if (requests.length > 0) {
@@ -602,7 +602,6 @@ exports.createClient = function (port, host) {
602602
var c = new Client;
603603
c.port = port;
604604
c.host = host;
605-
c.connect(port, host);
606605
return c;
607606
}
608607

test/simple/test-http-proxy.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ var PROXY_PORT = PORT;
66
var BACKEND_PORT = PORT+1;
77

88
var backend = http.createServer(function (req, res) {
9-
// debug("backend");
9+
debug("backend request");
1010
res.writeHead(200, {"content-type": "text/plain"});
1111
res.write("hello world\n");
1212
res.close();
1313
});
14-
// debug("listen backend")
14+
debug("listen backend")
1515
backend.listen(BACKEND_PORT);
1616

1717
var proxy_client = http.createClient(BACKEND_PORT);
@@ -25,31 +25,40 @@ var proxy = http.createServer(function (req, res) {
2525
});
2626
proxy_res.addListener("end", function() {
2727
res.close();
28-
// debug("proxy res");
28+
debug("proxy res");
2929
});
3030
});
3131
proxy_req.close();
3232
});
33-
// debug("listen proxy")
33+
debug("listen proxy")
3434
proxy.listen(PROXY_PORT);
3535

3636
var body = "";
3737

38-
var client = http.createClient(PROXY_PORT);
39-
var req = client.request("/test");
40-
// debug("client req")
41-
req.addListener('response', function (res) {
42-
// debug("got res");
43-
assert.equal(200, res.statusCode);
44-
res.setBodyEncoding("utf8");
45-
res.addListener('data', function (chunk) { body += chunk; });
46-
res.addListener('end', function () {
47-
proxy.close();
48-
backend.close();
49-
// debug("closed both");
38+
nlistening = 0;
39+
function startReq () {
40+
nlistening++;
41+
if (nlistening < 2) return;
42+
43+
var client = http.createClient(PROXY_PORT);
44+
var req = client.request("/test");
45+
debug("client req")
46+
req.addListener('response', function (res) {
47+
debug("got res");
48+
assert.equal(200, res.statusCode);
49+
res.setBodyEncoding("utf8");
50+
res.addListener('data', function (chunk) { body += chunk; });
51+
res.addListener('end', function () {
52+
proxy.close();
53+
backend.close();
54+
debug("closed both");
55+
});
5056
});
51-
});
52-
req.close();
57+
req.close();
58+
}
59+
60+
proxy.addListener('listening', startReq);
61+
backend.addListener('listening', startReq);
5362

5463
process.addListener("exit", function () {
5564
assert.equal(body, "hello world\n");

0 commit comments

Comments
 (0)