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
net/http: FileServer: Content-Length is not set when Content-Encoding was set #66735
Comments
I believe the comment preceding the line sufficiently explains why it isn't set. |
@seankhliao It does not sufficiently cover it. The comment even says within that this is a hacky fix. I am raising a situation where this needs to be set and there should be some kind of way to force that code branch to not be taken. |
I don't find the comment above this @paralin your code is incorrect, Your best bet is to not use |
@Jorropo This is not correct. The file is already compressed with .br in advance. The decompression step is happening on the client side after the Range request is complete. So, I am actually asking for that specific range of the compressed file. |
When (In contrast, So what @paralin is doing here is correct. Unfortunately, as the comment in
This is wrong, and breaks As the comment in |
Is there any way the fs http handler could be configured to always set this header and ignore content-encoding? It's a decent heuristic to check if the response writer is the standard one, but what if it's not? (Like in my case, unfortunately). At the moment I'm working around this by opening the file and getting the size in advance before calling the http ServeHTTP. But this is opening the file twice and seems inherently inefficient / hacky and also bypasses the other logic in the fs http handler |
Exactly, so we agree on what the code do, and that disagree with the HTTP RFC,
And it fails with error code
The difference is that your code truncates the brotli header, while Here I modified the code to skip
This also solve your original issue and sets |
This comment was marked as outdated.
This comment was marked as outdated.
Change https://go.dev/cl/579095 mentions this issue: |
In the case when requesting a Range and have set Content-Encoding I agree that this would not work because the browser will try to decompress the result and get invalid compressed data. My particular case is serving the entire .br file with content encoding set (not using Range). To be clear my specific use case is serving a .wasm file brotli compressed. @Jorropo I got a bit confused with the specifics of this issue when you mentioned the Range requests because I'm using Range requests to access an encoded file, but without Content-Encoding set, decompressing on the client side, in a different context. With respect to this issue it was about setting the Content-Length and Content-Encoding when requesting the entire file. |
This comment was marked as outdated.
This comment was marked as outdated.
The problem is that Content-Encoding nor content-type are set properly for wasm and wasm.br. I am otherwise using the filesystem as designed. Just for this file type it's a special case. |
This comment was marked as outdated.
This comment was marked as outdated.
I'm sorry, but this is not accurate.
The This is in contrast to Your |
The best I can come up with is to have an optional New magic Or I suppose we could go the other way, and have a magic method on the Or add a new I don't like any of the options I can think of. |
It's not pretty, but passing the options at construct time as a new optional parameter with perhaps a NewFileServerWithOptions or so, probably is the least hacky way. Then there are probably other flags that could be set too. Like disabling the index page, adding additional file extension to content type mappings, setting Content-Encoding, etc. |
Whoa mb thx. |
Go version
go version go1.22.2 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
I am trying to set Content-Encoding for .br brotli compressed files with http.FileServer.
This code then skips setting Content-Length because Content-Encoding is set:
go/src/net/http/fs.go
Line 377 in 9f13665
This causes my httptest to fail with an empty Content-Length, as well as other cases where I call ServeHTTP without using the full http client: https://go.dev/play/p/2UvgBSH8FWx
When using http.Get it works correctly, so the client must be filling in the Content-Length: https://go.dev/play/p/xp08V07UrtQ
What did you see happen?
The content-length header is not set when the content-encoding header is set.
What did you expect to see?
I expect the content-length header to be set even if the content-encoding header is set.
The text was updated successfully, but these errors were encountered: