@@ -29,12 +29,6 @@ const sniffLen = 512
2929// all of the byte-range-spec values is greater than the content size.
3030var errNoOverlap = errors .New ("invalid range: failed to overlap" )
3131
32- func hasFile (fs http.FileSystem , name string ) bool {
33- f , err := fs .Open (name )
34- f .Close ()
35- return err == nil
36- }
37-
3832// if name is empty, filename is unknown. (used for mime type, before sniffing)
3933// if modtime.IsZero(), modtime is unknown.
4034// content must be seeked to the beginning of the file.
@@ -78,6 +72,7 @@ func serveContent(w http.ResponseWriter, r *http.Request, name string, modtime t
7872 if ! hasContentEncoding && acceptEncoding (r , "br" ) {
7973 f , err := fs .Open (name + ".br" )
8074 if err == nil {
75+ defer f .Close ()
8176 // assume stat would work, we just opened the file
8277 d , _ := f .Stat ()
8378 content , size = f , d .Size ()
@@ -88,6 +83,7 @@ func serveContent(w http.ResponseWriter, r *http.Request, name string, modtime t
8883 if ! hasContentEncoding && acceptEncoding (r , "gzip" ) {
8984 f , err := fs .Open (name + ".gz" )
9085 if err == nil {
86+ defer f .Close ()
9187 // assume stat would work, we just opened the file
9288 d , _ := f .Stat ()
9389 content , size = f , d .Size ()
@@ -167,13 +163,16 @@ func serveContent(w http.ResponseWriter, r *http.Request, name string, modtime t
167163 }
168164
169165 w .Header ().Set ("Accept-Ranges" , "bytes" )
170- if w .Header ().Get ("Content-Encoding" ) == "" {
166+ // Only set the length if the content-encoding is set inside this function
167+ if hasContentEncoding || w .Header ().Get ("Content-Encoding" ) == "" {
171168 w .Header ().Set ("Content-Length" , strconv .FormatInt (sendSize , 10 ))
172169 }
173170 }
174171
175172 w .WriteHeader (code )
176173
174+ fmt .Println (name , "code=" , code , "size=" , sendSize , "ct=" , w .Header ().Get ("Content-Type" ), "ce=" , w .Header ().Get ("Content-Encoding" ))
175+
177176 if r .Method != "HEAD" {
178177 io .CopyN (w , sendContent , sendSize )
179178 }
@@ -518,7 +517,7 @@ func serveFile(w http.ResponseWriter, r *http.Request, hfs http.FileSystem, name
518517 }
519518
520519 // serveContent will check modification time
521- serveContent (w , r , d . Name () , d .ModTime (), d .Size (), f , hfs )
520+ serveContent (w , r , name , d .ModTime (), d .Size (), f , hfs )
522521}
523522
524523// toHTTPError returns a non-specific HTTP error message and status code
0 commit comments