Skip to content
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

http: refactor to use validateHeaderName #46143

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -3661,13 +3661,18 @@ Passing an `AbortSignal` and then calling `abort` on the corresponding
`AbortController` will behave the same way as calling `.destroy()` on the
request itself.

## `http.validateHeaderName(name)`
## `http.validateHeaderName(name[, label])`

<!-- YAML
added: v14.3.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/46143
description: The `label` parameter is added.
-->

* `name` {string}
* `label` {string} Label for error message. **Default:** `Header name`.
deokjinkim marked this conversation as resolved.
Show resolved Hide resolved

Performs the low-level validations on the provided `name` that are done when
`res.setHeader(name, value)` is called.
Expand Down
8 changes: 3 additions & 5 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,9 @@ function matchHeader(self, state, field, value) {
}
}

const validateHeaderName = hideStackFrames((name) => {
const validateHeaderName = hideStackFrames((name, label) => {
if (typeof name !== 'string' || !name || !checkIsHttpToken(name)) {
throw new ERR_INVALID_HTTP_TOKEN('Header name', name);
throw new ERR_INVALID_HTTP_TOKEN(label || 'Header name', name);
}
});

Expand Down Expand Up @@ -933,9 +933,7 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
field = key;
value = headers[key];
}
if (typeof field !== 'string' || !field || !checkIsHttpToken(field)) {
throw new ERR_INVALID_HTTP_TOKEN('Trailer name', field);
}
validateHeaderName(field, 'Trailer name');

// Check if the field must be sent several times
const isArrayValue = ArrayIsArray(value);
Expand Down