Skip to content
This repository

Showing 2 changed files with 121 additions and 7 deletions. Show diff stats Hide diff stats

  1. +46 7 lib/http.js
  2. +75 0 test/simple/test-http-pause.js
53 lib/http.js
@@ -26,6 +26,7 @@ var EventEmitter = require('events').EventEmitter;
26 26 var FreeList = require('freelist').FreeList;
27 27 var HTTPParser = process.binding('http_parser').HTTPParser;
28 28 var assert = require('assert').ok;
  29 +var END_OF_FILE = {};
29 30
30 31
31 32 var debug;
@@ -108,11 +109,10 @@ var parsers = new FreeList('parsers', 1000, function() {
108 109 parser.onBody = function(b, start, len) {
109 110 // TODO body encoding?
110 111 var slice = b.slice(start, start + len);
111   - if (parser.incoming._decoder) {
112   - var string = parser.incoming._decoder.write(slice);
113   - if (string.length) parser.incoming.emit('data', string);
  112 + if (parser.incoming._paused || parser.incoming._pendings.length) {
  113 + parser.incoming._pendings.push(slice);
114 114 } else {
115   - parser.incoming.emit('data', slice);
  115 + parser.incoming._emitData(slice);
116 116 }
117 117 };
118 118
@@ -133,8 +133,12 @@ var parsers = new FreeList('parsers', 1000, function() {
133 133
134 134 if (!parser.incoming.upgrade) {
135 135 // For upgraded connections, also emit this after parser.execute
136   - parser.incoming.readable = false;
137   - parser.incoming.emit('end');
  136 + if (parser.incoming._paused || parser.incoming._pendings.length) {
  137 + parser.incoming._pendings.push(END_OF_FILE);
  138 + } else {
  139 + parser.incoming.readable = false;
  140 + parser.incoming.emit('end');
  141 + }
138 142 }
139 143 };
140 144
@@ -224,6 +228,9 @@ function IncomingMessage(socket) {
224 228
225 229 this.readable = true;
226 230
  231 + this._paused = false;
  232 + this._pendings = [];
  233 +
227 234 // request (server) only
228 235 this.url = '';
229 236
@@ -251,12 +258,44 @@ IncomingMessage.prototype.setEncoding = function(encoding) {
251 258
252 259
253 260 IncomingMessage.prototype.pause = function() {
  261 + this._paused = true;
254 262 this.socket.pause();
255 263 };
256 264
257 265
258 266 IncomingMessage.prototype.resume = function() {
259   - this.socket.resume();
  267 + this._paused = false;
  268 + if (this.socket) {
  269 + this.socket.resume();
  270 + }
  271 + if (this._pendings.length) {
  272 + var self = this;
  273 + process.nextTick(function() {
  274 + while (!self._paused && self._pendings.length) {
  275 + var chunk = self._pendings.shift();
  276 + if (chunk !== END_OF_FILE) {
  277 + assert(Buffer.isBuffer(chunk));
  278 + self._emitData(chunk);
  279 + } else {
  280 + assert(self._pendings.length === 0);
  281 + self.readable = false;
  282 + self.emit('end');
  283 + }
  284 + }
  285 + });
  286 + }
  287 +};
  288 +
  289 +
  290 +IncomingMessage.prototype._emitData = function(d) {
  291 + if (this._decoder) {
  292 + var string = this._decoder.write(d);
  293 + if (string.length) {
  294 + this.emit('data', string);
  295 + }
  296 + } else {
  297 + this.emit('data', d);
  298 + }
260 299 };
261 300
262 301
75 test/simple/test-http-pause.js
... ... @@ -0,0 +1,75 @@
  1 +// Copyright Joyent, Inc. and other Node contributors.
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining a
  4 +// copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to permit
  8 +// persons to whom the Software is furnished to do so, subject to the
  9 +// following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be included
  12 +// in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20 +// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +
  22 +var common = require('../common');
  23 +var assert = require('assert');
  24 +var http = require('http');
  25 +
  26 +var expectedServer = 'Request Body from Client';
  27 +var resultServer = '';
  28 +var expectedClient = 'Response Body from Server';
  29 +var resultClient = '';
  30 +
  31 +var server = http.createServer(function(req, res) {
  32 + common.debug('pause server request');
  33 + req.pause();
  34 + setTimeout(function() {
  35 + common.debug('resume server request');
  36 + req.resume();
  37 + req.setEncoding('utf8');
  38 + req.on('data', function(chunk) {
  39 + resultServer += chunk;
  40 + });
  41 + req.on('end', function() {
  42 + common.debug(resultServer);
  43 + res.writeHead(200);
  44 + res.end(expectedClient);
  45 + });
  46 + }, 100);
  47 +});
  48 +
  49 +server.listen(common.PORT, function() {
  50 + var req = http.request({
  51 + port: common.PORT,
  52 + path: '/',
  53 + method: 'POST'
  54 + }, function(res) {
  55 + common.debug('pause client response');
  56 + res.pause();
  57 + setTimeout(function() {
  58 + common.debug('resume client response');
  59 + res.resume();
  60 + res.on('data', function(chunk) {
  61 + resultClient += chunk;
  62 + });
  63 + res.on('end', function() {
  64 + common.debug(resultClient);
  65 + server.close();
  66 + });
  67 + }, 100);
  68 + });
  69 + req.end(expectedServer);
  70 +});
  71 +
  72 +process.on('exit', function() {
  73 + assert.equal(expectedServer, resultServer);
  74 + assert.equal(expectedClient, resultClient);
  75 +});

3 comments on commit e6b6075

Mikeal Rogers

Love that this is in, and I'm not complaining, but....

This really needs to be fixed in Stream.pipe() as well.

The fact that we insure this for http.ServerRequest and nowhere else is not good.

We also need to get pause() to take a size argument for how large it should allow the buffer to get before it causes the remote end to stop sending data. This has all been discussed and more or less approved in pull requests that weren't finished.

It would be great if, for 0.8, we could get Stream.pause(size) implemented/fixed and make it consistent across all of node.

Marco Rogers

I'd like to be in on discussions about the Stream api. I've talked to @mikeal about a few ideas I have, but I'm not up to speed on a lot of internal discussion you guys have had. I agree that this shouldn't be specific to fd backed streams if possible.

Ben Noordhuis

@polotek: See #1681. Might be worth raising on nodejs-dev as well.

Please sign in to comment.
Something went wrong with that request. Please try again.