Skip to content

Commit

Permalink
Merge pull request #362 from RomanBurunkov/master
Browse files Browse the repository at this point in the history
Do not run next after abortion on limit
  • Loading branch information
RomanBurunkov authored Nov 1, 2023
2 parents fcb6952 + 718a574 commit 24749bd
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/processMultipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,23 @@ module.exports = (options, req, res, next) => {
const busboyOptions = buildOptions(options, { headers: req.headers });
const busboy = Busboy(busboyOptions);

// Close connection with specified reason and http code, default: 400 Bad Request.
/**
* Closes connection with specified reason and http code.
* @param {number} code HTTP response code, default: 400.
* @param {*} reason Reason to close connection, default: 'Bad Request'.
*/
const closeConnection = (code, reason) => {
req.unpipe(busboy);
res.writeHead(code || 400, { Connection: 'close' });
res.end(reason || 'Bad Request');
req.resume();
if (res.headersSent) {
debugLog(options, 'Headers already sent, can\'t close connection.');
return;
}
const resCode = code || 400;
const resReason = reason || 'Bad Request';
debugLog(options, `Closing connection with ${resCode}: ${resReason}`);
res.writeHead(resCode, { Connection: 'close' });
res.end(resReason);
};

// Express proxies sometimes attach multipart data to a buffer
Expand Down Expand Up @@ -87,11 +99,13 @@ module.exports = (options, req, res, next) => {
// Reset upload timer in case of file limit reached.
uploadTimer.clear();
// Run a user defined limit handler if it has been set.
if (isFunc(options.limitHandler)) return options.limitHandler(req, res, next);
if (isFunc(options.limitHandler)) {
options.limitHandler(req, res, next);
}
// Close connection with 413 code and do cleanup if abortOnLimit set(default: false).
if (options.abortOnLimit) {
debugLog(options, `Aborting upload because of size limit ${field}->${filename}.`);
!isFunc(options.limitHandler) ? closeConnection(413, options.responseOnLimit) : '';
closeConnection(413, options.responseOnLimit);
cleanup();
}
});
Expand Down

0 comments on commit 24749bd

Please sign in to comment.