Skip to content

Commit 8624adf

Browse files
committed
http: use IncomingMessage._dump() instead of resume()
1 parent 836593d commit 8624adf

1 file changed

Lines changed: 35 additions & 48 deletions

File tree

‎lib/http.js‎

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ function parserOnHeadersComplete(info) {
119119
function parserOnBody(b, start, len) {
120120
var parser = this;
121121
var stream = parser.incoming;
122+
123+
// if the stream has already been removed, then drop it.
124+
if (!stream)
125+
return;
126+
122127
var rs = stream._readableState;
123128
var socket = stream.socket;
124129

@@ -135,29 +140,29 @@ function parserOnBody(b, start, len) {
135140
function parserOnMessageComplete() {
136141
var parser = this;
137142
var stream = parser.incoming;
138-
var socket = stream.socket;
139143

140-
stream.complete = true;
141-
142-
// Emit any trailing headers.
143-
var headers = parser._headers;
144-
if (headers) {
145-
for (var i = 0, n = headers.length; i < n; i += 2) {
146-
var k = headers[i];
147-
var v = headers[i + 1];
148-
parser.incoming._addHeaderLine(k, v);
144+
if (stream) {
145+
stream.complete = true;
146+
// Emit any trailing headers.
147+
var headers = parser._headers;
148+
if (headers) {
149+
for (var i = 0, n = headers.length; i < n; i += 2) {
150+
var k = headers[i];
151+
var v = headers[i + 1];
152+
parser.incoming._addHeaderLine(k, v);
153+
}
154+
parser._headers = [];
155+
parser._url = '';
149156
}
150-
parser._headers = [];
151-
parser._url = '';
152-
}
153157

154-
if (!stream.upgrade)
155-
// For upgraded connections, also emit this after parser.execute
156-
stream._readableState.onread(null, null);
158+
if (!stream.upgrade)
159+
// For upgraded connections, also emit this after parser.execute
160+
stream._readableState.onread(null, null);
161+
}
157162

158163
if (parser.socket.readable) {
159164
// force to read the next incoming message
160-
socket.resume();
165+
parser.socket.resume();
161166
}
162167
}
163168

@@ -307,6 +312,7 @@ exports.IncomingMessage = IncomingMessage;
307312

308313
IncomingMessage.prototype.read = function(n) {
309314
this._consuming = true;
315+
this.read = Stream.Readable.prototype.read;
310316
return Stream.Readable.prototype.read.call(this, n);
311317
};
312318

@@ -327,35 +333,6 @@ IncomingMessage.prototype.destroy = function(error) {
327333
};
328334

329335

330-
331-
332-
333-
IncomingMessage.prototype._emitData = function(d) {
334-
if (this._decoder) {
335-
var string = this._decoder.write(d);
336-
if (string.length) {
337-
this.emit('data', string);
338-
}
339-
} else {
340-
this.emit('data', d);
341-
}
342-
};
343-
344-
345-
IncomingMessage.prototype._emitEnd = function() {
346-
if (!this._endEmitted) {
347-
if (this._decoder) {
348-
var ret = this._decoder.end();
349-
if (ret)
350-
this.emit('data', ret);
351-
}
352-
this.emit('end');
353-
}
354-
355-
this._endEmitted = true;
356-
};
357-
358-
359336
// Add the given (field, value) pair to the message
360337
//
361338
// Per RFC2616, section 4.2 it is acceptable to join multiple instances of the
@@ -415,6 +392,16 @@ IncomingMessage.prototype._addHeaderLine = function(field, value) {
415392
};
416393

417394

395+
// Call this instead of resume() if we want to just
396+
// dump all the data to /dev/null
397+
IncomingMessage.prototype._dump = function() {
398+
this._dumped = true;
399+
this.socket.parser.incoming = null;
400+
this._readableState.onread(null, null);
401+
this.socket.resume();
402+
};
403+
404+
418405
function OutgoingMessage() {
419406
Stream.call(this);
420407

@@ -1534,7 +1521,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
15341521
// can't possibly read the data, so we .resume() it into the void
15351522
// so that the socket doesn't hang there in a paused state.
15361523
if (!handled)
1537-
res.resume();
1524+
res._dump();
15381525

15391526
return isHeadResponse;
15401527
}
@@ -1861,7 +1848,7 @@ function connectionListener(socket) {
18611848
// .resume() or .on('data'), then we call req.resume() so that the
18621849
// bytes will be pulled off the wire.
18631850
if (!req._consuming)
1864-
req.resume();
1851+
req._dump();
18651852

18661853
res.detachSocket(socket);
18671854

0 commit comments

Comments
 (0)