MCPcopy Create free account
hub / github.com/nodejs/undici / parseMultipartFormDataHeaders

Function parseMultipartFormDataHeaders

lib/web/fetch/formdata-parser.js:315–468  ·  view source on GitHub ↗

* @see https://andreubotella.github.io/multipart-form-data/#parse-multipart-form-data-headers * @param {Buffer} input * @param {{ position: number }} position

(input, position)

Source from the content-addressed store, hash-verified

313 * @param {{ position: number }} position
314 */
315function parseMultipartFormDataHeaders (input, position) {
316 // 1. Let name, filename and contentType be null.
317 let name = null
318 let filename = null
319 let contentType = null
320 let encoding = null
321
322 // 2. While true:
323 while (true) {
324 // 2.1. If position points to a sequence of bytes starting with 0x0D 0x0A (CR LF):
325 if (input[position.position] === 0x0d && input[position.position + 1] === 0x0a) {
326 // 2.1.1. If name is null, return failure.
327 if (name === null) {
328 throw parsingError('header name is null')
329 }
330
331 // 2.1.2. Return name, filename and contentType.
332 return { name, filename, contentType, encoding }
333 }
334
335 // 2.2. Let header name be the result of collecting a sequence of bytes that are
336 // not 0x0A (LF), 0x0D (CR) or 0x3A (:), given position.
337 let headerName = collectASequenceOfBytes(
338 (char) => char !== 0x0a && char !== 0x0d && char !== 0x3a,
339 input,
340 position
341 )
342
343 // 2.3. Remove any HTTP tab or space bytes from the start or end of header name.
344 headerName = removeChars(headerName, true, true, (char) => char === 0x9 || char === 0x20)
345
346 // 2.4. If header name does not match the field-name token production, return failure.
347 if (!HTTP_TOKEN_CODEPOINTS.test(headerName.toString())) {
348 throw parsingError('header name does not match the field-name token production')
349 }
350
351 // 2.5. If the byte at position is not 0x3A (:), return failure.
352 if (input[position.position] !== 0x3a) {
353 throw parsingError('expected :')
354 }
355
356 // 2.6. Advance position by 1.
357 position.position++
358
359 // 2.7. Collect a sequence of bytes that are HTTP tab or space bytes given position.
360 // (Do nothing with those bytes.)
361 collectASequenceOfBytes(
362 (char) => char === 0x20 || char === 0x09,
363 input,
364 position
365 )
366
367 // 2.8. Byte-lowercase header name and switch on the result:
368 switch (bufferToLowerCasedHeaderName(headerName)) {
369 case 'content-disposition': {
370 name = filename = null
371 // Track whether filename was set from the extended (RFC 5987) form so
372 // a subsequent legacy `filename` attribute does not override it.

Callers 1

multipartFormDataParserFunction · 0.85

Calls 8

parsingErrorFunction · 0.85
collectASequenceOfBytesFunction · 0.85
isTokenFunction · 0.85
isomorphicDecodeFunction · 0.85
removeCharsFunction · 0.70
toStringMethod · 0.45

Tested by

no test coverage detected