Skip to content

Commit 19114a9

Browse files
committed
Merge pull request #23 from hapijs/issue-payload-timeout
Fix timeouts for multipart payloads
2 parents 728d5ed + df8086d commit 19114a9

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lib/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ internals.Parser.prototype.multipart = function (source, contentType) {
288288
next = Hoek.once(next); // Modify next() for async events
289289
this.next = next;
290290

291+
// Set stream timeout
292+
293+
const clientTimeout = this.settings.timeout;
294+
let clientTimeoutId = null;
295+
291296
const dispenser = new Pez.Dispenser(contentType);
292297

293298
const onError = (err) => {
@@ -301,6 +306,7 @@ internals.Parser.prototype.multipart = function (source, contentType) {
301306
let data = {};
302307
const finalize = () => {
303308

309+
clearTimeout(clientTimeoutId);
304310
dispenser.removeListener('error', onError);
305311
dispenser.removeListener('part', onPart);
306312
dispenser.removeListener('field', onField);
@@ -314,6 +320,15 @@ internals.Parser.prototype.multipart = function (source, contentType) {
314320
return next();
315321
};
316322

323+
if (clientTimeout &&
324+
clientTimeout > 0) {
325+
326+
clientTimeoutId = setTimeout(() => {
327+
328+
return next(Boom.clientTimeout());
329+
}, clientTimeout);
330+
}
331+
317332
const set = (name, value) => {
318333

319334
arrayFields = arrayFields || (name.indexOf('[') !== -1);

test/index.js

+36
Original file line numberDiff line numberDiff line change
@@ -1467,4 +1467,40 @@ describe('parse()', () => {
14671467
});
14681468
});
14691469
});
1470+
1471+
it('will timeout correctly for a multipart payload with output as stream', (done) => {
1472+
1473+
const path = Path.join(__dirname, './file/image.jpg');
1474+
const fileStream = Fs.createReadStream(path);
1475+
1476+
const form = new FormData();
1477+
form.append('my_file', fileStream);
1478+
form.headers = form.getHeaders();
1479+
1480+
Subtext.parse(form, null, { parse: true, output: 'stream', timeout: 1 }, (err, parsed) => {
1481+
1482+
expect(err).to.exist();
1483+
expect(err.message).to.equal('Request Timeout');
1484+
expect(err.output.statusCode).to.equal(408);
1485+
done();
1486+
});
1487+
});
1488+
1489+
it('will timeout correctly for a multipart payload with output file', (done) => {
1490+
1491+
const path = Path.join(__dirname, './file/image.jpg');
1492+
const fileStream = Fs.createReadStream(path);
1493+
1494+
const form = new FormData();
1495+
form.append('my_file', fileStream);
1496+
form.headers = form.getHeaders();
1497+
1498+
Subtext.parse(form, null, { parse: true, output: 'file', timeout: 1 }, (err, parsed) => {
1499+
1500+
expect(err).to.exist();
1501+
expect(err.message).to.equal('Request Timeout');
1502+
expect(err.output.statusCode).to.equal(408);
1503+
done();
1504+
});
1505+
});
14701506
});

0 commit comments

Comments
 (0)