Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
http: Expose http.validateHeaderName/Value#33119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2440,6 +2440,76 @@ events will be emitted in the following order: | ||
| Setting the `timeout` option or using the `setTimeout()` function will | ||
| not abort the request or do anything besides add a `'timeout'` event. | ||
| ## `http.validateHeaderName(name)` | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
| * `name` {string} | ||
| Performs the low-level validations on the provided `name` that are done when | ||
| `res.setHeader(name, value)` is called. | ||
| Passing illegal value as `name` will result in a [`TypeError`][] being thrown, | ||
| identified by `code: 'ERR_INVALID_HTTP_TOKEN'`. | ||
| It is not necessary to use this method before passing headers to an HTTP request | ||
| or response. The HTTP module will automatically validate such headers. | ||
| Examples: | ||
| Example: | ||
| ```js | ||
| const { validateHeaderName } = require('http'); | ||
| try { | ||
| validateHeaderName(''); | ||
| } catch (err) { | ||
| err instanceof TypeError; // --> true | ||
| err.code; // --> 'ERR_INVALID_HTTP_TOKEN' | ||
| err.message; // --> 'Header name must be a valid HTTP token [""]' | ||
| } | ||
| ``` | ||
| ## `http.validateHeaderValue(name, value)` | ||
osher marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
| * `name` {string} | ||
| * `value` {any} | ||
| Performs the low-level validations on the provided `value` that are done when | ||
| `res.setHeader(name, value)` is called. | ||
| Passing illegal value as `value` will result in a [`TypeError`][] being thrown. | ||
| * Undefined value error is identified by `code: 'ERR_HTTP_INVALID_HEADER_VALUE'`. | ||
| * Invalid value character error is identified by `code: 'ERR_INVALID_CHAR'`. | ||
| It is not necessary to use this method before passing headers to an HTTP request | ||
| or response. The HTTP module will automatically validate such headers. | ||
| Examples: | ||
| ```js | ||
| const { validateHeaderValue } = require('http'); | ||
| try { | ||
| validateHeaderValue('x-my-header', undefined); | ||
| } catch (err) { | ||
| err instanceof TypeError; // --> true | ||
| err.code === 'ERR_HTTP_INVALID_HEADER_VALUE'; // --> true | ||
| err.message; // --> 'Invalid value "undefined" for header "x-my-header"' | ||
| } | ||
| try { | ||
| validateHeaderValue('x-my-header', 'oʊmɪɡə'); | ||
| } catch (err) { | ||
| err instanceof TypeError; // --> true | ||
| err.code === 'ERR_INVALID_CHAR'; // --> true | ||
| err.message; // --> 'Invalid character in header content ["x-my-header"]' | ||
| } | ||
| ``` | ||
| [`--insecure-http-parser`]: cli.html#cli_insecure_http_parser | ||
| [`--max-http-header-size`]: cli.html#cli_max_http_header_size_size | ||
| [`'checkContinue'`]: #http_event_checkcontinue | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| 'use strict'; | ||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const { validateHeaderName, validateHeaderValue } = require('http'); | ||
| // Expected static methods | ||
| isFunc(validateHeaderName, 'validateHeaderName'); | ||
| isFunc(validateHeaderValue, 'validateHeaderValue'); | ||
| // Expected to be useful as static methods | ||
| console.log('validateHeaderName'); | ||
| // - when used with valid header names - should not throw | ||
| [ | ||
| 'user-agent', | ||
| 'USER-AGENT', | ||
| 'User-Agent', | ||
| 'x-forwarded-for' | ||
| ].forEach((name) => { | ||
| console.log('does not throw for "%s"', name); | ||
| validateHeaderName(name); | ||
| }); | ||
| // - when used with invalid header names: | ||
| [ | ||
| 'איקס-פורוורד-פור', | ||
| 'x-forwarded-fםr', | ||
| ].forEach((name) => { | ||
| console.log('throws for: "%s"', name.slice(0, 50)); | ||
| assert.throws( | ||
| () => validateHeaderName(name), | ||
| { code: 'ERR_INVALID_HTTP_TOKEN' } | ||
| ); | ||
| }); | ||
| console.log('validateHeaderValue'); | ||
| // - when used with valid header values - should not throw | ||
| [ | ||
| ['x-valid', 1], | ||
| ['x-valid', '1'], | ||
| ['x-valid', 'string'], | ||
| ].forEach(([name, value]) => { | ||
| console.log('does not throw for "%s"', name); | ||
| validateHeaderValue(name, value); | ||
| }); | ||
| // - when used with invalid header values: | ||
| [ | ||
| // [header, value, expectedCode] | ||
| ['x-undefined', undefined, 'ERR_HTTP_INVALID_HEADER_VALUE'], | ||
| ['x-bad-char', 'לא תקין', 'ERR_INVALID_CHAR'], | ||
| ].forEach(([name, value, code]) => { | ||
| console.log('throws %s for: "%s: %s"', code, name, value); | ||
| assert.throws( | ||
| () => validateHeaderValue(name, value), | ||
| { code } | ||
| ); | ||
| }); | ||
| // Misc. | ||
| function isFunc(v, ttl) { | ||
| assert.ok(v.constructor === Function, `${ttl} is expected to be a function`); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.