oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.49k stars 2.79k forks source link

Bun always sets Content-Length to 0 for HEAD response #15355

Open kensnyder opened 1 week ago

kensnyder commented 1 week ago

What version of Bun is running?

1.1.36

What platform is your computer?

Darwin 24.1.0 arm64 arm

What steps can reproduce the bug?

Consider the following code:

const server = Bun.serve({
  fetch() {
    return new Response(null, {
      status: 200,
      headers: {
        'Content-Type': 'image/jpeg',
        'Content-Length': '1000',
      },
    });
  },
});

const resp = await fetch(server.url, { method: 'HEAD' });

// should be 1000, but is actually 0
const contentLength = resp.headers.get('Content-Length');
console.log(`Content-length=${contentLength}`);

server.stop(true);

What is the expected behavior?

To conform to HTTP Server specs, I'd like to be able to respond to HEAD requests with a Content-Length of the size of the actual resource.

What do you see instead?

Bun appears to change the Content-Length to 0 before responding.

Additional information

Also reported in Discord on May 15, 2024 by fry69: https://discord.com/channels/876711213126520882/1240212448552816710/1240212448552816710

Kapsonfire-DE commented 3 days ago

@cirospaciari I'd like to work on this, since I also did the OPTIONS thing. The same part of code is responsible here. But HEAD is different, since RFC says it must not send a body, but headers should be same as if its a GET Request... so i think its reasonable to make an exception in Code for HEAD Request, not changing headers, but ommiting the body.

If you want me working on this, just set me as assignee