HTTP writeHead(headers) is not the same as setHeader() #5036

Closed
hueniverse opened this Issue Mar 15, 2013 · 6 comments

Projects

None yet

4 participants

@hueniverse

I expected the following to return the same headers:

var Http = require('http');

Http.createServer(function (req, res) {
    res.setHeader('Test', '1');
    res.setHeader('test', '2');
    res.writeHead(200);
    res.end();
}).listen(8000);

Http.createServer(function (req, res) {
    res.writeHead(200, { Test: '1', test: '2' });
    res.end();
}).listen(8001);

Http.createServer(function (req, res) {
    res.setHeader('Test', '1');
    res.writeHead(200, { test: '2' });
    res.end();
}).listen(8002);

The output:

$ curl -v http://localhost:8000
< HTTP/1.1 200 OK
< test: 2
< Date: Fri, 15 Mar 2013 23:37:28 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked

$ curl -v http://localhost:8001
< Test: 1
< test: 2
< Date: Fri, 15 Mar 2013 23:38:28 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked

$ curl -v http://localhost:8002
< Test: 1
< test: 2
< Date: Fri, 15 Mar 2013 23:39:35 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked

Basically, writeHead(headers) does not pass the headers through the normalization code in setHeader() which prevents duplicate headers due to changes in lower/upper case.

This can be easily fix (I will send a pull request if you confirm this as a bug), or at least documented.

@bnoordhuis
Member

That's a bug. I'll take a PR provided it doesn't affect performance.

@hueniverse

Will do.

@isaacs
isaacs commented Mar 17, 2013

I'd prefer to leave this as is

On Friday, March 15, 2013, Eran Hammer wrote:

Will do.


Reply to this email directly or view it on GitHubhttps://github.com/joyent/node/issues/5036#issuecomment-14995999
.

@isaacs
isaacs commented Mar 17, 2013

Premature send.

I'd prefer to leave this as-is and fix it in 0.12. It's not a new bug, as
far a I can tell.

On Saturday, March 16, 2013, Isaac Schlueter wrote:

I'd prefer to leave this as is

On Friday, March 15, 2013, Eran Hammer wrote:

Will do.


Reply to this email directly or view it on GitHubhttps://github.com/joyent/node/issues/5036#issuecomment-14995999
.

@hueniverse

Should we update the docs to make people aware? If you care about this issue, the workaround is trivial.

@kesla
kesla commented Jan 29, 2014

Hey! I just ran into this today and got super confused. If you got a PR for this asap, would it be possible to get this in before 0.12?

@kesla kesla added a commit to kesla/node that referenced this issue Feb 10, 2014
@kesla kesla http: in writeHeader, set key to lowercase. fixes #5036 d0f745c
@tjfontaine tjfontaine added a commit that closed this issue Feb 19, 2014
@kesla @tjfontaine kesla + tjfontaine http: avoid duplicate keys in writeHead
Use setHeader in writeHead to avoid sending duplicate headers

Fixes #5036
b105997
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment