oven-sh / bun

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

Content-Length Header gets overwritten #10507

Open Calemy opened 5 months ago

Calemy commented 5 months ago

What version of Bun is running?

1.1.4+fbe2fe0c3

What platform is your computer?

Linux 5.15.0-97-generic x86_64 x86_64

What steps can reproduce the bug?

import { Elysia } from "elysia";
import fetch from "node-fetch"

const app = new Elysia()
app.get("/", async ({ set }) => {
    const download = await fetch(`https://catboy.best/d/1`)
    const size = download.headers.get("Content-Length")
    console.log(size) //Expected to be 2797150

    return new Response(download.body, {
        headers: {
            "Content-Length": size,
            "Content-Type": "application/octet-stream"
        }
    })
})
app.listen({ hostname: "0.0.0.0", port: 2449 })

console.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`);

What is the expected behavior?

by setting the content-length inside the header it should use that and being able to show up on the end-client.

What do you see instead?

no content-length set at all.

Additional information

No response

Calemy commented 5 months ago

Also tested with following code:

const server = Bun.serve({
    port: 2450,
    async fetch(req){
        const download = await fetch(`https://catboy.best/d/1`)
        return new Response(download.body, {
            headers: {
                "Content-Type": "application/x-osu-beatmap-archive",
                "Content-Disposition": "attachment; filename=1.osz",
                "Content-Length": "101"
            }
        })
    }
})
Calemy commented 5 months ago

@Electroid maybe you can give me some insight?

SEAPUNK commented 4 months ago

I'm running into the same issue. I think it's happening here:

https://github.com/oven-sh/bun/blob/942061a40a862e942b93829e161b40b3f83a72be/src/http.zig#L2159-L2165

I don't think Bun should overwrite the content-length header (or any header, really) if it's already set.

If you can build bun yourself, this patch should work, I think: https://github.com/SEAPUNK/bun/commit/53528ff64d1f8aa1a7f6dbfa35d3248aded42c9c#