Skip to content

http server respond with inappropriate default headers for HEAD method #28438

Description

@abbshr
  • Version: >= 10.x
  • Platform: any
  • Subsystem: http

To implement a HTTP keep alive mechanism, I use the http.Agent({ keepAlive: true }) and http.request({ agent, method, host, port }).

Everything is ok when the method is GET or TRACE and others, the agent reuse the TCP connection as I expect.

But when changing the method to HEAD, the agent close the connection when server responded.

From the response header, the only different is that: for HEAD method, there isn't a Content-Length: 0 or Transfer-Encoding: chunked in the default header (but GET has by default), so the HTTP parser think this connection should not be kept alive.

But from the RFC, we can infer that if keep-alive was enabled, either Content-Length: 0 or Transfer-Encoding: chunked should be setted in response header, I think this should be a default behavior.

# server
const http = require("http")

const server = http.createServer((req, res) => {
  // res.setHeader("Content-Length", 0)
  // res.setHeader("Transfer-Encoding", "chunked")
  res.end()
})

server.keepAliveTimeout = 0
server.listen(2333, "x.x.x.x")
# client
const http = require("http")

const agent = new http.Agent({
  keepAlive: true,
})

request = () => {
  const req = http.request({
    agent,
    host: "x.x.x.x",
    port: 2333,
    method: 'HEAD',
  })

  req.end()
}

request()

Activity

  1. changed the title [-]http server respond with inappropriate headers for `HEAD` method[/-] [+]http server respond with inappropriate default headers for `HEAD` method[/+] on Jun 26, 2019
  2. jimmiehansson commented on Jun 29, 2019

    @jimmiehansson

    Hey @abbshr
    In contrary to GET this is the intended behavior for the HEAD requests as its simply a meta request of GET, excluding the body contents that you would typically have in GET, this seems like an expected behavior from the http server as there is no buffer or chunk to expect.
    RFC-accordingly, depending on the request payload this may also lead to early termination.
    Did i understand your problem correctly?
    Also see the semantics of the HTTP payload https://tools.ietf.org/html/rfc7231#section-3.3

  3. added
    httpIssues and PRs related to the http subsystem.
    on Jul 28, 2019
  4. added
    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.
    on Jun 26, 2020
  5. wa-Nadoo commented on Jun 15, 2021

    @wa-Nadoo

    It seems this issue should be reopened because the bugfix commit was reverted.

  6. reopened this on Jun 15, 2021
  7. nawed2611 commented on May 6, 2022

    @nawed2611

    Hey! Can I work on this?

  8. github-actions commented on Jun 27, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  9. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jun 27, 2026
  10. github-actions commented on Jul 28, 2026

    @github-actions
    Contributor

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

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.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions