Skip to content

Commit

Permalink
add null checks for parsing boundary from content-type hea
Browse files Browse the repository at this point in the history
der
  • Loading branch information
ilanashapiro committed Jul 17, 2023
1 parent 2c9f3a3 commit 2466aff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 6 additions & 5 deletions packages/http/src/mocker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,16 @@ function runCallbacks({
we cannot carry parsed informations in case of an error — which is what we do need instead.
*/
function parseBodyIfUrlEncoded(request: IHttpRequest, resource: IHttpOperation) {
const contentTypeHeader = caseless(request.headers || {}).get('content-type');
if (!contentTypeHeader) return request;

// parse boundary string from content-type in case media type is multipart/form-data
const multipart = require('parse-multipart-data');
const mediaInfo = caseless(request.headers || {}).get('content-type');
const multipartBoundary = multipart.getBoundary(mediaInfo);
const mediaType = mediaInfo.replace(";boundary=" + multipartBoundary, "");
const multipartBoundary = multipart.getBoundary(contentTypeHeader);
const mediaType = contentTypeHeader.replace(";boundary=" + multipartBoundary, "");

if (!mediaType) return request;

if (!is(mediaType, ['application/x-www-form-urlencoded', 'multipart/form-data'])) return request;

const specs = pipe(
O.fromNullable(resource.request),
O.chainNullableK(request => request.body),
Expand Down
6 changes: 3 additions & 3 deletions packages/http/src/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ const validateInputBody = (
E.chain(([requestBody, body, headers]) => {
// parse boundary string from content-type in case media type is multipart/form-data
const multipart = require('parse-multipart-data');
const mediaInfo = headers.get('content-type');
const multipartBoundary = multipart.getBoundary(mediaInfo);
const mediaType = mediaInfo.replace(new RegExp(";\\s*boundary=" + multipartBoundary), "");
const contentTypeHeader = headers.get('content-type');
const multipartBoundary = contentTypeHeader ? multipart.getBoundary(contentTypeHeader) : "";
const mediaType = contentTypeHeader ? contentTypeHeader.replace(new RegExp(";\\s*boundary=" + multipartBoundary), "") : contentTypeHeader;

const contentLength = parseInt(headers.get('content-length')) || 0;
if (contentLength === 0) {
Expand Down

0 comments on commit 2466aff

Please sign in to comment.