Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set X-Gitea-Debug header once #23361

Merged
merged 9 commits into from Mar 8, 2023
2 changes: 1 addition & 1 deletion modules/context/api.go
Expand Up @@ -244,7 +244,7 @@ func APIContexter() func(http.Handler) http.Handler {
}
}

httpcache.AddCacheControlToHeader(ctx.Resp.Header(), 0, "no-transform")
httpcache.SetCacheControlInHeader(ctx.Resp.Header(), 0, "no-transform")
ctx.Resp.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)

ctx.Data["Context"] = &ctx
Expand Down
4 changes: 2 additions & 2 deletions modules/context/context.go
Expand Up @@ -388,7 +388,7 @@ func (ctx *Context) SetServeHeaders(opts *ServeHeaderOptions) {
if duration == 0 {
duration = 5 * time.Minute
}
httpcache.AddCacheControlToHeader(header, duration)
httpcache.SetCacheControlInHeader(header, duration)

if !opts.LastModified.IsZero() {
header.Set("Last-Modified", opts.LastModified.UTC().Format(http.TimeFormat))
Expand Down Expand Up @@ -753,7 +753,7 @@ func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
}
}

httpcache.AddCacheControlToHeader(ctx.Resp.Header(), 0, "no-transform")
httpcache.SetCacheControlInHeader(ctx.Resp.Header(), 0, "no-transform")
ctx.Resp.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)

ctx.Data["CsrfToken"] = ctx.csrf.GetToken()
Expand Down
12 changes: 6 additions & 6 deletions modules/httpcache/httpcache.go
Expand Up @@ -15,8 +15,8 @@ import (
"code.gitea.io/gitea/modules/setting"
)

// AddCacheControlToHeader adds suitable cache-control headers to response
func AddCacheControlToHeader(h http.Header, maxAge time.Duration, additionalDirectives ...string) {
// SetCacheControlInHeader sets suitable cache-control headers in the response
func SetCacheControlInHeader(h http.Header, maxAge time.Duration, additionalDirectives ...string) {
directives := make([]string, 0, 2+len(additionalDirectives))

// "max-age=0 + must-revalidate" (aka "no-cache") is preferred instead of "no-store"
Expand All @@ -31,7 +31,7 @@ func AddCacheControlToHeader(h http.Header, maxAge time.Duration, additionalDire
directives = append(directives, "max-age=0", "private", "must-revalidate")

// to remind users they are using non-prod setting.
h.Add("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
h.Set("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
yardenshoham marked this conversation as resolved.
Show resolved Hide resolved
}

h.Set("Cache-Control", strings.Join(append(directives, additionalDirectives...), ", "))
Expand All @@ -50,7 +50,7 @@ func HandleTimeCache(req *http.Request, w http.ResponseWriter, fi os.FileInfo) (

// HandleGenericTimeCache handles time-based caching for a HTTP request
func HandleGenericTimeCache(req *http.Request, w http.ResponseWriter, lastModified time.Time) (handled bool) {
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)

ifModifiedSince := req.Header.Get("If-Modified-Since")
if ifModifiedSince != "" {
Expand Down Expand Up @@ -81,7 +81,7 @@ func HandleGenericETagCache(req *http.Request, w http.ResponseWriter, etag strin
return true
}
}
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
return false
}

Expand Down Expand Up @@ -125,6 +125,6 @@ func HandleGenericETagTimeCache(req *http.Request, w http.ResponseWriter, etag s
}
}
}
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
return false
}
2 changes: 1 addition & 1 deletion routers/install/routes.go
Expand Up @@ -64,7 +64,7 @@ func installRecovery(ctx goctx.Context) func(next http.Handler) http.Handler {
"SignedUserName": "",
}

httpcache.AddCacheControlToHeader(w.Header(), 0, "no-transform")
httpcache.SetCacheControlInHeader(w.Header(), 0, "no-transform")
w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)

if !setting.IsProd {
Expand Down
2 changes: 1 addition & 1 deletion routers/web/base.go
Expand Up @@ -159,7 +159,7 @@ func Recovery(ctx goctx.Context) func(next http.Handler) http.Handler {
store["SignedUserName"] = ""
}

httpcache.AddCacheControlToHeader(w.Header(), 0, "no-transform")
httpcache.SetCacheControlInHeader(w.Header(), 0, "no-transform")
w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)

if !setting.IsProd {
Expand Down
2 changes: 1 addition & 1 deletion routers/web/user/avatar.go
Expand Up @@ -17,7 +17,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.AddCacheControlToHeader(ctx.Resp.Header(), 5*time.Minute)
httpcache.SetCacheControlInHeader(ctx.Resp.Header(), 5*time.Minute)
ctx.Redirect(location)
}

Expand Down