nodejs/node

http server respond with inappropriate default headers for `HEAD` method

Open

#28.438 aberto em 26 de jun. de 2019

Ver no GitHub
 (4 comments) (0 reactions) (0 assignees)JavaScript (35.535 forks)batch import
help wantedhttpstale

Métricas do repositório

Stars
 (117.218 stars)
Métricas de merge de PR
 (Mesclagem média 18d 17h) (219 fundiu PRs em 30d)

Description

  • 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()

Guia do colaborador