Skip to content

Commit 3aca284

Browse files
author
Eran Hammer
committed
Cleanup for hapijs#2429
1 parent d1ec2d5 commit 3aca284

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

lib/request.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ exports = module.exports = internals.Request = function (connection, req, res, o
113113

114114
this._states = {};
115115
this._logger = [];
116+
this._isPayloadPending = true; // false when incoming payload fully processed
116117
this._isBailed = false; // true when lifecycle should end
117118
this._isReplied = false; // true when response processing started
118119
this._isFinalized = false; // true when request completed (may be waiting on tails to complete)
@@ -121,11 +122,19 @@ exports = module.exports = internals.Request = function (connection, req, res, o
121122
this._protect = new Protect(this);
122123
this.domain = this._protect.domain;
123124

124-
// Listen to request errors
125+
// Listen to request state
126+
127+
this._onEnd = function () {
128+
129+
self._isPayloadPending = false;
130+
};
131+
132+
this.raw.req.once('end', this._onEnd);
125133

126134
this._onClose = function () {
127135

128136
self._log(['request', 'closed', 'error']);
137+
self._isPayloadPending = false;
129138
self._isBailed = true;
130139
};
131140

@@ -134,6 +143,7 @@ exports = module.exports = internals.Request = function (connection, req, res, o
134143
this._onError = function (err) {
135144

136145
self._log(['request', 'error'], err);
146+
self._isPayloadPending = false;
137147
};
138148

139149
this.raw.req.once('error', this._onError);
@@ -387,6 +397,7 @@ internals.Request.prototype._finalize = function () {
387397

388398
// Cleanup
389399

400+
this.raw.req.removeListener('end', this._onEnd);
390401
this.raw.req.removeListener('close', this._onClose);
391402
this.raw.req.removeListener('error', this._onError);
392403

lib/route.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ internals.payload = function (request, next) {
363363
return next();
364364
}
365365

366-
Subtext.parse(request.raw.req, request._tap(), request.route.settings.payload, function (err, parsed) {
366+
var onParsed = function (err, parsed) {
367367

368368
request.mime = parsed.mime;
369369
request.payload = parsed.payload || null;
@@ -382,6 +382,23 @@ internals.payload = function (request, next) {
382382
}
383383

384384
return next();
385+
};
386+
387+
Subtext.parse(request.raw.req, request._tap(), request.route.settings.payload, function (err, parsed) {
388+
389+
if (!request._isPayloadPending) {
390+
return onParsed(err, parsed);
391+
}
392+
393+
// Flush out any pending request payload not consumed due to errors or other restrictions
394+
395+
request.raw.req.once('end', function () {
396+
397+
request._isPayloadPending = false;
398+
return onParsed(err, parsed);
399+
});
400+
401+
request.raw.req.resume();
385402
});
386403
};
387404

lib/transmit.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ internals.transmit = function (response, callback) {
312312
var preview = (tap ? source.pipe(tap) : source);
313313
var compressed = (compressor ? preview.pipe(compressor) : preview);
314314
var ranged = (ranger ? compressed.pipe(ranger) : compressed);
315-
ranged.pipe(request.raw.res, { end: false });
316-
315+
ranged.pipe(request.raw.res);//, { end: false });
316+
/*
317317
ranged.once('end', function () {
318318
319319
if (request.raw.req.complete === false) {
@@ -322,7 +322,7 @@ internals.transmit = function (response, callback) {
322322
323323
end();
324324
});
325-
325+
*/
326326
// Injection
327327

328328
if (Shot.isInjection(request.raw.req)) {

test/payload.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,36 +154,25 @@ describe('payload', function () {
154154
});
155155
});
156156

157-
it('returns 400 when payload is not consumed', function (done) {
157+
it('returns 400 with response when payload is not consumed', function (done) {
158158

159159
var payload = new Buffer(10 * 1024 * 1024).toString();
160160

161-
var handler = function (request, reply) {
162-
163-
expect(request.payload.toString()).to.equal(payload);
164-
return reply(request.payload);
165-
};
166-
167161
var server = new Hapi.Server();
168162
server.connection();
169-
server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { maxBytes: 10 } } });
163+
server.route({ method: 'POST', path: '/', config: { handler: function (request, reply) { return reply(); }, payload: { maxBytes: 1024 * 1024 } } });
170164

171165
server.start(function () {
172166

173167
var uri = 'http://localhost:' + server.info.port;
174168

175-
Wreck.post(uri, { payload: payload, agent: false }, function (err, res, body) {
169+
Wreck.post(uri, { payload: payload }, function (err, res, body) {
176170

177171
expect(err).to.not.exist();
178172
expect(res.statusCode).to.equal(400);
179-
expect(body).to.exist();
180-
Wreck.post(uri, { payload: payload, agent: false }, function (err, res, body) {
181-
182-
expect(err).to.not.exist();
183-
expect(res.statusCode).to.equal(400);
184-
expect(body).to.exist();
185-
done();
186-
});
173+
expect(body).to.equal('{"statusCode":400,"error":"Bad Request","message":"Payload content length greater than maximum allowed: 1048576"}');
174+
175+
done();
187176
});
188177
});
189178
});

0 commit comments

Comments
 (0)