-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathutils.js
More file actions
32 lines (28 loc) · 994 Bytes
/
Copy pathutils.js
File metadata and controls
32 lines (28 loc) · 994 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
'use strict'
function convertUrlToWebSocket (urlString) {
return urlString.replace(/^(http)(s)?:\/\//, 'ws$2://')
}
// Strip characters that are not valid in an HTTP header value, so proxying a
// reply with an invalid upstream header does not throw.
// Allowed: HTAB (0x09), SP (0x20)-~ (0x7E), and obs-text (0x80-0xFF).
function sanitizeHeaderValue (value) {
return value.replace(/[^\t\x20-\x7E\x80-\xFF]/g, '')
}
// Sanitize every header value in place. Values may be strings or arrays of
// strings (e.g. set-cookie); anything else is left untouched.
function sanitizeHeaders (headers) {
for (const key in headers) {
const value = headers[key]
if (typeof value === 'string') {
headers[key] = sanitizeHeaderValue(value)
} else if (Array.isArray(value)) {
headers[key] = value.map((v) => (typeof v === 'string' ? sanitizeHeaderValue(v) : v))
}
}
return headers
}
module.exports = {
convertUrlToWebSocket,
sanitizeHeaderValue,
sanitizeHeaders
}