Skip to content

Commit 0027ca8

Browse files
lunnydelvhwxiaoguang
authored
Use maintained gziphandler (#30592) (#30637)
Replace #27894 Backport #30592 Co-authored-by: delvh <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 682a076 commit 0027ca8

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed

assets/go-licenses.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ require (
1616
gitea.com/lunny/levelqueue v0.4.2-0.20230414023320-3c0159fe0fe4
1717
github.com/42wim/sshsig v0.0.0-20211121163825-841cf5bbc121
1818
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358
19-
github.com/NYTimes/gziphandler v1.1.1
2019
github.com/PuerkitoBio/goquery v1.9.1
2120
github.com/alecthomas/chroma/v2 v2.13.0
2221
github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb
@@ -68,7 +67,7 @@ require (
6867
github.com/json-iterator/go v1.1.12
6968
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
7069
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
71-
github.com/klauspost/compress v1.17.7
70+
github.com/klauspost/compress v1.17.8
7271
github.com/klauspost/cpuid/v2 v2.2.7
7372
github.com/lib/pq v1.10.9
7473
github.com/markbates/goth v1.79.0

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBa
6161
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
6262
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
6363
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
64-
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
65-
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
6664
github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78=
6765
github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
6866
github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI=
@@ -493,8 +491,8 @@ github.com/klauspost/compress v1.11.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYs
493491
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
494492
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
495493
github.com/klauspost/compress v1.15.6/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
496-
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
497-
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
494+
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
495+
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
498496
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
499497
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
500498
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=

modules/web/handler.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ func hasResponseBeenWritten(argsIn []reflect.Value) bool {
128128
return false
129129
}
130130

131+
func wrapHandlerProvider[T http.Handler](hp func(next http.Handler) T, funcInfo *routing.FuncInfo) func(next http.Handler) http.Handler {
132+
return func(next http.Handler) http.Handler {
133+
h := hp(next) // this handle could be dynamically generated, so we can't use it for debug info
134+
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
135+
routing.UpdateFuncInfo(req.Context(), funcInfo)
136+
h.ServeHTTP(resp, req)
137+
})
138+
}
139+
}
140+
131141
// toHandlerProvider converts a handler to a handler provider
132142
// A handler provider is a function that takes a "next" http.Handler, it can be used as a middleware
133143
func toHandlerProvider(handler any) func(next http.Handler) http.Handler {
@@ -138,13 +148,9 @@ func toHandlerProvider(handler any) func(next http.Handler) http.Handler {
138148
}
139149

140150
if hp, ok := handler.(func(next http.Handler) http.Handler); ok {
141-
return func(next http.Handler) http.Handler {
142-
h := hp(next) // this handle could be dynamically generated, so we can't use it for debug info
143-
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
144-
routing.UpdateFuncInfo(req.Context(), funcInfo)
145-
h.ServeHTTP(resp, req)
146-
})
147-
}
151+
return wrapHandlerProvider(hp, funcInfo)
152+
} else if hp, ok := handler.(func(http.Handler) http.HandlerFunc); ok {
153+
return wrapHandlerProvider(hp, funcInfo)
148154
}
149155

150156
provider := func(next http.Handler) http.Handler {

routers/web/web.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ import (
4848
_ "code.gitea.io/gitea/modules/session" // to registers all internal adapters
4949

5050
"gitea.com/go-chi/captcha"
51-
"github.com/NYTimes/gziphandler"
5251
chi_middleware "github.com/go-chi/chi/v5/middleware"
5352
"github.com/go-chi/cors"
53+
"github.com/klauspost/compress/gzhttp"
5454
"github.com/prometheus/client_golang/prometheus"
5555
)
5656

@@ -244,11 +244,13 @@ func Routes() *web.Route {
244244
var mid []any
245245

246246
if setting.EnableGzip {
247-
h, err := gziphandler.GzipHandlerWithOpts(gziphandler.MinSize(GzipMinSize))
247+
// random jitter is recommended by: https://pkg.go.dev/github.com/klauspost/compress/gzhttp#readme-breach-mitigation
248+
// compression level 6 is the gzip default and a good general tradeoff between speed, CPU usage, and compression
249+
wrapper, err := gzhttp.NewWrapper(gzhttp.RandomJitter(32, 0, false), gzhttp.MinSize(GzipMinSize), gzhttp.CompressionLevel(6))
248250
if err != nil {
249-
log.Fatal("GzipHandlerWithOpts failed: %v", err)
251+
log.Fatal("gzhttp.NewWrapper failed: %v", err)
250252
}
251-
mid = append(mid, h)
253+
mid = append(mid, wrapper)
252254
}
253255

254256
if setting.Service.EnableCaptcha {

0 commit comments

Comments
 (0)