Repository navigation
http.ServerResponse.writeHead() can be called multiple times for a single response #8446
Description
Activity
- addedhttpIssues and PRs related to the http subsystem.Issues and PRs related to the http subsystem.
on Sep 8, 2016 @nodejs/http
@nodejs/http @nodejs/documentation @nodejs/streams It would be great to get a determination on this:
I'm not sure if this is unintended behavior or simply misleading naming/documentation
I don't know exactly how to categorize this, as maybe
writeorwriteHeadshould emit or throw an error.What I am getting using the server in the example is:
$ curl -v localhost:3000 * Rebuilt URL to: localhost:3000/ * Trying ::1... * TCP_NODELAY set * Connected to localhost (::1) port 3000 (#0) > GET / HTTP/1.1 > Host: localhost:3000 > User-Agent: curl/7.54.0 > Accept: */* > < HTTP/1.1 500 Internal Server Error < Date: Mon, 05 Mar 2018 10:37:42 GMT < Connection: keep-alive < Transfer-Encoding: chunked < * Illegal or missing hexadecimal sequence in chunked-encoding * stopped the pause stream! * Closing connection 0 curl: (56) Illegal or missing hexadecimal sequence in chunked-encodingI think the correct behavior should be for the second
writeHeadto throw ifres.headersSentis true.Also, the following text is obscure to me:
Additionally, calling writeHead() after a write() on the response object will modify the header of any subsequent response messages sent to the client with write(), or cause the server to send one final empty response with the modified head if end() is called without any additional messages being sent.
I do not understand what is being asked here, as multiple
write()should be issues for different parts of the same message. There are not multiple "response messages" in HTTP.Please note that 1xx status codes can be sent multiple times; the 2xx-5xx codes must only be sent once. See my related issue #27921.
Reacted by Jesús Leganés-Combarro and Pelle Wessman- addedhelp wantedIssues that need assistance from volunteers or PRs that need help to proceed.Issues that need assistance from volunteers or PRs that need help to proceed.
on Jun 26, 2020 this has been fixed by #45508 @ShogunPanda
@marco-ippolito Thanks, closing this!
Despite the API documentation's statement that http.ServerResponse.writeHead() "must only be called once on a message", multiple calls to the method simply modify the response header without complaint.
To test this (verified in 6.4, 4.x, and 0.10): Create an instance of http.Server whose request listener writes the response header more than once, then calls write() or end() on the response.
Additionally, calling writeHead() after a write() on the response object will modify the header of any subsequent response messages sent to the client with write(), or cause the server to send one final empty response with the modified head if end() is called without any additional messages being sent.
Example code:
require('http').createServer(function(req,res){res.writeHead(500,'Internal Server Error');res.write('Hello World!');setImmediate(function(){res.writeHead(200);res.end();});}).listen(3000);
I'm not sure if this is unintended behavior or simply misleading naming/documentation, but one might assume that the first call to writeHead() would cause any subsequent calls to either throw an error or fail silently.