Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http.ServerResponse.writeHead() can be called multiple times for a single response #8446

Closed
Hesulan opened this issue Sep 8, 2016 · 6 comments
Labels
help wanted Issues that need assistance from volunteers or PRs that need help to proceed. http Issues or PRs related to the http subsystem.

Comments

@Hesulan
Copy link

Hesulan commented Sep 8, 2016

  • Version: 6.4
  • Platform: Ubuntu 16.04 64-bit
  • Subsystem: http

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.

@mscdex mscdex added the http Issues or PRs related to the http subsystem. label Sep 8, 2016
@Trott
Copy link
Member

Trott commented May 27, 2017

@nodejs/http

@Trott
Copy link
Member

Trott commented Mar 4, 2018

@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

@mcollina
Copy link
Member

mcollina commented Mar 5, 2018

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.

@awwright
Copy link
Contributor

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.

@jasnell jasnell added the help wanted Issues that need assistance from volunteers or PRs that need help to proceed. label Jun 26, 2020
@marco-ippolito
Copy link
Member

marco-ippolito commented Nov 21, 2022

this has been fixed by #45508 @ShogunPanda

@ShogunPanda
Copy link
Contributor

@marco-ippolito Thanks, closing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues that need assistance from volunteers or PRs that need help to proceed. http Issues or PRs related to the http subsystem.
Projects
None yet
Development

No branches or pull requests

8 participants