Skip to content

Commit 78a07bb

Browse files
deokjinkimRafaelGSS
authored andcommitted
http: refactor to use validateHeaderName
Remove duplicate implementation by using validateHeaderName. PR-URL: nodejs#46143 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent dbcafab commit 78a07bb

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

doc/api/http.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3699,13 +3699,18 @@ Passing an `AbortSignal` and then calling `abort` on the corresponding
36993699
`AbortController` will behave the same way as calling `.destroy()` on the
37003700
request itself.
37013701

3702-
## `http.validateHeaderName(name)`
3702+
## `http.validateHeaderName(name[, label])`
37033703

37043704
<!-- YAML
37053705
added: v14.3.0
3706+
changes:
3707+
- version: REPLACEME
3708+
pr-url: https://github.com/nodejs/node/pull/46143
3709+
description: The `label` parameter is added.
37063710
-->
37073711

37083712
* `name` {string}
3713+
* `label` {string} Label for error message. **Default:** `'Header name'`.
37093714

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

lib/_http_outgoing.js

Lines changed: 3 additions & 5 deletions
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

@@ -954,9 +954,7 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
954954
field = key;
955955
value = headers[key];
956956
}
957-
if (typeof field !== 'string' || !field || !checkIsHttpToken(field)) {
958-
throw new ERR_INVALID_HTTP_TOKEN('Trailer name', field);
959-
}
957+
validateHeaderName(field, 'Trailer name');
960958

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

0 commit comments

Comments
 (0)