mirror of https://github.com/nodejs/node.git
Move the http client's initParser() into prototype
parent
f323f34353
commit
c3bc48d451
100
lib/http.js
100
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.
|
||||
|
|
Loading…
Reference in New Issue