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 RafaelGSS committed Jan 20, 2023
1 parent 36ae3cc commit 907d67d
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 @@ -3657,13 +3657,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 @@ -627,9 +627,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 @@ -932,9 +932,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 907d67d

Please sign in to comment.