-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathvalid_headers_only.ts
More file actions
40 lines (30 loc) · 907 Bytes
/
valid_headers_only.ts
File metadata and controls
40 lines (30 loc) · 907 Bytes
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
import { validateHeaderName, validateHeaderValue } from 'node:http';
import { isHopByHopHeader } from './is_hop_by_hop_header.js';
/**
* @see https://nodejs.org/api/http.html#http_message_rawheaders
*/
export const validHeadersOnly = (rawHeaders: string[]): string[] => {
const result = [];
let containsHost = false;
for (let i = 0; i < rawHeaders.length; i += 2) {
const name = rawHeaders[i];
const value = rawHeaders[i + 1];
try {
validateHeaderName(name);
validateHeaderValue(name, value);
} catch {
continue;
}
if (isHopByHopHeader(name)) {
continue;
}
if (name.toLowerCase() === 'host') {
if (containsHost) {
continue;
}
containsHost = true;
}
result.push(name, value);
}
return result;
};