public
Last active

  • Download Gist
http-on-incoming-fix-maybe.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
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;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.