Skip to content

[http]: Missing Content-Length Header when Responding to HEAD Requests #56680

Open
@qwerzl

Description

@qwerzl

Version

v23.5.0

Platform

Darwin MacbookPro-2.local 24.0.0 Darwin Kernel Version 24.0.0: Tue Sep 24 23:39:07 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T6000 arm64 arm Darwin

Subsystem

http

What steps will reproduce the bug?

Just start the server and curl the results:

import { createServer } from 'node:http';
const hostname = '127.0.0.1';
const port = 3000;
const server = createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

curl -v http://127.0.0.1:3000/ to get the response header through GET;
curl -I http://127.0.0.1:3000/ to get the response header through HEAD.

How often does it reproduce? Is there a required condition?

Always. No required condition.

What is the expected behavior? Why is that the expected behavior?

According to MDN:

The HEAD HTTP method requests the metadata of a resource in the form of headers that the server would have sent if the GET method was used instead. This method can be used in cases where a URL might produce a large download, for example, a HEAD request can read the Content-Length header to check the file size before downloading the file with a GET.

HEAD requests are a bit different than other request methods without body because it is essentially GETs with body removed. It should be allowed to return all the headers that a GET request returns.

What do you see instead?

However, in the current http library, all responses without body will not return the Content-Length header:

if (!this._hasBody) {

Curl results:

  • -I (HEAD Requests)

    ~> curl -I http://127.0.0.1:3000/
    HTTP/1.1 200 OK
    Content-Type: text/plain
    Date: [redacted]
    
  • -v (GET Requests)

    ~> curl -v http://127.0.0.1:3000/
    *   Trying 127.0.0.1:3000...
    * Connected to 127.0.0.1 (127.0.0.1) port 3000
    > GET / HTTP/1.1
    > Host: 127.0.0.1:3000
    > User-Agent: curl/8.7.1
    > Accept: */*
    >
    * Request completely sent off
    < HTTP/1.1 200 OK
    < Content-Type: text/plain
    < Date: [redacted]
    < Content-Length: 11
    <
    * Connection #0 to host 127.0.0.1 left intact
    Hello World
    

Additional information

No response

Activity

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

    httpIssues or 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