Skip to content

Commit

Permalink
Merge pull request #3472 from kanongil/close-on-payload
Browse files Browse the repository at this point in the history
Send connection close when there is unconsumed payload
  • Loading branch information
hueniverse committed May 29, 2017
2 parents 50aaf12 + 4fdec0d commit c212bc8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/request.js
Expand Up @@ -133,7 +133,7 @@ internals.Request = function (connection, req, res, options) {
this._logger = [];
this._allowInternals = !!options.allowInternals;
this._expectContinue = !!options.expectContinue;
this._isPayloadPending = true; // false when incoming payload fully processed
this._isPayloadPending = !!(req.headers['content-length'] || req.headers['transfer-encoding']); // false when incoming payload fully processed
this._isBailed = false; // true when lifecycle should end
this._isReplied = false; // true when response processing started
this._isFinalized = false; // true when request completed (may be waiting on tails to complete)
Expand Down
2 changes: 1 addition & 1 deletion lib/route.js
Expand Up @@ -428,7 +428,7 @@ internals.payload = function (request, next) {
if (!err ||
!request._isPayloadPending) {

request._isPayloadPending = false;
request._isPayloadPending = !!(err || (parsed.payload && parsed.payload._readableState));
return onParsed(err, parsed);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/transmit.js
Expand Up @@ -223,7 +223,9 @@ internals.transmit = function (response, callback) {
// Connection: close

const isInjection = Shot.isInjection(request.raw.req);
if (!isInjection && !request.connection._started) {
if (!(isInjection || request.connection._started) ||
(request._isPayloadPending && !request.raw.req._readableState.ended)) {

response._header('connection', 'close');
}

Expand Down
22 changes: 22 additions & 0 deletions test/payload.js
Expand Up @@ -677,6 +677,28 @@ describe('payload', () => {
});
});

it('signals connection close when payload is unconsumed', (done) => {

const payload = new Buffer(10 * 1024 * 1024).toString();

const handler = function (request, reply) {

return reply('ok');
};

const server = new Hapi.Server();
server.connection();
server.route({ method: 'POST', path: '/', config: { handler, payload: { maxBytes: 11 * 1024 * 1024, output: 'stream', parse: false } } });

server.inject({ method: 'POST', url: '/', payload, headers: { 'content-type': 'application/octet-stream' } }, (res) => {

expect(res.statusCode).to.equal(200);
expect(res.headers).to.include({ connection: 'close' });
expect(res.result).to.equal('ok');
done();
});
});

it('times out when client request taking too long', (done) => {

const handler = function (request, reply) {
Expand Down

0 comments on commit c212bc8

Please sign in to comment.