Skip to content

Commit

Permalink
return indices when CRLF is encountered
Browse files Browse the repository at this point in the history
  • Loading branch information
zefir-git committed Oct 6, 2024
1 parent 8fdda19 commit ba5bd72
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,29 +272,28 @@ export class Multipart implements Part {
* @param data Multipart body bytes
* @param boundary The multipart boundary bytes
* @param [start] The index to start the search at (i.e. the number of bytes to skip/ignore at the beginning of the byte array). Defaults to 0.
* @returns The start and end index of the boundary delimiter, or `null` if no boundary delimiter can be found
* @internal
*/
private static findBoundaryBounds(data: Uint8Array, boundary: Uint8Array, start = 0): [number, number] | null {
if (start >= data.length) return null;
const boundaryStartIndex = Multipart.findSequenceIndex(data, Multipart.combineArrays([Multipart.CRLF, Multipart.DOUBLE_DASH, boundary]), start);
if (boundaryStartIndex === -1) return null;
// ignore any linear whitespace
let currentEndOfBoundaryIndex = boundaryStartIndex + boundary.length + 4;
while (currentEndOfBoundaryIndex < data.length) {
const byte = data[currentEndOfBoundaryIndex];
if (byte === Multipart.CR) break;
if (byte === Multipart.LF) return null;
if (byte === Multipart.CR && data[currentEndOfBoundaryIndex + 1] === Multipart.LF)
return [boundaryStartIndex, currentEndOfBoundaryIndex + 2];
if (byte === Multipart.SP || byte === 0x09) {
currentEndOfBoundaryIndex++;
continue;
}
// encountered non-linear whitespace after boundary and before any CR or LF
// encountered non-linear whitespace after boundary and before any CRLF
// meaning the boundary could not be terminated, therefore continue search for boundary
return Multipart.findBoundaryBounds(data, boundary, boundaryStartIndex + 2);
}
if (data[currentEndOfBoundaryIndex + 1] !== Multipart.LF) return null;

return [boundaryStartIndex, currentEndOfBoundaryIndex + 2];
return null;
}

/**
Expand Down

0 comments on commit ba5bd72

Please sign in to comment.