From 78a07bb8957e1f222fbc0c5b9df2ad21209d3c64 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Tue, 17 Jan 2023 22:40:39 +0900 Subject: [PATCH] http: refactor to use `validateHeaderName` Remove duplicate implementation by using validateHeaderName. PR-URL: https://github.com/nodejs/node/pull/46143 Reviewed-By: Matteo Collina Reviewed-By: Antoine du Hamel --- doc/api/http.md | 7 ++++++- lib/_http_outgoing.js | 8 +++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index e2d00f4b0d1ea7..747e974c6d3911 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -3699,13 +3699,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 1a3a4750bf90a6..e362ea62472e45 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -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); } }); @@ -954,9 +954,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);