Move the http client's initParser() into prototype
This commit is contained in:
parent
f323f34353
commit
c3bc48d451
174
lib/http.js
174
lib/http.js
@ -842,13 +842,94 @@ function Client ( ) {
|
|||||||
|
|
||||||
httpSocketSetup(self);
|
httpSocketSetup(self);
|
||||||
|
|
||||||
var parser;
|
function onData(d, start, end) {
|
||||||
|
if (!self.parser) {
|
||||||
|
throw new Error("parser not initialized prior to Client.ondata call");
|
||||||
|
}
|
||||||
|
var ret = self.parser.execute(d, start, end - start);
|
||||||
|
if (ret instanceof Error) {
|
||||||
|
self.destroy(ret);
|
||||||
|
} else if (self.parser.incoming && self.parser.incoming.upgrade) {
|
||||||
|
var bytesParsed = ret;
|
||||||
|
self.ondata = null;
|
||||||
|
self.onend = null
|
||||||
|
|
||||||
function initParser () {
|
var req = self.parser.incoming;
|
||||||
if (!parser) parser = parsers.alloc();
|
|
||||||
parser.reinitialize('response');
|
var upgradeHead = d.slice(start + bytesParsed + 1, end);
|
||||||
parser.socket = self;
|
|
||||||
parser.onIncoming = function (res) {
|
if (self.listeners('upgrade').length) {
|
||||||
|
self.emit('upgrade', req, self, upgradeHead);
|
||||||
|
} else {
|
||||||
|
self.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.addListener("connect", function () {
|
||||||
|
debug('client connected');
|
||||||
|
|
||||||
|
self.ondata = onData;
|
||||||
|
self.onend = onEnd;
|
||||||
|
|
||||||
|
if (this.https) {
|
||||||
|
this.setSecure(this.credentials);
|
||||||
|
} else {
|
||||||
|
self._initParser();
|
||||||
|
debug('requests: ' + sys.inspect(self._outgoing));
|
||||||
|
outgoingFlush(self);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
self.addListener("secure", function () {
|
||||||
|
self._initParser();
|
||||||
|
debug('requests: ' + sys.inspect(self._outgoing));
|
||||||
|
outgoingFlush(self);
|
||||||
|
});
|
||||||
|
|
||||||
|
function onEnd() {
|
||||||
|
if (self.parser) self.parser.finish();
|
||||||
|
debug("self got end closing. readyState = " + self.readyState);
|
||||||
|
self.end();
|
||||||
|
};
|
||||||
|
|
||||||
|
self.addListener("close", function (e) {
|
||||||
|
if (e) return;
|
||||||
|
|
||||||
|
debug("HTTP CLIENT onClose. readyState = " + self.readyState);
|
||||||
|
|
||||||
|
// finally done with the request
|
||||||
|
self._outgoing.shift();
|
||||||
|
|
||||||
|
// If there are more requests to handle, reconnect.
|
||||||
|
if (self._outgoing.length) {
|
||||||
|
self._reconnect();
|
||||||
|
} else if (self.parser) {
|
||||||
|
parsers.free(self.parser);
|
||||||
|
self.parser = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
sys.inherits(Client, net.Stream);
|
||||||
|
|
||||||
|
exports.Client = Client;
|
||||||
|
|
||||||
|
exports.createClient = function (port, host, https, credentials) {
|
||||||
|
var c = new Client();
|
||||||
|
c.port = port;
|
||||||
|
c.host = host;
|
||||||
|
c.https = https;
|
||||||
|
c.credentials = credentials;
|
||||||
|
return c;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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!");
|
debug("incoming response!");
|
||||||
|
|
||||||
var req = self._outgoing[0];
|
var req = self._outgoing[0];
|
||||||
@ -882,87 +963,6 @@ function Client ( ) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
function onData(d, start, end) {
|
|
||||||
if (!parser) {
|
|
||||||
throw new Error("parser not initialized prior to Client.ondata call");
|
|
||||||
}
|
|
||||||
var ret = parser.execute(d, start, end - start);
|
|
||||||
if (ret instanceof Error) {
|
|
||||||
self.destroy(ret);
|
|
||||||
} else if (parser.incoming && parser.incoming.upgrade) {
|
|
||||||
var bytesParsed = ret;
|
|
||||||
self.ondata = null;
|
|
||||||
self.onend = null
|
|
||||||
|
|
||||||
var req = parser.incoming;
|
|
||||||
|
|
||||||
var upgradeHead = d.slice(start + bytesParsed + 1, end);
|
|
||||||
|
|
||||||
if (self.listeners('upgrade').length) {
|
|
||||||
self.emit('upgrade', req, self, upgradeHead);
|
|
||||||
} else {
|
|
||||||
self.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
self.addListener("connect", function () {
|
|
||||||
debug('client connected');
|
|
||||||
|
|
||||||
self.ondata = onData;
|
|
||||||
self.onend = onEnd;
|
|
||||||
|
|
||||||
if (this.https) {
|
|
||||||
this.setSecure(this.credentials);
|
|
||||||
} else {
|
|
||||||
initParser();
|
|
||||||
debug('requests: ' + sys.inspect(self._outgoing));
|
|
||||||
outgoingFlush(self);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
self.addListener("secure", function () {
|
|
||||||
initParser();
|
|
||||||
debug('requests: ' + sys.inspect(self._outgoing));
|
|
||||||
outgoingFlush(self);
|
|
||||||
});
|
|
||||||
|
|
||||||
function onEnd() {
|
|
||||||
if (parser) parser.finish();
|
|
||||||
debug("self got end closing. readyState = " + self.readyState);
|
|
||||||
self.end();
|
|
||||||
};
|
|
||||||
|
|
||||||
self.addListener("close", function (e) {
|
|
||||||
if (e) return;
|
|
||||||
|
|
||||||
debug("HTTP CLIENT onClose. readyState = " + self.readyState);
|
|
||||||
|
|
||||||
// finally done with the request
|
|
||||||
self._outgoing.shift();
|
|
||||||
|
|
||||||
// If there are more requests to handle, reconnect.
|
|
||||||
if (self._outgoing.length) {
|
|
||||||
self._reconnect();
|
|
||||||
} else if (parser) {
|
|
||||||
parsers.free(parser);
|
|
||||||
parser = null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
sys.inherits(Client, net.Stream);
|
|
||||||
|
|
||||||
exports.Client = Client;
|
|
||||||
|
|
||||||
exports.createClient = function (port, host, https, credentials) {
|
|
||||||
var c = new Client();
|
|
||||||
c.port = port;
|
|
||||||
c.host = host;
|
|
||||||
c.https = https;
|
|
||||||
c.credentials = credentials;
|
|
||||||
return c;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// 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]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user