GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — getHeader() returns undefined after using writeHead() in a certain circumstance · Issue #10354 · nodejs/node

getHeader() returns undefined after using writeHead() in a certain circumstance #10354

New issue
Copy link
New issue
Copy link
Closed
Closed
getHeader() returns undefined after using writeHead() in a certain circumstance#10354
Copy link
Labels
docIssues and PRs related to Node.js documentation.good first issueIssues that are suitable for first-time contributors.httpIssues and PRs related to the http subsystem.

Description

@AndrewDoesNotCompute
AndrewDoesNotCompute
opened on Dec 20, 2016
Issue body actions
  • Version: 6.9.1
  • Platform: Linux 4.4.0-21-generic logo ideas #37-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: http

In short, calling writeHead() with a ServerResponse object which has no headers already set causes getHeader() to return undefined even if the header was set in the writeHead() function call. I am unfamiliar with the project, so I'm unsure if this is defined behavior or not.

//Case 1: using setHeader, getHeader works as intended
res.setHeader("Location", "index.html");
res.getHeader("Location"); //index.html

//Case 2: using setHeader then writeHead, getHeader works as intended
res.setHeader("Set-Cookie", "hello=world");
res.writeHead(303, {Location: "index.html"});
res.getHeader("Location"); //index.html

//Case 3: using writeHead when res._headers is null, getHeader does not work as intended
res.writeHead(303, {Location: "index.html"});
res.getHeader("Location"); //undefined
Reacted by strarsis and José Cage

Activity

  1. mscdex
    added
    httpIssues and PRs related to the http subsystem.
    on Dec 20, 2016
  2. Fishrock123
    added
    confirmed-bugIssues and PRs for confirmed bugs.
    on Dec 20, 2016
  3. Fishrock123 commented on Dec 20, 2016

    @Fishrock123
    Fishrock123
    on Dec 20, 2016
    Contributor
    More actions

    Oh haha, one of these "bug"s. Related to #1873

    IIRC there are a number of performance reason this is difficult to fix because? Implicit headers... stuff?

    Maybe when it's null we can just assign it to that object...

  4. AndrewDoesNotCompute commented on Dec 21, 2016

    @AndrewDoesNotCompute
    AndrewDoesNotCompute
    on Dec 21, 2016
    Author
    More actions

    Seems when req._headers is null that writeHead() skips calling setHeader() a bunch of times and instead goes straight to writing the final header. Unfortunately, I don't think we could assign the passed object to req._headers because of the way keys are handled in setHeader().

  5. Fishrock123 commented on Dec 22, 2016

    @Fishrock123
    Fishrock123
    on Dec 22, 2016
    Contributor
    More actions

    @DevAndrewGeorge I think (but I have not checked) that it should be possible to invoke the same mechanism as setHeader() uses on each of the keys in the object passed to writeHead() if req._headers is null. It could have a performance impact though. :/

    However it should be mentioned that you are still missing implicit headers and... I think you can still get poorly defined behavior when mixing writeHead() and setHeader(). Until the API is re-written (probably via HTTP/2) headers is just always going to have loopholes and strange behavior. :(

    That being said I'd review a PR if you are willing to make one that covers what I've mentioned in the first paragraph. :D

  6. Trott commented on Jul 15, 2017

    @Trott
    Trott
    on Jul 15, 2017
    Member
    More actions

    @nodejs/http

  7. gireeshpunathil commented on Apr 10, 2018

    @gireeshpunathil
    gireeshpunathil
    on Apr 10, 2018
    Member
    More actions

    A reasonable explanation to this behavior is optimization as opposed to a bug. This block explains the logic. setHeader is for a progressive supply of header fields (iteratively set the headers as the program advances) and writeHead is for a one shot write (all the header values available to write at a single program point) in which case you don't need to reflect on the written data and hence the storing of headers for future retrieval through getHeader is short-circuited.

    The same is also explained in #13825 (comment) and a PR is being worked upon here : #19902

  8. gireeshpunathil commented on May 14, 2018

    @gireeshpunathil
    gireeshpunathil
    on May 14, 2018
    Member
    More actions

    @Fishrock123 - can we have a consensus on this as to:

    • Is it a bug or no bug?
    • Is it a need fix, or a need doc?

    My assertion is that use setHeader for progressive supply of headers for future modification potential (and hence can be retrieved through getHeader) and use writeHead for committing fields that are confirmed to be final (cannot be retrieved through getHeader), and a doc to this effect would be good enough.

  9. bnoordhuis commented on May 14, 2018

    @bnoordhuis
    bnoordhuis
    on May 14, 2018
    Member
    More actions

    Not a bug, an optimization. Could maybe use a note in the docs.

    If we want uniform behavior, we could set this[outHeadersKey] = null in writeHead(). There's no reason to hang on to them after the headers have been sent and it saves some memory.

    Reacted by Gireesh Punathil
  10. gireeshpunathil
    added
    docIssues and PRs related to Node.js documentation.
    good first issueIssues that are suitable for first-time contributors.
    and removed
    confirmed-bugIssues and PRs for confirmed bugs.
    on May 20, 2018
  11. gireeshpunathil commented on May 20, 2018

    @gireeshpunathil
    gireeshpunathil
    on May 20, 2018
    Member
    More actions

    thanks. Added good first issue to attract contributions.

  12. AndrewDoesNotCompute commented on May 21, 2018

    @AndrewDoesNotCompute
    AndrewDoesNotCompute
    on May 21, 2018
    Author
    More actions

    I should actually block away a few hours and actually get this done. It's been literal years.... :P

    Reacted by Gireesh Punathil
  13. gireeshpunathil
    closed this as completedin cbe307don Jun 22, 2018
  14. targos
    added a commit that references this issue on Jun 22, 2018

    doc: explain HTTP writeHead()'s fast path behavior

    8e5104b
  15. sjarmak
    added a commit that references this issue on Jul 27, 2026

    doc: explain HTTP writeHead()'s fast path behavior

    cbe307d
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

    docIssues and PRs related to Node.js documentation.good first issueIssues that are suitable for first-time contributors.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

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.