Fix issue 89, parsing responses to HEAD requests
authorRyan Dahl <ry@tinyclouds.org>
Wed, 26 May 2010 02:24:30 +0000 (19:24 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 26 May 2010 02:25:40 +0000 (19:25 -0700)
Test from Mark Hansen (mark at markhansen.co.nz)

lib/http.js
test/simple/test-http-head-request.js [new file with mode: 0644]

index f46ef18..85b055b 100644 (file)
@@ -80,13 +80,15 @@ var parsers = new FreeList('parsers', 1000, function () {
 
     parser.incoming.upgrade = info.upgrade;
 
+    var isHeadResponse = false;
+
     if (!info.upgrade) {
       // For upgraded connections, we'll emit this after parser.execute
       // so that we can capture the first part of the new protocol
-      parser.onIncoming(parser.incoming, info.shouldKeepAlive);
+      isHeadResponse = parser.onIncoming(parser.incoming, info.shouldKeepAlive);
     }
 
-    return false; // Is response to HEAD request?
+    return isHeadResponse;
   };
 
   parser.onBody = function (b, start, len) {
@@ -502,6 +504,7 @@ ServerResponse.prototype.writeHeader = function () {
 function ClientRequest (socket, method, url, headers) {
   OutgoingMessage.call(this, socket);
 
+  this.method = method;
   this.shouldKeepAlive = false;
   if (method === "GET" || method === "HEAD") {
     this.useChunkedEncodingByDefault = false;
@@ -670,6 +673,7 @@ function connectionListener (socket) {
     responses.push(res);
 
     self.emit('request', req, res);
+    return false; // Not a HEAD response. (Not even a response!)
   };
 }
 
@@ -687,15 +691,21 @@ function Client ( ) {
     if (!parser) parser = parsers.alloc();
     parser.reinitialize('response');
     parser.socket = self;
+    parser.reqs = []; // list of request methods
     parser.onIncoming = function (res) {
       debug("incoming response!");
 
+      var isHeadResponse = currentRequest.method == "HEAD";
+      debug('isHeadResponse ' + isHeadResponse);
+
       res.addListener('end', function ( ) {
         debug("request complete disconnecting. readyState = " + self.readyState);
         self.end();
       });
 
       currentRequest.emit("response", res);
+
+      return isHeadResponse;
     };
   };
 
diff --git a/test/simple/test-http-head-request.js b/test/simple/test-http-head-request.js
new file mode 100644 (file)
index 0000000..207a028
--- /dev/null
@@ -0,0 +1,35 @@
+require('../common');
+
+assert = require("assert");
+http = require("http");
+sys = require("sys");
+
+
+body = "hello world\n";
+
+server = http.createServer(function (req, res) {
+  error('req: ' + req.method);
+  res.writeHead(200, {"Content-Length": body.length});
+  res.end();
+  server.close();
+});
+server.listen(PORT);
+
+var gotEnd = false;
+
+server.addListener('listening', function () {
+  var client = http.createClient(PORT);
+  var request = client.request("HEAD", "/");
+  request.addListener('response', function (response) {
+    error('response start');
+    response.addListener("end", function () {
+      error('response end');
+      gotEnd = true;
+    });
+  });
+  request.end();
+});
+
+process.addListener('exit', function () {
+  assert.ok(gotEnd);
+});