Skip to content

Commit e620de6

Browse files
deokjinkimjuanarbol
authored andcommittedJan 31, 2023
http: refactor to use validateHeaderName
Remove duplicate implementation by using validateHeaderName. PR-URL: #46143 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 94605b1 commit e620de6

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
@@ -3619,13 +3619,18 @@ Passing an `AbortSignal` and then calling `abort` on the corresponding
36193619
`AbortController` will behave the same way as calling `.destroy()` on the
36203620
request itself.
36213621

3622-
## `http.validateHeaderName(name)`
3622+
## `http.validateHeaderName(name[, label])`
36233623

36243624
<!-- YAML
36253625
added: v14.3.0
3626+
changes:
3627+
- version: REPLACEME
3628+
pr-url: https://github.com/nodejs/node/pull/46143
3629+
description: The `label` parameter is added.
36263630
-->
36273631

36283632
* `name` {string}
3633+
* `label` {string} Label for error message. **Default:** `'Header name'`.
36293634

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

‎lib/_http_outgoing.js

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

631-
const validateHeaderName = hideStackFrames((name) => {
631+
const validateHeaderName = hideStackFrames((name, label) => {
632632
if (typeof name !== 'string' || !name || !checkIsHttpToken(name)) {
633-
throw new ERR_INVALID_HTTP_TOKEN('Header name', name);
633+
throw new ERR_INVALID_HTTP_TOKEN(label || 'Header name', name);
634634
}
635635
});
636636

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.