From 8c92903b5081ac3484da673700d4fb0476624abb Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Thu, 17 Nov 2022 17:03:39 +0100 Subject: [PATCH 1/6] test: test case for multiple res.writeHead and res.getHeader --- test/parallel/test-http-write-head-2.js | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/parallel/test-http-write-head-2.js b/test/parallel/test-http-write-head-2.js index a47d0d72e3be56..028320633a0e1a 100644 --- a/test/parallel/test-http-write-head-2.js +++ b/test/parallel/test-http-write-head-2.js @@ -59,3 +59,63 @@ const http = require('http'); })); })); } + +{ + const server = http.createServer((req, res) => { + try { + res.writeHead(200,[ 'test', '1' ]); + res.writeHead(200,[ 'test2', '2' ]); + res.end(); + } catch (error) { + assert.strictEqual(err.code, 'ERR_INVALID_ARG_VALUE'); + } + }); + + server.listen(0, () => { + http.get({ port: server.address().port }, common.mustCall((res) => { + assert.equal('test' in res.headers, false) + assert.strictEqual(res.headers.test2, '2'); + res.resume().on('end', common.mustCall(() => { + server.close(); + })); + })); + }); +} + +{ + const server = http.createServer((req, res) => { + res.writeHead(200,[ 'test', '1' ]); + assert.equal(res.getHeader('test'), undefined) + res.end(); + }); + + server.listen(0, () => { + http.get({ port: server.address().port }, (res) => { + assert.strictEqual(res.headers.test, '1'); + res.resume().on('end', common.mustCall(() => { + server.close(); + })); + }); + }); +} + +{ + const server = http.createServer((req, res) => { + res.setHeader('test2','2') + res.writeHead(200,[ 'test', '1' ]); + assert.strictEqual(res.getHeader('test'), '1'); + assert.strictEqual(res.getHeader('test2'), '2'); + res.end(); + }); + + server.listen(0, () => { + http.get({ port: server.address().port }, (res) => { + assert.strictEqual(res.headers.test, '1'); + assert.strictEqual(res.headers.test2, '2'); + res.resume().on('end', common.mustCall(() => { + server.close(); + })); + }); + }); +} + From 24d17b616e59ad2a6d9f0ce78af7c8c148d99cd8 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Fri, 18 Nov 2022 11:48:33 +0100 Subject: [PATCH 2/6] fix: throw ERR_HTTP_HEADERS_SENT error after multiple writeHead --- lib/_http_server.js | 5 +++ test/parallel/test-http-write-head-2.js | 60 ------------------------- test/parallel/test-http-write-head.js | 24 +++++++++- 3 files changed, 28 insertions(+), 61 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index f882ee7d2a6cb1..26adf5af320bc8 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -331,6 +331,11 @@ ServerResponse.prototype._implicitHeader = function _implicitHeader() { ServerResponse.prototype.writeHead = writeHead; function writeHead(statusCode, reason, obj) { + + if (this._header) { + throw new ERR_HTTP_HEADERS_SENT('writeHead'); + } + const originalStatusCode = statusCode; statusCode |= 0; diff --git a/test/parallel/test-http-write-head-2.js b/test/parallel/test-http-write-head-2.js index 028320633a0e1a..a47d0d72e3be56 100644 --- a/test/parallel/test-http-write-head-2.js +++ b/test/parallel/test-http-write-head-2.js @@ -59,63 +59,3 @@ const http = require('http'); })); })); } - -{ - const server = http.createServer((req, res) => { - try { - res.writeHead(200,[ 'test', '1' ]); - res.writeHead(200,[ 'test2', '2' ]); - res.end(); - } catch (error) { - assert.strictEqual(err.code, 'ERR_INVALID_ARG_VALUE'); - } - }); - - server.listen(0, () => { - http.get({ port: server.address().port }, common.mustCall((res) => { - assert.equal('test' in res.headers, false) - assert.strictEqual(res.headers.test2, '2'); - res.resume().on('end', common.mustCall(() => { - server.close(); - })); - })); - }); -} - -{ - const server = http.createServer((req, res) => { - res.writeHead(200,[ 'test', '1' ]); - assert.equal(res.getHeader('test'), undefined) - res.end(); - }); - - server.listen(0, () => { - http.get({ port: server.address().port }, (res) => { - assert.strictEqual(res.headers.test, '1'); - res.resume().on('end', common.mustCall(() => { - server.close(); - })); - }); - }); -} - -{ - const server = http.createServer((req, res) => { - res.setHeader('test2','2') - res.writeHead(200,[ 'test', '1' ]); - assert.strictEqual(res.getHeader('test'), '1'); - assert.strictEqual(res.getHeader('test2'), '2'); - res.end(); - }); - - server.listen(0, () => { - http.get({ port: server.address().port }, (res) => { - assert.strictEqual(res.headers.test, '1'); - assert.strictEqual(res.headers.test2, '2'); - res.resume().on('end', common.mustCall(() => { - server.close(); - })); - }); - }); -} - diff --git a/test/parallel/test-http-write-head.js b/test/parallel/test-http-write-head.js index 2199f11be61bb1..85052ad59c1c57 100644 --- a/test/parallel/test-http-write-head.js +++ b/test/parallel/test-http-write-head.js @@ -58,7 +58,7 @@ const s = http.createServer(common.mustCall((req, res) => { }, { code: 'ERR_HTTP_HEADERS_SENT', name: 'Error', - message: 'Cannot render headers after they are sent to the client' + message: 'Cannot writeHead headers after they are sent to the client' }); res.end(); @@ -76,3 +76,25 @@ function runTest() { response.resume(); })); } + +{ + const server = http.createServer((req, res) => { + res.writeHead(200, [ 'test', '1' ]); + assert.throws(() => res.writeHead(200, [ 'test2', '2' ]), { + code: 'ERR_HTTP_HEADERS_SENT', + name: 'Error', + message: 'Cannot writeHead headers after they are sent to the client' + }); + res.end(); + }); + + server.listen(0, () => { + http.get({ port: server.address().port }, (res) => { + assert.strictEqual(res.headers.test, '1'); + assert.strictEqual('test2' in res.headers, false); + res.resume().on('end', common.mustCall(() => { + server.close(); + })); + }); + }); +} From 9ac34d7b2be5c35d7717e53ff8c44e13c4b1b4cc Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Fri, 18 Nov 2022 18:06:48 +0100 Subject: [PATCH 3/6] fix: changed error label --- lib/_http_server.js | 2 +- test/parallel/test-http-write-head.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index 26adf5af320bc8..1783a407370656 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -333,7 +333,7 @@ ServerResponse.prototype.writeHead = writeHead; function writeHead(statusCode, reason, obj) { if (this._header) { - throw new ERR_HTTP_HEADERS_SENT('writeHead'); + throw new ERR_HTTP_HEADERS_SENT('write'); } const originalStatusCode = statusCode; diff --git a/test/parallel/test-http-write-head.js b/test/parallel/test-http-write-head.js index 85052ad59c1c57..2f58e27851524b 100644 --- a/test/parallel/test-http-write-head.js +++ b/test/parallel/test-http-write-head.js @@ -58,7 +58,7 @@ const s = http.createServer(common.mustCall((req, res) => { }, { code: 'ERR_HTTP_HEADERS_SENT', name: 'Error', - message: 'Cannot writeHead headers after they are sent to the client' + message: 'Cannot write headers after they are sent to the client' }); res.end(); @@ -83,7 +83,7 @@ function runTest() { assert.throws(() => res.writeHead(200, [ 'test2', '2' ]), { code: 'ERR_HTTP_HEADERS_SENT', name: 'Error', - message: 'Cannot writeHead headers after they are sent to the client' + message: 'Cannot write headers after they are sent to the client' }); res.end(); }); From ba88877771101cabbdb990c2cd8684952a559497 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Sat, 19 Nov 2022 09:38:13 +0100 Subject: [PATCH 4/6] fix: removed error message check and added common.mustCall --- test/parallel/test-http-write-head.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/parallel/test-http-write-head.js b/test/parallel/test-http-write-head.js index 2f58e27851524b..e132f607ba0ec7 100644 --- a/test/parallel/test-http-write-head.js +++ b/test/parallel/test-http-write-head.js @@ -58,7 +58,6 @@ const s = http.createServer(common.mustCall((req, res) => { }, { code: 'ERR_HTTP_HEADERS_SENT', name: 'Error', - message: 'Cannot write headers after they are sent to the client' }); res.end(); @@ -78,17 +77,16 @@ function runTest() { } { - const server = http.createServer((req, res) => { + const server = http.createServer(common.mustCall((req, res) => { res.writeHead(200, [ 'test', '1' ]); assert.throws(() => res.writeHead(200, [ 'test2', '2' ]), { code: 'ERR_HTTP_HEADERS_SENT', name: 'Error', - message: 'Cannot write headers after they are sent to the client' }); res.end(); - }); + })); - server.listen(0, () => { + server.listen(0, common.mustCall(() => { http.get({ port: server.address().port }, (res) => { assert.strictEqual(res.headers.test, '1'); assert.strictEqual('test2' in res.headers, false); @@ -96,5 +94,5 @@ function runTest() { server.close(); })); }); - }); + })); } From 07216533c68f2f7fe0de743d554bcf152d75a3b6 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 22 Nov 2022 14:34:20 +0100 Subject: [PATCH 5/6] fix: replaced writeHead with socket.write --- test/parallel/test-domain-multi.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-domain-multi.js b/test/parallel/test-domain-multi.js index 4a30dfeec0449e..6a53af42dfeffb 100644 --- a/test/parallel/test-domain-multi.js +++ b/test/parallel/test-domain-multi.js @@ -46,7 +46,10 @@ const server = http.createServer((req, res) => { b.on('error', common.mustCall((er) => { if (res) { - res.writeHead(500); + // Introduce an error on the client by writing unexpected data. + // The client is now expecting a chunk header so any letter will + // have the parser throw an error. + res.socket.write('H'); res.end('An error occurred'); } // res.writeHead(500), res.destroy, etc. From 0ccb0c2cefd6be3121f6c0ef4337540cff79f577 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 22 Nov 2022 14:47:16 +0100 Subject: [PATCH 6/6] fix: removed res.end --- test/parallel/test-domain-multi.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/parallel/test-domain-multi.js b/test/parallel/test-domain-multi.js index 6a53af42dfeffb..27cdff7b1c92f1 100644 --- a/test/parallel/test-domain-multi.js +++ b/test/parallel/test-domain-multi.js @@ -47,10 +47,8 @@ const server = http.createServer((req, res) => { b.on('error', common.mustCall((er) => { if (res) { // Introduce an error on the client by writing unexpected data. - // The client is now expecting a chunk header so any letter will - // have the parser throw an error. + // The client is now expecting a chunk header so any letter will have the parser throw an error. res.socket.write('H'); - res.end('An error occurred'); } // res.writeHead(500), res.destroy, etc. server.close();