UNPKG

check_header

Version:

Check if headers aren't invalid for correct use in setHeader or writeHead functions

1 lines 2.27 kB
{"version":3,"sources":["src/headers.es6"],"names":[],"mappings":";;;;;;;;AAMA,SAAS,sBAAsB,CAAC,GAAG,EAAE;AACnC,OAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;AACnC,QAAM,EAAE,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AAC7B,QAAI,EAAE,KAAK,CAAC,EAAE,SAAS;AACvB,QAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,GAAG,IAAI,EAAE,KAAK,GAAG,EAAE,OAAO,IAAI,CAAC;GACrD;AACD,SAAO,KAAK,CAAC;CACd;;;;AAID,IAAM,KAAK,GAAG,+BAA+B,CAAC;AAC9C,SAAS,gBAAgB,CAAC,GAAG,EAAE;AAC7B,SAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;CACnD;;AAED,SAAS,OAAO,CAAC,OAAO,EAAE;AACxB,OAAK,IAAM,QAAQ,IAAI,OAAO,EAAE;AAC9B,QAAI,OAAO,CAAC,cAAc,CAAC,QAAQ,CAAC,EAAE;AACpC,UAAI,sBAAsB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,IAAI,EAAE;AACtD,eAAO,KAAK,CAAC;OACd;AACD,UAAI,gBAAgB,CAAC,QAAQ,CAAC,KAAK,KAAK,EAAE;AACxC,eAAO,KAAK,CAAC;OACd;KACF;GACF;AACD,SAAO,IAAI,CAAC;CACb;;AAED,MAAM,CAAC,OAAO,GAAG;AACf,wBAAsB,EAAE,sBAAsB;AAC9C,kBAAgB,EAAE,gBAAgB;AAClC,SAAO,EAAE,OAAO;CACjB,CAAC","file":"src/headers.es6","sourcesContent":["// From NodeJS - https://github.com/nodejs/node/commit/2a2d3e1b0333441d1bd00def06d687f807067f6e\n\n// True if val contains an invalid field-vchar\n// field-value = *( field-content / obs-fold )\n// field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]\n// field-vchar = VCHAR / obs-text\nfunction checkInvalidHeaderChar(val) {\n for (let i = 0; i < val.length; i++) {\n const ch = val.charCodeAt(i);\n if (ch === 9) continue;\n if (ch <= 31 || ch > 255 || ch === 127) return true;\n }\n return false;\n}\n\n// Verifies that the given val is a valid HTTP token\n// per the rules defined in RFC 7230\nconst token = /^[a-zA-Z0-9_!#$%&'*+.^`|~-]+$/;\nfunction checkIsHttpToken(val) {\n return typeof val === 'string' && token.test(val);\n}\n// Get object with headers and return bool\nfunction isValid(headers) {\n for (const property in headers) {\n if (headers.hasOwnProperty(property)) {\n if (checkInvalidHeaderChar(headers[property]) === true) {\n return false;\n }\n if (checkIsHttpToken(property) === false) {\n return false;\n }\n }\n }\n return true;\n}\n\nmodule.exports = {\n checkInvalidHeaderChar: checkInvalidHeaderChar,\n checkIsHttpToken: checkIsHttpToken,\n isValid: isValid,\n};\n"]}