aboutsummaryrefslogtreecommitdiffstats
path: root/routers/web
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2025-03-13 07:04:50 +0800
committerGitHub <noreply@github.com>2025-03-13 07:04:50 +0800
commit3996518ed432218d7f2fd62d451ee0e29953d853 (patch)
tree9a3ef66b10e40eeee72c7a63b57f6153eee8a0ac /routers/web
parent91610a987e4c805a9305d4ee951963f80dbeb9ee (diff)
downloadgitea-3996518ed432218d7f2fd62d451ee0e29953d853.tar.gz
gitea-3996518ed432218d7f2fd62d451ee0e29953d853.zip
Refactor cache-control (#33861)
And fix #21391
Diffstat (limited to 'routers/web')
-rw-r--r--routers/web/base.go16
-rw-r--r--routers/web/misc/misc.go2
-rw-r--r--routers/web/repo/download.go8
-rw-r--r--routers/web/repo/wiki.go2
-rw-r--r--routers/web/user/avatar.go2
-rw-r--r--routers/web/web.go4
6 files changed, 18 insertions, 16 deletions
diff --git a/routers/web/base.go b/routers/web/base.go
index abe11593f7..a284dd0288 100644
--- a/routers/web/base.go
+++ b/routers/web/base.go
@@ -19,12 +19,12 @@ import (
"code.gitea.io/gitea/modules/web/routing"
)
-func storageHandler(storageSetting *setting.Storage, prefix string, objStore storage.ObjectStorage) http.HandlerFunc {
+func avatarStorageHandler(storageSetting *setting.Storage, prefix string, objStore storage.ObjectStorage) http.HandlerFunc {
prefix = strings.Trim(prefix, "/")
- funcInfo := routing.GetFuncInfo(storageHandler, prefix)
+ funcInfo := routing.GetFuncInfo(avatarStorageHandler, prefix)
if storageSetting.ServeDirect() {
- return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+ return func(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" && req.Method != "HEAD" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
@@ -52,10 +52,10 @@ func storageHandler(storageSetting *setting.Storage, prefix string, objStore sto
}
http.Redirect(w, req, u.String(), http.StatusTemporaryRedirect)
- })
+ }
}
- return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+ return func(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" && req.Method != "HEAD" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
@@ -93,6 +93,8 @@ func storageHandler(storageSetting *setting.Storage, prefix string, objStore sto
return
}
defer fr.Close()
- httpcache.ServeContentWithCacheControl(w, req, path.Base(rPath), fi.ModTime(), fr)
- })
+
+ httpcache.SetCacheControlInHeader(w.Header(), httpcache.CacheControlForPublicStatic())
+ http.ServeContent(w, req, path.Base(rPath), fi.ModTime(), fr)
+ }
}
diff --git a/routers/web/misc/misc.go b/routers/web/misc/misc.go
index caaca7f521..d42afafe9e 100644
--- a/routers/web/misc/misc.go
+++ b/routers/web/misc/misc.go
@@ -38,7 +38,7 @@ func RobotsTxt(w http.ResponseWriter, req *http.Request) {
if ok, _ := util.IsExist(robotsTxt); !ok {
robotsTxt = util.FilePathJoinAbs(setting.CustomPath, "robots.txt") // the legacy "robots.txt"
}
- httpcache.SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
+ httpcache.SetCacheControlInHeader(w.Header(), httpcache.CacheControlForPublicStatic())
http.ServeFile(w, req, robotsTxt)
}
diff --git a/routers/web/repo/download.go b/routers/web/repo/download.go
index 060381e9d6..020cebf196 100644
--- a/routers/web/repo/download.go
+++ b/routers/web/repo/download.go
@@ -46,7 +46,7 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob, lastModified *time.Tim
log.Error("ServeBlobOrLFS: Close: %v", err)
}
closed = true
- return common.ServeBlob(ctx.Base, ctx.Repo.TreePath, blob, lastModified)
+ return common.ServeBlob(ctx.Base, ctx.Repo.Repository, ctx.Repo.TreePath, blob, lastModified)
}
if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+pointer.Oid+`"`) {
return nil
@@ -78,7 +78,7 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob, lastModified *time.Tim
}
closed = true
- return common.ServeBlob(ctx.Base, ctx.Repo.TreePath, blob, lastModified)
+ return common.ServeBlob(ctx.Base, ctx.Repo.Repository, ctx.Repo.TreePath, blob, lastModified)
}
func getBlobForEntry(ctx *context.Context) (*git.Blob, *time.Time) {
@@ -114,7 +114,7 @@ func SingleDownload(ctx *context.Context) {
return
}
- if err := common.ServeBlob(ctx.Base, ctx.Repo.TreePath, blob, lastModified); err != nil {
+ if err := common.ServeBlob(ctx.Base, ctx.Repo.Repository, ctx.Repo.TreePath, blob, lastModified); err != nil {
ctx.ServerError("ServeBlob", err)
}
}
@@ -142,7 +142,7 @@ func DownloadByID(ctx *context.Context) {
}
return
}
- if err = common.ServeBlob(ctx.Base, ctx.Repo.TreePath, blob, nil); err != nil {
+ if err = common.ServeBlob(ctx.Base, ctx.Repo.Repository, ctx.Repo.TreePath, blob, nil); err != nil {
ctx.ServerError("ServeBlob", err)
}
}
diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go
index 98c84b6993..0f8e1223c6 100644
--- a/routers/web/repo/wiki.go
+++ b/routers/web/repo/wiki.go
@@ -740,7 +740,7 @@ func WikiRaw(ctx *context.Context) {
}
if entry != nil {
- if err = common.ServeBlob(ctx.Base, ctx.Repo.TreePath, entry.Blob(), nil); err != nil {
+ if err = common.ServeBlob(ctx.Base, ctx.Repo.Repository, ctx.Repo.TreePath, entry.Blob(), nil); err != nil {
ctx.ServerError("ServeBlob", err)
}
return
diff --git a/routers/web/user/avatar.go b/routers/web/user/avatar.go
index 81c00b3bd4..6d3179bc48 100644
--- a/routers/web/user/avatar.go
+++ b/routers/web/user/avatar.go
@@ -16,7 +16,7 @@ func cacheableRedirect(ctx *context.Context, location string) {
// here we should not use `setting.StaticCacheTime`, it is pretty long (default: 6 hours)
// we must make sure the redirection cache time is short enough, otherwise a user won't see the updated avatar in 6 hours
// it's OK to make the cache time short, it is only a redirection, and doesn't cost much to make a new request
- httpcache.SetCacheControlInHeader(ctx.Resp.Header(), 5*time.Minute)
+ httpcache.SetCacheControlInHeader(ctx.Resp.Header(), &httpcache.CacheControlOptions{MaxAge: 5 * time.Minute})
ctx.Redirect(location)
}
diff --git a/routers/web/web.go b/routers/web/web.go
index 01dc8cf697..3144cb26b2 100644
--- a/routers/web/web.go
+++ b/routers/web/web.go
@@ -233,8 +233,8 @@ func Routes() *web.Router {
routes.Head("/", misc.DummyOK) // for health check - doesn't need to be passed through gzip handler
routes.Methods("GET, HEAD, OPTIONS", "/assets/*", optionsCorsHandler(), public.FileHandlerFunc())
- routes.Methods("GET, HEAD", "/avatars/*", storageHandler(setting.Avatar.Storage, "avatars", storage.Avatars))
- routes.Methods("GET, HEAD", "/repo-avatars/*", storageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars))
+ routes.Methods("GET, HEAD", "/avatars/*", avatarStorageHandler(setting.Avatar.Storage, "avatars", storage.Avatars))
+ routes.Methods("GET, HEAD", "/repo-avatars/*", avatarStorageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars))
routes.Methods("GET, HEAD", "/apple-touch-icon.png", misc.StaticRedirect("/assets/img/apple-touch-icon.png"))
routes.Methods("GET, HEAD", "/apple-touch-icon-precomposed.png", misc.StaticRedirect("/assets/img/apple-touch-icon.png"))
routes.Methods("GET, HEAD", "/favicon.ico", misc.StaticRedirect("/assets/img/favicon.png"))