fileserver: set Content-Length for precompressed files (#7251)

* set Content-Length for precompressed files

* Update modules/caddyhttp/fileserver/staticfiles.go

Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

---------

Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
This commit is contained in:
WeidiDeng 2025-09-11 12:48:03 +08:00 committed by GitHub
parent 012b4b3d40
commit b462615439
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -458,12 +458,12 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
defer file.Close()
respHeader.Set("Content-Encoding", ae)
// stdlib won't set Content-Length if Content-Encoding is set.
// set Range header if it's not present will force Content-Length to be set
// stdlib won't set Content-Length for non-range requests if Content-Encoding is set.
// see: https://github.com/caddyserver/caddy/issues/7040
// Setting the Range header manually will result in 206 Partial Content.
// see: https://github.com/caddyserver/caddy/issues/7250
if r.Header.Get("Range") == "" {
r.Header.Set("Range", "bytes=0-")
// remove this header, because it is not part of the request
defer r.Header.Del("Range")
respHeader.Set("Content-Length", strconv.FormatInt(compressedInfo.Size(), 10))
}
// try to get the etag from pre computed files if an etag suffix list was provided