Skip to content

Commit

Permalink
fixup: bug fixes api
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Toozs committed Jul 8, 2024
1 parent d18f9ec commit ce04e83
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ const api = {
}
return { returnTagCount, isImplicitDeny };
}
let bb;
let formDataParser;
let fileEventData = null;

if (apiMethod === 'objectPost') {
bb = busboy({ headers: request.headers });
formDataParser = busboy({ headers: request.headers });
}

return async.waterfall([
Expand All @@ -204,39 +204,41 @@ const api = {
request.formData = {};
let totalFieldSize = 0;
const MAX_FIELD_SIZE = 20 * 1024; // 20KB
bb.on('field', (fieldname, val) => {
formDataParser.on('field', (fieldname, val) => {
totalFieldSize += Buffer.byteLength(val, 'utf8');
if (totalFieldSize > MAX_FIELD_SIZE) {
const err = errors.MaxPostPreDataLengthExceeded;
bb.emit('error', err);
formDataParser.emit('error', err);
}
// Convert fieldname to lowercase for case-insensitive comparison
const lowerFieldname = fieldname.toLowerCase();
request.formData[lowerFieldname] = val;
});

bb.on('file', (fieldname, file, filename, encoding, mimetype) => {
formDataParser.on('file', (fieldname, file, filename, encoding, mimetype) => {
fileEventData = { fieldname, file, filename, encoding, mimetype };
return next(null);
});

bb.on('finish', () => {
// if fields are found but no file, continue
formDataParser.on('finish', () => {
// No file field == error
if (!fileEventData) {
return next(null);
const err = errors.InvalidArgument
.customizeDescription('POST requires exactly one file upload per request.');
formDataParser.emit('error', err);
}
return undefined;
});

bb.on('error', (err) => {
formDataParser.on('error', (err) => {
log.trace('Error processing form data:', {
error: err,
});
request.unpipe(bb);
request.unpipe(formDataParser);
return next(err);
});

request.pipe(bb);
request.pipe(formDataParser);
} else {
// issue 100 Continue to the client
writeContinue(request, response);
Expand Down

0 comments on commit ce04e83

Please sign in to comment.