lpar / gzipped

Replacement for golang http.FileServer which supports precompressed static assets.
BSD 3-Clause "New" or "Revised" License
94 stars 15 forks source link

Should we sniff content? #18

Open lpar opened 4 years ago

lpar commented 4 years ago

Turning part of #16 into a standalone item.

In case the mime type cannot be determined from the extension by the mime.TypeByExtension method, ServeContent will try to infer from the actual file content: https://golang.org/src/net/http/fs.go?s=4932:5036#L198 so it will return a type like "application/x-gzip", whereas http.FileServer would have return "text/plain" for a YAML file, for example. This can lead to bugs, for example when trying to use the Swagger UI. => workaround: register such non-standard types (there's actually no official mime type for YAML): mime.AddExtensionType(".yaml", "application/x-yaml")

I'm not a big fan of content sniffing, as it can lead to really obscure bugs. However, it wouldn't be all that hard to read a chunk from the uncompressed version of the file, and call http.DetectContentType to pick a MIME type.

stokito commented 1 year ago

I just had a problem that mime type wasn't detected by a file extension but was sniffed instead. This is a well known problem for the golang http package and you can't do anything about it. I solved the problem myself by registering a mime type of the file extension:

_ = mime.AddExtensionType(".ndjson", "application/x-ndjson")