gzipped icon indicating copy to clipboard operation
gzipped copied to clipboard

Should we sniff content?

Open lpar opened this issue 4 years ago • 1 comments

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.

lpar avatar Jul 11 '20 19:07 lpar

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")

stokito avatar Feb 22 '23 17:02 stokito