diff --git a/doc/api/http.md b/doc/api/http.md index bb28a440b79fda..bbefe1e49a8199 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -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])` * `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. diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 8c80eabaec9e74..60ea7ca5ef9c29 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -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); } }); @@ -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);