Browse Source

Move the http client's initParser() into prototype

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
c3bc48d451
  1. 100
      lib/http.js

100
lib/http.js

@ -842,59 +842,19 @@ function Client ( ) {
httpSocketSetup(self); httpSocketSetup(self);
var parser;
function initParser () {
if (!parser) parser = parsers.alloc();
parser.reinitialize('response');
parser.socket = self;
parser.onIncoming = function (res) {
debug("incoming response!");
var req = self._outgoing[0];
// Responses to HEAD requests are AWFUL. Ask Ryan.
// A major oversight in HTTP. Hence this nastiness.
var isHeadResponse = req.method == "HEAD";
debug('isHeadResponse ' + isHeadResponse);
if (req.shouldKeepAlive && res.headers.connection === 'close') {
req.shouldKeepAlive = false;
}
res.addListener('end', function ( ) {
debug("request complete disconnecting. readyState = " + self.readyState);
// For the moment we reconnect for every request. FIXME!
// All that should be required for keep-alive is to not reconnect,
// but outgoingFlush instead.
if (req.shouldKeepAlive) {
outgoingFlush(self)
self._outgoing.shift()
outgoingFlush(self)
} else {
self.end();
}
});
req.emit("response", res);
return isHeadResponse;
};
};
function onData(d, start, end) { function onData(d, start, end) {
if (!parser) { if (!self.parser) {
throw new Error("parser not initialized prior to Client.ondata call"); throw new Error("parser not initialized prior to Client.ondata call");
} }
var ret = parser.execute(d, start, end - start); var ret = self.parser.execute(d, start, end - start);
if (ret instanceof Error) { if (ret instanceof Error) {
self.destroy(ret); self.destroy(ret);
} else if (parser.incoming && parser.incoming.upgrade) { } else if (self.parser.incoming && self.parser.incoming.upgrade) {
var bytesParsed = ret; var bytesParsed = ret;
self.ondata = null; self.ondata = null;
self.onend = null self.onend = null
var req = parser.incoming; var req = self.parser.incoming;
var upgradeHead = d.slice(start + bytesParsed + 1, end); var upgradeHead = d.slice(start + bytesParsed + 1, end);
@ -915,20 +875,20 @@ function Client ( ) {
if (this.https) { if (this.https) {
this.setSecure(this.credentials); this.setSecure(this.credentials);
} else { } else {
initParser(); self._initParser();
debug('requests: ' + sys.inspect(self._outgoing)); debug('requests: ' + sys.inspect(self._outgoing));
outgoingFlush(self); outgoingFlush(self);
} }
}); });
self.addListener("secure", function () { self.addListener("secure", function () {
initParser(); self._initParser();
debug('requests: ' + sys.inspect(self._outgoing)); debug('requests: ' + sys.inspect(self._outgoing));
outgoingFlush(self); outgoingFlush(self);
}); });
function onEnd() { function onEnd() {
if (parser) parser.finish(); if (self.parser) self.parser.finish();
debug("self got end closing. readyState = " + self.readyState); debug("self got end closing. readyState = " + self.readyState);
self.end(); self.end();
}; };
@ -944,9 +904,9 @@ function Client ( ) {
// If there are more requests to handle, reconnect. // If there are more requests to handle, reconnect.
if (self._outgoing.length) { if (self._outgoing.length) {
self._reconnect(); self._reconnect();
} else if (parser) { } else if (self.parser) {
parsers.free(parser); parsers.free(self.parser);
parser = null; self.parser = null;
} }
}); });
}; };
@ -964,6 +924,46 @@ exports.createClient = function (port, host, https, credentials) {
}; };
Client.prototype._initParser = function () {
var self = this;
if (!self.parser) self.parser = parsers.alloc();
self.parser.reinitialize('response');
self.parser.socket = self;
self.parser.onIncoming = function (res) {
debug("incoming response!");
var req = self._outgoing[0];
// Responses to HEAD requests are AWFUL. Ask Ryan.
// A major oversight in HTTP. Hence this nastiness.
var isHeadResponse = req.method == "HEAD";
debug('isHeadResponse ' + isHeadResponse);
if (req.shouldKeepAlive && res.headers.connection === 'close') {
req.shouldKeepAlive = false;
}
res.addListener('end', function ( ) {
debug("request complete disconnecting. readyState = " + self.readyState);
// For the moment we reconnect for every request. FIXME!
// All that should be required for keep-alive is to not reconnect,
// but outgoingFlush instead.
if (req.shouldKeepAlive) {
outgoingFlush(self)
self._outgoing.shift()
outgoingFlush(self)
} else {
self.end();
}
});
req.emit("response", res);
return isHeadResponse;
};
};
// This is called each time a request has been pushed completely to the // This is called each time a request has been pushed completely to the
// socket. The message that was sent is still sitting at client._outgoing[0] // socket. The message that was sent is still sitting at client._outgoing[0]
// it is our responsibility to shift it off. // it is our responsibility to shift it off.

Loading…
Cancel
Save