Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

Commit

Permalink
http2: fix res.setHeader to update linkedlist APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
yosuke-furukawa committed May 5, 2017
1 parent 4d6d609 commit 608dc75
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-http2-server-set-header.js
Original file line number Diff line number Diff line change
@@ -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 =
'<html><head></head><body><h1>this is some data</h2></body></html>';

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());

0 comments on commit 608dc75

Please sign in to comment.