Skip to content

Commit

Permalink
Send connection close when there is unconsumed payload
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Apr 11, 2017
1 parent a5e1543 commit 347b74a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ internals.Request = function (connection, req, res, options) {
this._entity = {}; // Entity information set via reply.entity()
this._logger = [];
this._allowInternals = !!options.allowInternals;
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
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,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
3 changes: 2 additions & 1 deletion lib/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,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: 1E20, 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 347b74a

Please sign in to comment.