Sitelet https://github.com/nodejs/node/issues/8446
Skip to content

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

Description

@Hesulan
  • 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.

Activity

  1. added
    httpIssues and PRs related to the http subsystem.
    on Sep 8, 2016
  2. Trott commented on May 27, 2017

    @Trott
    Member

    @nodejs/http

  3. Trott commented on Mar 4, 2018

    @Trott
    Member

    @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

  4. mcollina commented on Mar 5, 2018

    @mcollina
    SponsorMember

    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.

  5. awwright commented on Jun 15, 2019

    @awwright
    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.

  6. added
    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.
    on Jun 26, 2020
  7. marco-ippolito commented on Nov 21, 2022

    @marco-ippolito
    Member

    this has been fixed by #45508 @ShogunPanda

  8. ShogunPanda commented on Dec 14, 2022

    @ShogunPanda
    Contributor

    @marco-ippolito Thanks, closing this!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.httpIssues and PRs related to the http subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions