Skip to content

Commit af0468e

Browse files
yardenshohamtechknowlogickjolheiser
authored
Set X-Gitea-Debug header once (#23361)
Instead of adding it # Before On the raw commit page: ![image](https://user-images.githubusercontent.com/20454870/223470744-cdf11898-e023-4198-8c8b-c294e5d78b73.png) # After ![image](https://user-images.githubusercontent.com/20454870/223470596-af898d66-bd5b-4ddb-b220-ceb1f149bfec.png) Fixes #23308 --------- Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: John Olheiser <[email protected]>
1 parent 1960ad5 commit af0468e

File tree

6 files changed

+12
-12
lines changed

6 files changed

+12
-12
lines changed

modules/context/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func APIContexter() func(http.Handler) http.Handler {
244244
}
245245
}
246246

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

250250
ctx.Data["Context"] = &ctx

modules/context/context.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func (ctx *Context) SetServeHeaders(opts *ServeHeaderOptions) {
388388
if duration == 0 {
389389
duration = 5 * time.Minute
390390
}
391-
httpcache.AddCacheControlToHeader(header, duration)
391+
httpcache.SetCacheControlInHeader(header, duration)
392392

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

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

759759
ctx.Data["CsrfToken"] = ctx.csrf.GetToken()

modules/httpcache/httpcache.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"code.gitea.io/gitea/modules/setting"
1616
)
1717

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

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

3333
// to remind users they are using non-prod setting.
34-
h.Add("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
34+
h.Set("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
3535
}
3636

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

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

5555
ifModifiedSince := req.Header.Get("If-Modified-Since")
5656
if ifModifiedSince != "" {
@@ -81,7 +81,7 @@ func HandleGenericETagCache(req *http.Request, w http.ResponseWriter, etag strin
8181
return true
8282
}
8383
}
84-
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
84+
SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
8585
return false
8686
}
8787

@@ -125,6 +125,6 @@ func HandleGenericETagTimeCache(req *http.Request, w http.ResponseWriter, etag s
125125
}
126126
}
127127
}
128-
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
128+
SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
129129
return false
130130
}

routers/install/routes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func installRecovery(ctx goctx.Context) func(next http.Handler) http.Handler {
6464
"SignedUserName": "",
6565
}
6666

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

7070
if !setting.IsProd {

routers/web/base.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func Recovery(ctx goctx.Context) func(next http.Handler) http.Handler {
159159
store["SignedUserName"] = ""
160160
}
161161

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

165165
if !setting.IsProd {

routers/web/user/avatar.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func cacheableRedirect(ctx *context.Context, location string) {
1717
// here we should not use `setting.StaticCacheTime`, it is pretty long (default: 6 hours)
1818
// we must make sure the redirection cache time is short enough, otherwise a user won't see the updated avatar in 6 hours
1919
// it's OK to make the cache time short, it is only a redirection, and doesn't cost much to make a new request
20-
httpcache.AddCacheControlToHeader(ctx.Resp.Header(), 5*time.Minute)
20+
httpcache.SetCacheControlInHeader(ctx.Resp.Header(), 5*time.Minute)
2121
ctx.Redirect(location)
2222
}
2323

0 commit comments

Comments
 (0)