Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
qwerzl opened this issue Jan 21, 2025 · 0 comments · May be fixed by #56681
Open

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

qwerzl opened this issue Jan 21, 2025 · 0 comments · May be fixed by #56681
Labels
http Issues or PRs related to the http subsystem.

Comments

@qwerzl
Copy link

qwerzl commented Jan 21, 2025

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

qwerzl added a commit to qwerzl/node that referenced this issue Jan 21, 2025
In the current http library, all responses without body will not return
the Content-Length header.

Fixes: nodejs#56680
@qwerzl qwerzl linked a pull request Jan 21, 2025 that will close this issue
@VoltrexKeyva VoltrexKeyva added the http Issues or PRs related to the http subsystem. label Jan 21, 2025
qwerzl added a commit to qwerzl/node that referenced this issue Jan 22, 2025
In the current http library, all responses without body will not return
the Content-Length header.

Fixes: nodejs#56680
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants