net/http: serveContent() via FS() fails when the fs.File does not implement io.Seeker and content-type cannot be determined from filename #44553
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Using
http.FS
with anfs.FS
that does not returnio.Seeker
files will fail to serve if the content-type cannot be guessed from the filename.Here is a testcase
bug_test.go
:(play.golang.org is currently running Go 1.14, but this testcase relies on
io/fs
from 1.16)What did you expect to see?
When the content-type cannot be guessed from the filename, and when the content itself is not seekable, I would suggest that returning
application/octet-stream
would be a reasonable default.What did you see instead?
HTTP response code 500 with body
seeker can't seek
(coming from https://github.com/golang/go/blob/master/src/net/http/fs.go#L243 ).Suggested fix
The following patch to
src/net/http/fs.go
works around the case where*http.ioFile
is being used to wrap anfs.File
(viahttp.FS
) and add aSeek
method, even when a givenfs.File
doesn't implement it (see https://github.com/golang/go/blob/master/src/net/http/fs.go#L774-L780):The text was updated successfully, but these errors were encountered: