diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2022-01-23 20:19:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-23 20:19:49 +0800 |
commit | 87141b908d4a03ce27af3ce042dc417da925b84f (patch) | |
tree | e4a5bd11db5ef425fd025971cf21bfc3a587968f /modules/public/public.go | |
parent | 35fdefc1ff253522f101ffb1337437b59676c302 (diff) | |
download | gitea-87141b908d4a03ce27af3ce042dc417da925b84f.tar.gz gitea-87141b908d4a03ce27af3ce042dc417da925b84f.zip |
Fix mime-type detection for HTTP server (#18370)
Bypass the unstable behavior of Golang's mime.TypeByExtension
Diffstat (limited to 'modules/public/public.go')
-rw-r--r-- | modules/public/public.go | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/modules/public/public.go b/modules/public/public.go index 91ecf42a3c..7804e945e7 100644 --- a/modules/public/public.go +++ b/modules/public/public.go @@ -92,6 +92,15 @@ func parseAcceptEncoding(val string) map[string]bool { return types } +// setWellKnownContentType will set the Content-Type if the file is a well-known type. +// See the comments of detectWellKnownMimeType +func setWellKnownContentType(w http.ResponseWriter, file string) { + mimeType := detectWellKnownMimeType(filepath.Ext(file)) + if mimeType != "" { + w.Header().Set("Content-Type", mimeType) + } +} + func (opts *Options) handle(w http.ResponseWriter, req *http.Request, fs http.FileSystem, file string) bool { // use clean to keep the file is a valid path with no . or .. f, err := fs.Open(path.Clean(file)) @@ -122,6 +131,8 @@ func (opts *Options) handle(w http.ResponseWriter, req *http.Request, fs http.Fi return true } + setWellKnownContentType(w, file) + serveContent(w, req, fi, fi.ModTime(), f) return true } |