Skip to content

Commit 907d67d

Browse files
deokjinkimRafaelGSS
authored andcommitted
http: refactor to use validateHeaderName
Remove duplicate implementation by using validateHeaderName. PR-URL: #46143 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 36ae3cc commit 907d67d

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

doc/api/http.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -3657,13 +3657,18 @@ Passing an `AbortSignal` and then calling `abort` on the corresponding
36573657
`AbortController` will behave the same way as calling `.destroy()` on the
36583658
request itself.
36593659

3660-
## `http.validateHeaderName(name)`
3660+
## `http.validateHeaderName(name[, label])`
36613661

36623662
<!-- YAML
36633663
added: v14.3.0
3664+
changes:
3665+
- version: REPLACEME
3666+
pr-url: https://github.com/nodejs/node/pull/46143
3667+
description: The `label` parameter is added.
36643668
-->
36653669

36663670
* `name` {string}
3671+
* `label` {string} Label for error message. **Default:** `'Header name'`.
36673672

36683673
Performs the low-level validations on the provided `name` that are done when
36693674
`res.setHeader(name, value)` is called.

lib/_http_outgoing.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,9 @@ function matchHeader(self, state, field, value) {
627627
}
628628
}
629629

630-
const validateHeaderName = hideStackFrames((name) => {
630+
const validateHeaderName = hideStackFrames((name, label) => {
631631
if (typeof name !== 'string' || !name || !checkIsHttpToken(name)) {
632-
throw new ERR_INVALID_HTTP_TOKEN('Header name', name);
632+
throw new ERR_INVALID_HTTP_TOKEN(label || 'Header name', name);
633633
}
634634
});
635635

@@ -932,9 +932,7 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
932932
field = key;
933933
value = headers[key];
934934
}
935-
if (typeof field !== 'string' || !field || !checkIsHttpToken(field)) {
936-
throw new ERR_INVALID_HTTP_TOKEN('Trailer name', field);
937-
}
935+
validateHeaderName(field, 'Trailer name');
938936

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

0 commit comments

Comments
 (0)