|
1 |
| -import { _checkIsHttpToken, _checkInvalidHeaderChar } from '_http_common'; // eslint-disable-line |
2 |
| - |
3 | 1 | const HOST_HEADER_REGEX = /^((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]))(:([0-9]+))?$/;
|
4 | 2 |
|
5 | 3 | /**
|
@@ -42,15 +40,35 @@ const HOP_BY_HOP_HEADERS_REGEX = new RegExp(`^(${HOP_BY_HOP_HEADERS.join('|')})$
|
42 | 40 |
|
43 | 41 | export const isHopByHopHeader = (header) => HOP_BY_HOP_HEADERS_REGEX.test(header);
|
44 | 42 |
|
| 43 | +const TOKEN_REGEX = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/; |
| 44 | + |
| 45 | +/** |
| 46 | + * Verifies that the given val is a valid HTTP token per the rules defined in RFC 7230 |
| 47 | + * @see https://tools.ietf.org/html/rfc7230#section-3.2.6 |
| 48 | + * @see https://github.com/nodejs/node/blob/8cf5ae07e9e80747c19e0fc04fad48423707f62c/lib/_http_common.js#L222 |
| 49 | + */ |
| 50 | +const isHttpToken = (val) => TOKEN_REGEX.test(val); |
| 51 | + |
| 52 | +const HEADER_CHAR_REGEX = /[^\t\x20-\x7e\x80-\xff]/; |
| 53 | + |
| 54 | +/** |
| 55 | + * True if val contains an invalid field-vchar |
| 56 | + * field-value = *( field-content / obs-fold ) |
| 57 | + * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 58 | + * field-vchar = VCHAR / obs-text |
| 59 | + * @see https://github.com/nodejs/node/blob/8cf5ae07e9e80747c19e0fc04fad48423707f62c/lib/_http_common.js#L233 |
| 60 | + */ |
| 61 | +const isInvalidHeaderChar = (val) => HEADER_CHAR_REGEX.test(val); |
| 62 | + |
45 | 63 | // This code is based on Node.js' validateHeader() function from _http_outgoing.js module
|
46 | 64 | // (see https://github.com/nodejs/node/blob/189d29f39e6de9ccf10682bfd1341819b4a2291f/lib/_http_outgoing.js#L485)
|
47 | 65 | export const isInvalidHeader = (name, value) => {
|
48 | 66 | // NOTE: These are internal Node.js functions, they might stop working in the future!
|
49 | 67 | return typeof name !== 'string'
|
50 | 68 | || !name
|
51 |
| - || !_checkIsHttpToken(name) |
| 69 | + || !isHttpToken(name) |
52 | 70 | || value === undefined
|
53 |
| - || _checkInvalidHeaderChar(value); |
| 71 | + || isInvalidHeaderChar(value); |
54 | 72 | };
|
55 | 73 |
|
56 | 74 | const bulletproofDecodeURIComponent = (encodedURIComponent) => {
|
|
0 commit comments