Improve HTTP debug messages

This commit is contained in:
Ryan Dahl 2010-11-29 13:53:35 -08:00
parent 1db59afb75
commit 6057747e9e

View File

@ -8,7 +8,7 @@ var HTTPParser = process.binding('http_parser').HTTPParser;
var debug; var debug;
var debugLevel = parseInt(process.env.NODE_DEBUG, 16); var debugLevel = parseInt(process.env.NODE_DEBUG, 16);
if (debugLevel & 0x4) { if (debugLevel & 0x4) {
debug = function (x) { util.error('HTTP: ' + x); }; debug = function (x) { console.error('HTTP: %s', x); };
} else { } else {
debug = function () { }; debug = function () { };
} }
@ -770,7 +770,7 @@ exports.createServer = function (requestListener) {
function connectionListener (socket) { function connectionListener (socket) {
var self = this; var self = this;
debug("new http connection"); debug("SERVER new http connection");
httpSocketSetup(socket); httpSocketSetup(socket);
@ -790,6 +790,7 @@ function connectionListener (socket) {
socket.ondata = function (d, start, end) { socket.ondata = function (d, start, end) {
var ret = parser.execute(d, start, end - start); var ret = parser.execute(d, start, end - start);
if (ret instanceof Error) { if (ret instanceof Error) {
debug("parse error");
socket.destroy(ret); socket.destroy(ret);
} else if (parser.incoming && parser.incoming.upgrade) { } else if (parser.incoming && parser.incoming.upgrade) {
var bytesParsed = ret; var bytesParsed = ret;
@ -906,26 +907,26 @@ function Client ( ) {
}; };
self.addListener("connect", function () { self.addListener("connect", function () {
debug('client connected'); debug('CLIENT connected');
self.ondata = onData; self.ondata = onData;
self.onend = onEnd; self.onend = onEnd;
self._initParser(); self._initParser();
debug('requests: ' + util.inspect(self._outgoing)); debug('CLIENT requests: ' + util.inspect(self._outgoing.map(function (r) { return r.method; })));
outgoingFlush(self); outgoingFlush(self);
}); });
function onEnd() { function onEnd() {
if (self.parser) self.parser.finish(); if (self.parser) self.parser.finish();
debug("self got end closing. readyState = " + self.readyState); debug("CLIENT got end closing. readyState = " + self.readyState);
self.end(); self.end();
}; };
self.addListener("close", function (e) { self.addListener("close", function (e) {
if (e) return; if (e) return;
debug("HTTP CLIENT onClose. readyState = " + self.readyState); debug("CLIENT onClose. readyState = " + self.readyState);
// finally done with the request // finally done with the request
self._outgoing.shift(); self._outgoing.shift();
@ -961,14 +962,14 @@ Client.prototype._initParser = function () {
self.parser.reinitialize('response'); self.parser.reinitialize('response');
self.parser.socket = self; self.parser.socket = self;
self.parser.onIncoming = function (res) { self.parser.onIncoming = function (res) {
debug("incoming response!"); debug("CLIENT incoming response!");
var req = self._outgoing[0]; var req = self._outgoing[0];
// Responses to HEAD requests are AWFUL. Ask Ryan. // Responses to HEAD requests are AWFUL. Ask Ryan.
// A major oversight in HTTP. Hence this nastiness. // A major oversight in HTTP. Hence this nastiness.
var isHeadResponse = req.method == "HEAD"; var isHeadResponse = req.method == "HEAD";
debug('isHeadResponse ' + isHeadResponse); debug('CLIENT isHeadResponse ' + isHeadResponse);
if (res.statusCode == 100) { if (res.statusCode == 100) {
// restart the parser, as this is a continue message. // restart the parser, as this is a continue message.
@ -981,7 +982,7 @@ Client.prototype._initParser = function () {
} }
res.addListener('end', function ( ) { res.addListener('end', function ( ) {
debug("request complete disconnecting. readyState = " + self.readyState); debug("CLIENT request complete disconnecting. readyState = " + self.readyState);
// For the moment we reconnect for every request. FIXME! // For the moment we reconnect for every request. FIXME!
// All that should be required for keep-alive is to not reconnect, // All that should be required for keep-alive is to not reconnect,
// but outgoingFlush instead. // but outgoingFlush instead.
@ -1020,7 +1021,7 @@ Client.prototype._onOutgoingSent = function (message) {
// Instead, we just check if the connection is closed, and if so // Instead, we just check if the connection is closed, and if so
// reconnect if we have pending messages. // reconnect if we have pending messages.
if (this._outgoing.length && this.readyState == "closed") { if (this._outgoing.length && this.readyState == "closed") {
debug("HTTP client request flush. reconnect. readyState = " + this.readyState); debug("CLIENT request flush. reconnect. readyState = " + this.readyState);
this._reconnect(); this._reconnect();
} }
}; };
@ -1028,7 +1029,7 @@ Client.prototype._onOutgoingSent = function (message) {
Client.prototype._reconnect = function () { Client.prototype._reconnect = function () {
if (this.readyState === "closed") { if (this.readyState === "closed") {
debug("HTTP CLIENT: reconnecting readyState = " + this.readyState); debug("CLIENT reconnecting readyState = " + this.readyState);
this.connect(this.port, this.host); this.connect(this.port, this.host);
} }
}; };