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.ServerResponse#strictContentLength = true is broken #57456

Open
zefir-git opened this issue Mar 14, 2025 · 2 comments · May be fixed by #57458
Open

http.ServerResponse#strictContentLength = true is broken #57456

zefir-git opened this issue Mar 14, 2025 · 2 comments · May be fixed by #57458

Comments

@zefir-git
Copy link

Version

v22.13.0

Platform

Linux razer 6.13.6-200.fc41.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Mar  7 21:33:48 UTC 2025 x86_64 GNU/Linux

Subsystem

http

What steps will reproduce the bug?

  1. Use following code
    import http from "node:http";
    http.createServer((req, res) => {
        res.strictContentLength = true;
        res.setHeader("content-length", "5");
        res.end("12345");
    }).listen(3000);
  2. Send any HTTP request to the server on port 3000

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

Always.

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

Should not throw error.

What do you see instead?

Error [ERR_HTTP_CONTENT_LENGTH_MISMATCH]: Response body's content-length of 5 byte(s) does not match the content-length of 5 byte(s) set in header

node:_http_outgoing:1129
    throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(this[kBytesWritten], this._contentLength);
    ^

Error [ERR_HTTP_CONTENT_LENGTH_MISMATCH]: Response body's content-length of 5 byte(s) does not match the content-length of 5 byte(s) set in header
    at ServerResponse.end (node:_http_outgoing:1129:11)
    at Server.<anonymous> (file:///home/zefir/Projects/web-ts/test/test.js:5:9)
    at Server.emit (node:events:524:28)
    at parserOnIncoming (node:_http_server:1153:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17) {
  code: 'ERR_HTTP_CONTENT_LENGTH_MISMATCH'
}

Node.js v22.13.0

Additional information

No response

@marco-ippolito
Copy link
Member

The issue is that you are passing the content-length value as a string, and its not being converted to number.
Opened a PR to fix

@marco-ippolito marco-ippolito linked a pull request Mar 14, 2025 that will close this issue
@zefir-git
Copy link
Author

zefir-git commented Mar 14, 2025

Thanks. When setting headers with Headers (e.g. ServerResponse#setHeaders(Headers)) it’s impossible not to have the value as string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants