diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2010-09-30 22:41:50 +0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-09-30 22:41:50 +0400 |
commit | c3bc48d451c77ff67a65338fefba4a6bc9478d7d (patch) | |
tree | fd322050cc07f8468e7cddc2eda39166273ed326 /lib/http.js | |
parent | f323f34353c054deed4bc0bc5c1b0c4d710186eb (diff) |
Move the http client's initParser() into prototype
Diffstat (limited to 'lib/http.js')
-rw-r--r-- | lib/http.js | 100 |
1 files changed, 50 insertions, 50 deletions
diff --git a/lib/http.js b/lib/http.js index 81e2126c9d3..15c1a1fbc68 100644 --- a/lib/http.js +++ b/lib/http.js @@ -842,59 +842,19 @@ function Client ( ) { 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) { - if (!parser) { + if (!self.parser) { 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) { self.destroy(ret); - } else if (parser.incoming && parser.incoming.upgrade) { + } else if (self.parser.incoming && self.parser.incoming.upgrade) { var bytesParsed = ret; self.ondata = null; self.onend = null - var req = parser.incoming; + var req = self.parser.incoming; var upgradeHead = d.slice(start + bytesParsed + 1, end); @@ -915,20 +875,20 @@ function Client ( ) { if (this.https) { this.setSecure(this.credentials); } else { - initParser(); + self._initParser(); debug('requests: ' + sys.inspect(self._outgoing)); outgoingFlush(self); } }); self.addListener("secure", function () { - initParser(); + self._initParser(); debug('requests: ' + sys.inspect(self._outgoing)); outgoingFlush(self); }); function onEnd() { - if (parser) parser.finish(); + if (self.parser) self.parser.finish(); debug("self got end closing. readyState = " + self.readyState); self.end(); }; @@ -944,9 +904,9 @@ function Client ( ) { // If there are more requests to handle, reconnect. if (self._outgoing.length) { self._reconnect(); - } else if (parser) { - parsers.free(parser); - parser = null; + } else if (self.parser) { + parsers.free(self.parser); + 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 // socket. The message that was sent is still sitting at client._outgoing[0] // it is our responsibility to shift it off. |