@@ -79,6 +79,8 @@ const STATUS_CODES = exports.STATUS_CODES = { |
79 | 79 | 511 : 'Network Authentication Required' // RFC 6585 |
80 | 80 | }; |
81 | 81 |
|
| 82 | +const kOnExecute = HTTPParser.kOnExecute | 0; |
| 83 | + |
82 | 84 |
|
83 | 85 | function ServerResponse(req) { |
84 | 86 | OutgoingMessage.call(this); |
@@ -317,6 +319,21 @@ function connectionListener(socket) { |
317 | 319 | socket.on('end', socketOnEnd); |
318 | 320 | socket.on('data', socketOnData); |
319 | 321 |
|
| 322 | + // We are consuming socket, so it won't get any actual data |
| 323 | + socket.on('resume', onSocketResume); |
| 324 | + socket.on('pause', onSocketPause); |
| 325 | + |
| 326 | + socket.on('drain', socketOnDrain); |
| 327 | + |
| 328 | + // Override on to unconsume on `data`, `readable` listeners |
| 329 | + socket.on = socketOnWrap; |
| 330 | + |
| 331 | + var external = socket._handle._externalStream; |
| 332 | + if (external) |
| 333 | + parser.consume(external); |
| 334 | + external = null; |
| 335 | + parser[kOnExecute] = onParserExecute; |
| 336 | + |
320 | 337 | // TODO(isaacs): Move all these functions out of here |
321 | 338 | function socketOnError(e) { |
322 | 339 | self.emit('clientError', e, this); |
@@ -326,6 +343,16 @@ function connectionListener(socket) { |
326 | 343 | assert(!socket._paused); |
327 | 344 | debug('SERVER socketOnData %d', d.length); |
328 | 345 | var ret = parser.execute(d); |
| 346 | + |
| 347 | + onParserExecuteCommon(ret, d); |
| 348 | + } |
| 349 | + |
| 350 | + function onParserExecute(ret, d) { |
| 351 | + debug('SERVER socketOnParserExecute %d', ret); |
| 352 | + onParserExecuteCommon(ret, undefined); |
| 353 | + } |
| 354 | + |
| 355 | + function onParserExecuteCommon(ret, d) { |
329 | 356 | if (ret instanceof Error) { |
330 | 357 | debug('parse error'); |
331 | 358 | socket.destroy(ret); |
@@ -335,9 +362,13 @@ function connectionListener(socket) { |
335 | 362 | var req = parser.incoming; |
336 | 363 | debug('SERVER upgrade or connect', req.method); |
337 | 364 |
|
| 365 | + if (!d) |
| 366 | + d = parser.getCurrentBuffer(); |
| 367 | + |
338 | 368 | socket.removeListener('data', socketOnData); |
339 | 369 | socket.removeListener('end', socketOnEnd); |
340 | 370 | socket.removeListener('close', serverSocketCloseListener); |
| 371 | + parser.unconsume(socket._handle._externalStream); |
341 | 372 | parser.finish(); |
342 | 373 | freeParser(parser, req, null); |
343 | 374 | parser = null; |
@@ -400,7 +431,6 @@ function connectionListener(socket) { |
400 | 431 | socket.resume(); |
401 | 432 | } |
402 | 433 | } |
403 | | - socket.on('drain', socketOnDrain); |
404 | 434 |
|
405 | 435 | function parserOnIncoming(req, shouldKeepAlive) { |
406 | 436 | incoming.push(req); |
@@ -480,3 +510,24 @@ function connectionListener(socket) { |
480 | 510 | } |
481 | 511 | } |
482 | 512 | exports._connectionListener = connectionListener; |
| 513 | + |
| 514 | +function onSocketResume() { |
| 515 | + this._handle.readStart(); |
| 516 | +} |
| 517 | + |
| 518 | +function onSocketPause() { |
| 519 | + this._handle.readStop(); |
| 520 | +} |
| 521 | + |
| 522 | +function socketOnWrap(ev, fn) { |
| 523 | + var res = net.Socket.prototype.on.call(this, ev, fn); |
| 524 | + if (!this.parser) { |
| 525 | + this.on = net.Socket.prototype.on; |
| 526 | + return res; |
| 527 | + } |
| 528 | + |
| 529 | + if (ev === 'data' || ev === 'readable') |
| 530 | + this.parser.unconsume(this._handle._externalStream); |
| 531 | + |
| 532 | + return res; |
| 533 | +} |