forked from tomphttp/bare-server-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitHeaderUtil.js
More file actions
85 lines (65 loc) · 1.64 KB
/
Copy pathsplitHeaderUtil.js
File metadata and controls
85 lines (65 loc) · 1.64 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// import Headers in NodeJS environments
// this line can safely be removed in browser environments
import { Headers } from './AbstractMessage.js';
/**
*
* @typedef {object} ErrorResult
* @property {{message:string,code:string,id:string}} error
*/
/** @constant
@type {number}
@default
*/
const MAX_HEADER_VALUE = 3072;
/**
*
* @param {Headers} headers
* @returns {Headers} split headers
*/
export function split_headers(headers) {
headers = new Headers(headers);
if (headers.has('x-bare-headers')) {
const value = headers.get('x-bare-headers');
if (value.length > MAX_HEADER_VALUE) {
headers.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++;
headers.set(`x-bare-headers-${id}`, `;${part}`);
}
}
}
return headers;
}
/**
* @description Joins headers in object, according to spec
* @param {Headers} headers joined headers
*/
export function join_headers(headers) {
headers = new Headers(headers);
const prefix = 'x-bare-headers';
if (headers.has(`${prefix}-0`)) {
const join = [];
for (let [header, value] of headers) {
if (!header.startsWith(prefix)) {
continue;
}
if (!value.startsWith(';')) {
return {
error: {
code: 'INVALID_BARE_HEADER',
id: `request.headers.${header}`,
message: `Value didn't begin with semi-colon.`,
},
};
}
value = value.slice(1);
const id = parseInt(header.slice(prefix.length + 1));
join[id] = value;
headers.delete(header);
}
headers.set(prefix, join.join(''));
}
return headers;
}