From 2e2458816edbc4bbebbae2cacb54e323b90bb348 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Thu, 15 Jan 2026 17:10:53 -0500 Subject: [PATCH 1/4] fix: improve content-type handling in res.send method --- lib/response.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/response.js b/lib/response.js index 7a2f0ecce56..152ee1e075c 100644 --- a/lib/response.js +++ b/lib/response.js @@ -134,7 +134,13 @@ res.send = function send(body) { switch (typeof chunk) { // string defaulting to html case 'string': - if (!this.get('Content-Type')) { + encoding = 'utf8'; + type = this.get('Content-Type'); + + // set content-type with charset in a single operation + if (type) { + this.set('Content-Type', setCharset(type, 'utf-8')); + } else { this.type('html'); } break; @@ -153,17 +159,6 @@ res.send = function send(body) { break; } - // write strings in utf-8 - if (typeof chunk === 'string') { - encoding = 'utf8'; - type = this.get('Content-Type'); - - // reflect this in content-type - if (typeof type === 'string') { - this.set('Content-Type', setCharset(type, 'utf-8')); - } - } - // determine if ETag should be generated var etagFn = app.get('etag fn') var generateETag = !this.get('ETag') && typeof etagFn === 'function' From 51caad20674d049a63e9608e9782f77e379959d2 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Thu, 15 Jan 2026 21:35:42 -0500 Subject: [PATCH 2/4] fix: ensure content-type is a string before setting charset in res.send --- lib/response.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/response.js b/lib/response.js index 152ee1e075c..8122c29e8f8 100644 --- a/lib/response.js +++ b/lib/response.js @@ -138,7 +138,7 @@ res.send = function send(body) { type = this.get('Content-Type'); // set content-type with charset in a single operation - if (type) { + if (typeof type === "string") { this.set('Content-Type', setCharset(type, 'utf-8')); } else { this.type('html'); From 1f683d5f1e283ec2e6bfa25c75cd827a7583b723 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Fri, 16 Jan 2026 15:46:26 -0500 Subject: [PATCH 3/4] fix: refactor content-type handling in res.send to use const and improve clarity --- lib/response.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/response.js b/lib/response.js index 8122c29e8f8..39e59116bd5 100644 --- a/lib/response.js +++ b/lib/response.js @@ -126,7 +126,6 @@ res.send = function send(body) { var chunk = body; var encoding; var req = this.req; - var type; // settings var app = this.app; @@ -135,11 +134,10 @@ res.send = function send(body) { // string defaulting to html case 'string': encoding = 'utf8'; - type = this.get('Content-Type'); + const type = this.get('Content-Type'); - // set content-type with charset in a single operation - if (typeof type === "string") { - this.set('Content-Type', setCharset(type, 'utf-8')); + if (typeof type === 'string') { + this.set('Content-Type', setCharset(type, encoding)); } else { this.type('html'); } From bf678ff1f4568cae16f24035a9e8d9c4fab64d38 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Fri, 16 Jan 2026 15:59:04 -0500 Subject: [PATCH 4/4] Apply suggestion from @bjohansebas --- lib/response.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/response.js b/lib/response.js index 39e59116bd5..1a4d2deff26 100644 --- a/lib/response.js +++ b/lib/response.js @@ -137,7 +137,7 @@ res.send = function send(body) { const type = this.get('Content-Type'); if (typeof type === 'string') { - this.set('Content-Type', setCharset(type, encoding)); + this.set('Content-Type', setCharset(type, 'utf-8')); } else { this.type('html'); }