You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
http.ServerResponse.writeHead() can be called multiple times for a single response #8446
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.
I don't know exactly how to categorize this, as maybe write or writeHead should 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-encoding
I think the correct behavior should be for the second writeHead to throw if res.headersSent is 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.
Activity
Trott commentedon May 27, 2017
@nodejs/http
Trott commentedon Mar 4, 2018
@nodejs/http @nodejs/documentation @nodejs/streams It would be great to get a determination on this:
mcollina commentedon Mar 5, 2018
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:
I think the correct behavior should be for the second
writeHeadto throw ifres.headersSentis true.Also, the following text is obscure to me:
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.awwright commentedon Jun 15, 2019
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.
marco-ippolito commentedon Nov 21, 2022
this has been fixed by #45508 @ShogunPanda
ShogunPanda commentedon Dec 14, 2022
@marco-ippolito Thanks, closing this!