From 15ec99ec5945c07616cd9299e126e9919bc9c8cc Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 25 May 2010 19:24:30 -0700 Subject: [PATCH] Fix issue 89, parsing responses to HEAD requests Test from Mark Hansen (mark at markhansen.co.nz) --- lib/http.js | 14 ++++++++++++-- test/simple/test-http-head-request.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 test/simple/test-http-head-request.js diff --git a/lib/http.js b/lib/http.js index f46ef18..85b055b 100644 --- a/lib/http.js +++ b/lib/http.js @@ -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 index 0000000..207a028 --- /dev/null +++ b/test/simple/test-http-head-request.js @@ -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); +}); -- 2.7.4