From 92d05cc54b5bd7173f26cb1d8461af605cb7461b Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Tue, 2 Aug 2022 11:31:12 +0800 Subject: [PATCH 1/5] Added test cases (http2) that will fail if using undocumented `res._implicitHeader()` --- test/session.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/test/session.js b/test/session.js index 7416b261..155d855a 100644 --- a/test/session.js +++ b/test/session.js @@ -16,6 +16,16 @@ var Cookie = require('../session/cookie') var min = 60 * 1000; +var describeHttp2 = describe.skip +try { + var http2 = require('http2') + describeHttp2 = describe +} catch (err) { + if (err) { + console.log('http2 tests disabled.') + } +} + describe('session()', function(){ it('should export constructors', function(){ assert.strictEqual(typeof session.Session, 'function') @@ -2294,6 +2304,37 @@ describe('session()', function(){ }) }) }) + + describeHttp2('http2', function () { + it('should work with http2 server', function (done) { + var server = createHttp2Server(null, function (req, res) { + res.setHeader(http2.constants.HTTP2_HEADER_CONTENT_TYPE, 'text/plain') + res.end('Hello, world!') + }) + server.on('listening', function () { + var client = createHttp2Client(server.address().port) + // using ES5 as Node.js <=4.0.0 does not have Computed Property Names + var reqHeaders = {} + reqHeaders[http2.constants.HTTP2_HEADER_PATH] = '/' + var request = client.request(reqHeaders) + request.on('response', function (headers) { + assert.strictEqual(headers[http2.constants.HTTP2_HEADER_STATUS], 200) + assert.strictEqual(headers[http2.constants.HTTP2_HEADER_CONTENT_TYPE], 'text/plain') + }) + var chunks = [] + request.on('data', function (chunk) { + chunks.push(chunk) + }) + request.on('end', function () { + closeHttp2(client, server, function () { + assert.strictEqual(Buffer.concat(chunks).toString(), 'Hello, world!') + done() + }) + }) + request.end() + }) + }) + }) }) function cookie(res) { @@ -2317,6 +2358,48 @@ function createServer (options, respond) { return server.on('request', createRequestListener(opts, fn)) } +function createHttp2Server (options, respond) { + var fn = respond + var opts = options + var server = http2.createServer() + + // setup, options, respond + if (typeof arguments[0] === 'function') { + opts = arguments[1] + fn = arguments[2] + + server.on('request', arguments[0]) + } + + server.on('request', createRequestListener(opts, fn)) + server.listen(0, '127.0.0.1') + return server +} + +function createHttp2Client (port) { + return http2.connect('http://127.0.0.1:' + port) +} + +function closeHttp2 (client, server, callback) { + if (typeof client.shutdown === 'function') { + // this is the node v8.x way of closing the connections + client.shutdown({}, function () { + server.close(function () { + callback() + }) + }) + } else { + // this is the node v9.x onwards way of closing the connections + client.close(function () { + // force existing connections to time out after 1ms. + // this is done to force the server to close in some cases where it wouldn't do it otherwise. + server.close(function () { + callback() + }) + }) + } +} + function createRequestListener(opts, fn) { var _session = createSession(opts) var respond = fn || end From df7163e9d32beb72ffa08f67d6d2f5ede9ba1855 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Wed, 3 Aug 2022 09:49:50 +0800 Subject: [PATCH 2/5] Replaced the usage of undocumented http API with the proper writeHead method (bringing about http2 support) - https://github.com/nodejs/node/blob/a8bc202d855cc7d14895db38a2ac09d1f873e803/lib/_http_server.js#L281 - https://github.com/nodejs/node/blob/a8bc202d855cc7d14895db38a2ac09d1f873e803/lib/_http_server.js#L282 --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 40a442ba..9279a897 100644 --- a/index.js +++ b/index.js @@ -273,7 +273,7 @@ function session(options) { } if (!res._header) { - res._implicitHeader() + res.writeHead(res.statusCode) } if (chunk == null) { From 416ae6f138ce2df20c3d46e9f095afb4904c5d19 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Wed, 3 Aug 2022 10:37:48 +0800 Subject: [PATCH 3/5] Removed explicit http2 headers for less verbosity (using default values) --- test/session.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/session.js b/test/session.js index 155d855a..8505a2e4 100644 --- a/test/session.js +++ b/test/session.js @@ -2313,10 +2313,7 @@ describe('session()', function(){ }) server.on('listening', function () { var client = createHttp2Client(server.address().port) - // using ES5 as Node.js <=4.0.0 does not have Computed Property Names - var reqHeaders = {} - reqHeaders[http2.constants.HTTP2_HEADER_PATH] = '/' - var request = client.request(reqHeaders) + var request = client.request() request.on('response', function (headers) { assert.strictEqual(headers[http2.constants.HTTP2_HEADER_STATUS], 200) assert.strictEqual(headers[http2.constants.HTTP2_HEADER_CONTENT_TYPE], 'text/plain') From 35880d6509e6ee6484521f746f936f8a56503be5 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Wed, 3 Aug 2022 19:31:48 +0800 Subject: [PATCH 4/5] Added test cases to check for `res._header` and `res.headersSent` equivalence --- test/session.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/session.js b/test/session.js index 8505a2e4..965fc94b 100644 --- a/test/session.js +++ b/test/session.js @@ -1381,6 +1381,25 @@ describe('session()', function(){ }) }); + describe('res._header patch', function () { + it('should be equivalent to res.headersSent', function (done) { + request(createServer(function(req, res) { + assert.strictEqual(!!res._header, !!res.headersSent, + 'res._header should be equal to res.headersSent (prior state change)') + + var oldState = !!res._header; + res.end('ended') + var newState = !!res._header; + assert.notStrictEqual(oldState, newState) + + assert.strictEqual(!!res._header, !!res.headersSent, + 'res._header should be equal to res.headersSent (after state change)') + })) + .get('/') + .expect(200, 'ended', done) + }) + }) + describe('res.end patch', function () { it('should correctly handle res.end/res.write patched prior', function (done) { function setup (req, res) { From 36618af14088362997f7f66d8c412ebd871c6c0d Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Wed, 3 Aug 2022 18:25:42 +0800 Subject: [PATCH 5/5] Replaced the usage of undocumented `res._header` with `res.headersSent` (regression-safe) - https://github.com/nodejs/node/blob/93e0bf9abf350637d772bcce14a5d9527b733300/lib/_http_outgoing.js#L741-L746 --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 9279a897..8fb21451 100644 --- a/index.js +++ b/index.js @@ -272,7 +272,7 @@ function session(options) { return ret; } - if (!res._header) { + if (typeof res.headersSent === 'boolean' ? !res.headersSent : !res._header) { res.writeHead(res.statusCode) }