Browse Source

Fix issue 89, parsing responses to HEAD requests

Test from Mark Hansen (mark at markhansen.co.nz)
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
15ec99ec59
  1. 14
      lib/http.js
  2. 35
      test/simple/test-http-head-request.js

14
lib/http.js

@ -80,13 +80,15 @@ var parsers = new FreeList('parsers', 1000, function () {
parser.incoming.upgrade = info.upgrade; parser.incoming.upgrade = info.upgrade;
var isHeadResponse = false;
if (!info.upgrade) { if (!info.upgrade) {
// For upgraded connections, we'll emit this after parser.execute // For upgraded connections, we'll emit this after parser.execute
// so that we can capture the first part of the new protocol // 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) { parser.onBody = function (b, start, len) {
@ -502,6 +504,7 @@ ServerResponse.prototype.writeHeader = function () {
function ClientRequest (socket, method, url, headers) { function ClientRequest (socket, method, url, headers) {
OutgoingMessage.call(this, socket); OutgoingMessage.call(this, socket);
this.method = method;
this.shouldKeepAlive = false; this.shouldKeepAlive = false;
if (method === "GET" || method === "HEAD") { if (method === "GET" || method === "HEAD") {
this.useChunkedEncodingByDefault = false; this.useChunkedEncodingByDefault = false;
@ -670,6 +673,7 @@ function connectionListener (socket) {
responses.push(res); responses.push(res);
self.emit('request', req, 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(); if (!parser) parser = parsers.alloc();
parser.reinitialize('response'); parser.reinitialize('response');
parser.socket = self; parser.socket = self;
parser.reqs = []; // list of request methods
parser.onIncoming = function (res) { parser.onIncoming = function (res) {
debug("incoming response!"); debug("incoming response!");
var isHeadResponse = currentRequest.method == "HEAD";
debug('isHeadResponse ' + isHeadResponse);
res.addListener('end', function ( ) { res.addListener('end', function ( ) {
debug("request complete disconnecting. readyState = " + self.readyState); debug("request complete disconnecting. readyState = " + self.readyState);
self.end(); self.end();
}); });
currentRequest.emit("response", res); currentRequest.emit("response", res);
return isHeadResponse;
}; };
}; };

35
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);
});
Loading…
Cancel
Save