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: HEAD request consumes response body #56697

Open
LiviaMedeiros opened this issue Jan 22, 2025 · 1 comment
Open

http: HEAD request consumes response body #56697

LiviaMedeiros opened this issue Jan 22, 2025 · 1 comment
Labels
http Issues or PRs related to the http subsystem.

Comments

@LiviaMedeiros
Copy link
Contributor

Version

v22.10.0

Platform

Linux tumba 6.12.6-gentoo-yuran #1 SMP Sat Dec 21 16:28:04 +08 2024 x86_64 Intel(R) Core(TM)2 Quad CPU Q8200 @ 2.33GHz GenuineIntel GNU/Linux

Subsystem

http

What steps will reproduce the bug?

// srv.mjs
import { createServer } from 'node:http';
import { Readable } from 'node:stream';
const stream = Readable.from(['Hello ', 'World', '\n']);
stream.on('end', () => console.log('Body consumed'));
const server = createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  stream.pipe(res);
});
server.listen(9999, '127.0.0.9');
$ node srv.mjs
$ curl -I http://127.0.0.9:9999/
$ curl -v http://127.0.0.9:9999/

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

Always.

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

No console output on server side after:

$ curl -I http://127.0.0.9:9999
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Wed, 22 Jan 2025 12:34:56 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Body consumed output on server side after:

$ curl http://127.0.0.9:9999
Hello World

What do you see instead?

Body consumed on server terminal after sending HEAD request.
Nothing on client terminal after sending subsequent GET request.

Additional information

With a patch from #56681, the body is correctly recognized as stream, resulting in Transfer-Encoding: chunked header in first response. However, the body is still slurped, and subsequent requests have zero length.

@LiviaMedeiros LiviaMedeiros added the http Issues or PRs related to the http subsystem. label Jan 22, 2025
@xt-riot
Copy link

xt-riot commented Jan 22, 2025

You are declaring the stream at the script start. It's only declared once, and once consumed it doesn't restart. According to node:

The 'end' event is emitted when there is no more data to be consumed from the stream.

Therefore, the subsequent request has no more data to read/pipe.

import { createServer } from 'node:http';
import { Readable } from 'node:stream';

const server = createServer((req, res) => {
  const stream = Readable.from(['Hello ', 'World', '\n']);
  stream.on('end', () => console.log('Body consumed'));
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  stream.pipe(res);
});
server.listen(9999, '127.0.0.9');

would fix that

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

No branches or pull requests

2 participants