Skip to content

HTTP/1.0 responses missing Content-Length #56277

Open
@jamesdiacono

Description

Version

v22.10.0

Platform

Darwin ... 24.2.0 Darwin Kernel Version 24.2.0: ...; root:xnu-11215.61.5~2/RELEASE_ARM64_T6000 arm64 arm Darwin

Subsystem

http

What steps will reproduce the bug?

Start a simple HTTP server.

import http from "node:http";
http.createServer(function (req, res) {
    res.end("hello");
}).listen(3000);

Send it an HTTP/1.0 request using cURL.

$ curl --http1.0 -XGET -I 127.0.0.1:3000
HTTP/1.1 200 OK
Date: Tue, 17 Dec 2024 03:26:02 GMT
Connection: close

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

Consistently reproducible.

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

I would expect the response to include a Content-Length header.

What do you see instead?

The response does not include a Content-Length header.

Additional information

If we instead send an HTTP/1.1 request (either with or without Keep-Alive), the Content-Length header is included.

$ curl --http1.1 -XGET -I 127.0.0.1:3000
HTTP/1.1 200 OK
Date: Tue, 17 Dec 2024 03:31:41 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 5

$ curl --http1.1 -H"Connection: close" -XGET -I 127.0.0.1:3000
HTTP/1.1 200 OK
Date: Tue, 17 Dec 2024 03:32:35 GMT
Connection: close
Content-Length: 5

This seems strange. I would expect to see a Content-Length header regardless of whether the request was HTTP/1.0 or HTTP/1.1.

Attaching a debugger, the HTTP/1.0 request triggers the former clause and the HTTP/1.1 request triggers the latter clause in the following HTTP server code.

node/lib/_http_outgoing.js

Lines 553 to 559 in 2cd385e

} else if (!this.useChunkedEncodingByDefault) {
this._last = true;
} else if (!state.trailer &&
!this._removedContLen &&
typeof this._contentLength === 'number') {
header += 'Content-Length: ' + this._contentLength + '\r\n';
} else if (!this._removedTE) {

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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