-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsplitHeaderUtil.ts
More file actions
71 lines (53 loc) · 1.41 KB
/
Copy pathsplitHeaderUtil.ts
File metadata and controls
71 lines (53 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { BareError } from './BareServer.js';
const MAX_HEADER_VALUE = 3072;
/**
*
* Splits headers according to spec
* @param headers
* @returns Split headers
*/
export function splitHeaders(headers: Headers): Headers {
const output = new Headers(headers);
if (headers.has('x-bare-headers')) {
const value = headers.get('x-bare-headers')!;
if (value.length > MAX_HEADER_VALUE) {
output.delete('x-bare-headers');
let split = 0;
for (let i = 0; i < value.length; i += MAX_HEADER_VALUE) {
const part = value.slice(i, i + MAX_HEADER_VALUE);
const id = split++;
output.set(`x-bare-headers-${id}`, `;${part}`);
}
}
}
return output;
}
/**
* Joins headers according to spec
* @param headers
* @returns Joined headers
*/
export function joinHeaders(headers: Headers): Headers {
const output = new Headers(headers);
const prefix = 'x-bare-headers';
if (headers.has(`${prefix}-0`)) {
const join: string[] = [];
for (const [header, value] of headers) {
if (!header.startsWith(prefix)) {
continue;
}
if (!value.startsWith(';')) {
throw new BareError(400, {
code: 'INVALID_BARE_HEADER',
id: `request.headers.${header}`,
message: `Value didn't begin with semi-colon.`,
});
}
const id = parseInt(header.slice(prefix.length + 1));
join[id] = value.slice(1);
output.delete(header);
}
output.set(prefix, join.join(''));
}
return output;
}