diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-05-13 22:04:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-13 16:04:57 +0200 |
commit | a94a8d0ab1f6c9d63ae607dfec866c47ae3e4b5c (patch) | |
tree | 84c1e5e4f3423ed952b573ab1403844de2951cb7 /routers/web | |
parent | f74501609246646a3ab62b37a1e7469d6fea63b7 (diff) | |
download | gitea-a94a8d0ab1f6c9d63ae607dfec866c47ae3e4b5c.tar.gz gitea-a94a8d0ab1f6c9d63ae607dfec866c47ae3e4b5c.zip |
Use standard HTTP library to serve files (#24693)
`http.ServeFile/ServeContent` handles `If-xxx`, `Content-Length`,
`Range` and `Etag` correctly
After this PR, storage files (eg: avatar) could be responded with
correct Content-Length.
Diffstat (limited to 'routers/web')
-rw-r--r-- | routers/web/base.go | 16 | ||||
-rw-r--r-- | routers/web/misc/misc.go | 9 |
2 files changed, 8 insertions, 17 deletions
diff --git a/routers/web/base.go b/routers/web/base.go index b9b5958389..1f6c4fbfc5 100644 --- a/routers/web/base.go +++ b/routers/web/base.go @@ -6,7 +6,6 @@ package web import ( "errors" "fmt" - "io" "net/http" "os" "path" @@ -76,12 +75,6 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor } fi, err := objStore.Stat(rPath) - if err == nil && httpcache.HandleTimeCache(req, w, fi) { - return - } - - // If we have matched and access to release or issue - fr, err := objStore.Open(rPath) if err != nil { if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) { log.Warn("Unable to find %s %s", prefix, rPath) @@ -92,14 +85,15 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor http.Error(w, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath), http.StatusInternalServerError) return } - defer fr.Close() - _, err = io.Copy(w, fr) + fr, err := objStore.Open(rPath) if err != nil { - log.Error("Error whilst rendering %s %s. Error: %v", prefix, rPath, err) - http.Error(w, fmt.Sprintf("Error whilst rendering %s %s", prefix, rPath), http.StatusInternalServerError) + log.Error("Error whilst opening %s %s. Error: %v", prefix, rPath, err) + http.Error(w, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath), http.StatusInternalServerError) return } + defer fr.Close() + httpcache.ServeContentWithCacheControl(w, req, path.Base(rPath), fi.ModTime(), fr) }) } } diff --git a/routers/web/misc/misc.go b/routers/web/misc/misc.go index 582179990a..6ed3b5c3ad 100644 --- a/routers/web/misc/misc.go +++ b/routers/web/misc/misc.go @@ -5,13 +5,13 @@ package misc import ( "net/http" - "os" "path" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" ) func SSHInfo(rw http.ResponseWriter, req *http.Request) { @@ -34,11 +34,8 @@ func DummyOK(w http.ResponseWriter, req *http.Request) { } func RobotsTxt(w http.ResponseWriter, req *http.Request) { - filePath := path.Join(setting.CustomPath, "robots.txt") - fi, err := os.Stat(filePath) - if err == nil && httpcache.HandleTimeCache(req, w, fi) { - return - } + filePath := util.FilePathJoinAbs(setting.CustomPath, "robots.txt") + httpcache.SetCacheControlInHeader(w.Header(), setting.StaticCacheTime) http.ServeFile(w, req, filePath) } |