Skip to content

@isaacs /http-on-incoming-fix-maybe.diff
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
diff --git a/lib/http.js b/lib/http.js
index ae6976a..8f2ac4f 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -1382,12 +1382,12 @@ function createHangUpError() {
function freeParser(parser, req) {
if (parser) {
parser._headers = [];
- parser.onIncoming = null;
if (parser.socket) {
parser.socket.onend = null;
parser.socket.ondata = null;
parser.socket.parser = null;
}
+ parser.onIncoming = null;
parser.socket = null;
parser.incoming = null;
parsers.free(parser);
@@ -1634,10 +1634,10 @@ ClientRequest.prototype.onSocket = function(socket) {
}
socket.on('error', socketErrorListener);
+ parser.onIncoming = parserOnIncomingClient;
socket.ondata = socketOnData;
socket.onend = socketOnEnd;
socket.on('close', socketCloseListener);
- parser.onIncoming = parserOnIncomingClient;
req.emit('socket', socket);
});
@@ -1820,58 +1820,6 @@ function connectionListener(socket) {
self.emit('clientError', e, this);
});
- socket.ondata = function(d, start, end) {
- var ret = parser.execute(d, start, end - start);
- if (ret instanceof Error) {
- debug('parse error');
- socket.destroy(ret);
- } else if (parser.incoming && parser.incoming.upgrade) {
- // Upgrade or CONNECT
- var bytesParsed = ret;
- var req = parser.incoming;
-
- socket.ondata = null;
- socket.onend = null;
- socket.removeListener('close', serverSocketCloseListener);
- parser.finish();
- freeParser(parser, req);
-
- // This is start + byteParsed
- var bodyHead = d.slice(start + bytesParsed, end);
-
- var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
- if (self.listeners(eventName).length) {
- self.emit(eventName, req, req.socket, bodyHead);
- } else {
- // Got upgrade header or CONNECT method, but have no handler.
- socket.destroy();
- }
- }
- };
-
- socket.onend = function() {
- var ret = parser.finish();
-
- if (ret instanceof Error) {
- debug('parse error');
- socket.destroy(ret);
- return;
- }
-
- if (!self.httpAllowHalfOpen) {
- abortIncoming();
- if (socket.writable) socket.end();
- } else if (outgoing.length) {
- outgoing[outgoing.length - 1]._last = true;
- } else if (socket._httpMessage) {
- socket._httpMessage._last = true;
- } else {
- if (socket.writable) socket.end();
- }
- };
-
- socket.addListener('close', serverSocketCloseListener);
-
// The following callback is issued after the headers have been read on a
// new message. In this callback we setup the response object and pass it
// to the user.
@@ -1935,6 +1883,58 @@ function connectionListener(socket) {
}
return false; // Not a HEAD response. (Not even a response!)
};
+
+ socket.ondata = function(d, start, end) {
+ var ret = parser.execute(d, start, end - start);
+ if (ret instanceof Error) {
+ debug('parse error');
+ socket.destroy(ret);
+ } else if (parser.incoming && parser.incoming.upgrade) {
+ // Upgrade or CONNECT
+ var bytesParsed = ret;
+ var req = parser.incoming;
+
+ socket.ondata = null;
+ socket.onend = null;
+ socket.removeListener('close', serverSocketCloseListener);
+ parser.finish();
+ freeParser(parser, req);
+
+ // This is start + byteParsed
+ var bodyHead = d.slice(start + bytesParsed, end);
+
+ var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
+ if (self.listeners(eventName).length) {
+ self.emit(eventName, req, req.socket, bodyHead);
+ } else {
+ // Got upgrade header or CONNECT method, but have no handler.
+ socket.destroy();
+ }
+ }
+ };
+
+ socket.onend = function() {
+ var ret = parser.finish();
+
+ if (ret instanceof Error) {
+ debug('parse error');
+ socket.destroy(ret);
+ return;
+ }
+
+ if (!self.httpAllowHalfOpen) {
+ abortIncoming();
+ if (socket.writable) socket.end();
+ } else if (outgoing.length) {
+ outgoing[outgoing.length - 1]._last = true;
+ } else if (socket._httpMessage) {
+ socket._httpMessage._last = true;
+ } else {
+ if (socket.writable) socket.end();
+ }
+ };
+
+ socket.addListener('close', serverSocketCloseListener);
}
exports._connectionListener = connectionListener;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.