Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions packages/kit/src/runtime/server/parse_body/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@ import { read_only_form_data } from './read_only_form_data.js';
* @param {import('types/helper').Headers} headers
*/
export function parse_body(raw, headers) {
if (!raw) return raw;
if (!raw || typeof raw !== 'string') return raw;

if (typeof raw === 'string') {
const [type, ...directives] = headers['content-type'].split(/;\s*/);
const [type, ...directives] = headers['content-type'].split(/;\s*/);

switch (type) {
case 'text/plain':
return raw;
switch (type) {
case 'text/plain':
return raw;

case 'application/json':
return JSON.parse(raw);
case 'application/json':
return JSON.parse(raw);

case 'application/x-www-form-urlencoded':
return get_urlencoded(raw);
case 'application/x-www-form-urlencoded':
return get_urlencoded(raw);

case 'multipart/form-data': {
const boundary = directives.find((directive) => directive.startsWith('boundary='));
if (!boundary) throw new Error('Missing boundary');
return get_multipart(raw, boundary.slice('boundary='.length));
}
default:
throw new Error(`Invalid Content-Type ${type}`);
case 'multipart/form-data': {
const boundary = directives.find((directive) => directive.startsWith('boundary='));
if (!boundary) throw new Error('Missing boundary');
return get_multipart(raw, boundary.slice('boundary='.length));
}
default:
throw new Error(`Invalid Content-Type ${type}`);
}

return raw;
}

/** @param {string} text */
Expand Down