Skip to content

Commit

Permalink
http: refactor to use validateHeaderName
Browse files Browse the repository at this point in the history
Remove duplicate implementation by using validateHeaderName.

PR-URL: #46143
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
deokjinkim authored and juanarbol committed Jan 31, 2023
1 parent 94605b1 commit e620de6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 6 additions & 1 deletion doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -3619,13 +3619,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'`.

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

0 comments on commit e620de6

Please sign in to comment.