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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/api/http.md
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
Expand Up @@ -628,9 +628,9 @@ function matchHeader(self, state, field, value) {
}
}

const validateHeaderName = hideStackFrames((name) => {
const validateHeaderName = hideStackFrames((name, label = 'Header name') => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work the way you want, as it will be undefined if the second parameter is explicitly passed as undefined. Move it to a || conditional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied your suggestion. Thank you for your help.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW it's not correct, the default value is used no matter if undefined is passed explicitly or not.

((param = 'default value') => console.log(param))(); // 'default value'
((param = 'default value') => console.log(param))(undefined); // 'default value'
((param = 'default value') => console.log(param))(null); // null

if (typeof name !== 'string' || !name || !checkIsHttpToken(name)) {
throw new ERR_INVALID_HTTP_TOKEN('Header name', name);
throw new ERR_INVALID_HTTP_TOKEN(label, 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