http: use IncomingMessage._dump() instead of resume()
authorisaacs <i@izs.me>
Wed, 19 Dec 2012 02:49:09 +0000 (18:49 -0800)
committerisaacs <i@izs.me>
Fri, 21 Dec 2012 00:07:34 +0000 (00:07 +0000)
lib/http.js

index cdfc17b..2e1cc84 100644 (file)
@@ -119,6 +119,11 @@ function parserOnHeadersComplete(info) {
 function parserOnBody(b, start, len) {
   var parser = this;
   var stream = parser.incoming;
+
+  // if the stream has already been removed, then drop it.
+  if (!stream)
+    return;
+
   var rs = stream._readableState;
   var socket = stream.socket;
 
@@ -135,29 +140,29 @@ function parserOnBody(b, start, len) {
 function parserOnMessageComplete() {
   var parser = this;
   var stream = parser.incoming;
-  var socket = stream.socket;
 
-  stream.complete = true;
-
-  // Emit any trailing headers.
-  var headers = parser._headers;
-  if (headers) {
-    for (var i = 0, n = headers.length; i < n; i += 2) {
-      var k = headers[i];
-      var v = headers[i + 1];
-      parser.incoming._addHeaderLine(k, v);
+  if (stream) {
+    stream.complete = true;
+    // Emit any trailing headers.
+    var headers = parser._headers;
+    if (headers) {
+      for (var i = 0, n = headers.length; i < n; i += 2) {
+        var k = headers[i];
+        var v = headers[i + 1];
+        parser.incoming._addHeaderLine(k, v);
+      }
+      parser._headers = [];
+      parser._url = '';
     }
-    parser._headers = [];
-    parser._url = '';
-  }
 
-  if (!stream.upgrade)
-    // For upgraded connections, also emit this after parser.execute
-    stream._readableState.onread(null, null);
+    if (!stream.upgrade)
+      // For upgraded connections, also emit this after parser.execute
+      stream._readableState.onread(null, null);
+  }
 
   if (parser.socket.readable) {
     // force to read the next incoming message
-    socket.resume();
+    parser.socket.resume();
   }
 }
 
@@ -307,6 +312,7 @@ exports.IncomingMessage = IncomingMessage;
 
 IncomingMessage.prototype.read = function(n) {
   this._consuming = true;
+  this.read = Stream.Readable.prototype.read;
   return Stream.Readable.prototype.read.call(this, n);
 };
 
@@ -327,35 +333,6 @@ IncomingMessage.prototype.destroy = function(error) {
 };
 
 
-
-
-
-IncomingMessage.prototype._emitData = function(d) {
-  if (this._decoder) {
-    var string = this._decoder.write(d);
-    if (string.length) {
-      this.emit('data', string);
-    }
-  } else {
-    this.emit('data', d);
-  }
-};
-
-
-IncomingMessage.prototype._emitEnd = function() {
-  if (!this._endEmitted) {
-    if (this._decoder) {
-      var ret = this._decoder.end();
-      if (ret)
-        this.emit('data', ret);
-    }
-    this.emit('end');
-  }
-
-  this._endEmitted = true;
-};
-
-
 // Add the given (field, value) pair to the message
 //
 // Per RFC2616, section 4.2 it is acceptable to join multiple instances of the
@@ -415,6 +392,16 @@ IncomingMessage.prototype._addHeaderLine = function(field, value) {
 };
 
 
+// Call this instead of resume() if we want to just
+// dump all the data to /dev/null
+IncomingMessage.prototype._dump = function() {
+  this._dumped = true;
+  this.socket.parser.incoming = null;
+  this._readableState.onread(null, null);
+  this.socket.resume();
+};
+
+
 function OutgoingMessage() {
   Stream.call(this);
 
@@ -1534,7 +1521,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
   // can't possibly read the data, so we .resume() it into the void
   // so that the socket doesn't hang there in a paused state.
   if (!handled)
-    res.resume();
+    res._dump();
 
   return isHeadResponse;
 }
@@ -1861,7 +1848,7 @@ function connectionListener(socket) {
       // .resume() or .on('data'), then we call req.resume() so that the
       // bytes will be pulled off the wire.
       if (!req._consuming)
-        req.resume();
+        req._dump();
 
       res.detachSocket(socket);