1
1
mirror of https://github.com/go-gitea/gitea synced 2025-08-20 00:18:27 +00:00

Fix mime-type detection for HTTP server (#18371)

This commit is contained in:
wxiaoguang
2022-01-23 21:17:20 +08:00
committed by GitHub
parent d644289fcb
commit 160de9fbda
4 changed files with 61 additions and 20 deletions

View File

@@ -95,6 +95,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))
@@ -125,6 +134,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
}