From 34ba176fd684618b4dea605a52513a89789caed5 Mon Sep 17 00:00:00 2001 From: Yosuke Furukawa Date: Thu, 4 May 2017 13:46:13 +0200 Subject: [PATCH] http2: fix res.setHeader to update linkedlist APIs PR-URL: https://github.com/nodejs/http2/pull/75 Reviewed-By: James M Snell --- lib/internal/http2/compat.js | 17 ++++++-- test/parallel/test-http2-server-set-header.js | 39 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-http2-server-set-header.js diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 92c27c9a8a..d42c97a518 100755 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -36,7 +36,8 @@ function setHeader(list, name, value) { function llistToHeaders(list, count) { var ret = {}; while (!linkedList.isEmpty(list)) { - var item = linkedList.shift(list); + var item = list._idlePrev; + linkedList.remove(item); var key = item[0]; if (ret[key]) { @@ -279,8 +280,18 @@ class Http2ServerResponse extends Stream { }; this[kStream] = stream; stream[kResponse] = this; - this[kHeaders] = linkedList.create(); - this[kTrailers] = linkedList.create(); + var headerList = { + _idleNext: null, + _idlePrev: null, + }; + linkedList.init(headerList); + this[kHeaders] = headerList; + var trailersList = { + _idleNext: null, + _idlePrev: null, + }; + linkedList.init(trailersList); + this[kHeaders] = trailersList; this.writable = true; stream.on('drain', onStreamResponseDrain); stream.on('error', onStreamResponseError); diff --git a/test/parallel/test-http2-server-set-header.js b/test/parallel/test-http2-server-set-header.js new file mode 100644 index 0000000000..7e97c52bb1 --- /dev/null +++ b/test/parallel/test-http2-server-set-header.js @@ -0,0 +1,39 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const http2 = require('http2'); +const path = require('path'); +const tls = require('tls'); +const net = require('net'); +const fs = require('fs'); +const body = + '

this is some data

'; + +const server = http2.createServer((req, res) => { + res.setHeader('foobar', 'baz'); + res.setHeader('X-POWERED-BY', 'node-test'); + res.end(body); +}); + +server.listen(0, common.mustCall(() => { + const client = http2.connect(`http://localhost:${server.address().port}`); + const headers = { ':path': '/' }; + const req = client.request(headers); + req.setEncoding('utf8'); + req.on('response', common.mustCall(function(headers) { + assert.strictEqual(headers['foobar'], 'baz'); + assert.strictEqual(headers['x-powered-by'], 'node-test'); + })); + + let data = ''; + req.on('data', (d) => data += d); + req.on('end', () => { + assert.strictEqual(body, data); + server.close(); + client.destroy(); + }); + req.end(); +})); + +server.on('error', common.mustNotCall());