Skip to content

Commit ed094ce

Browse files
committed
quote Content-Type boundary param if needed as per RFC 2616
1 parent ee7e131 commit ed094ce

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Multipart.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,32 @@ export class Multipart implements Part {
439439
return Multipart.combineArrays(result);
440440
}
441441

442+
private static boundaryShouldBeQuoted(boundary: Uint8Array): boolean {
443+
for (const byte of boundary) {
444+
if (
445+
byte === Multipart.HT
446+
|| byte === Multipart.SP
447+
|| byte === 0x22
448+
|| byte === 0x28
449+
|| byte === 0x29
450+
|| byte === 0x2c
451+
|| byte === 0x2f
452+
|| (byte >= Multipart.COLON && byte <= 0x40)
453+
|| (byte >= 0x5b && byte <= 0x5d)
454+
|| byte === 0x7b
455+
|| byte === 0x7d
456+
) return true;
457+
}
458+
return false;
459+
}
460+
442461
/**
443462
* Set the `Content-Type` header of this multipart based on {@link mediaType} and {@link boundary}.
444463
*/
445464
private setHeaders() {
446-
this.headers.set("Content-Type", this.#mediaType + "; boundary=" + new TextDecoder().decode(this.#boundary));
465+
const shouldQuoteBoundary = Multipart.boundaryShouldBeQuoted(this.#boundary);
466+
const boundaryString = new TextDecoder().decode(this.#boundary);
467+
const boundary = shouldQuoteBoundary ? `"${boundaryString.replace(/"/g, '\\"')}"` : boundaryString;
468+
this.headers.set("Content-Type", this.#mediaType + "; boundary=" + boundary);
447469
}
448470
}

0 commit comments

Comments
 (0)