Skip to content

Commit c69ee80

Browse files
committed
fix(handler): do not cache websocket connections
1 parent ccf3a4e commit c69ee80

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

handler/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
headerContentType = "Content-Type"
1717
headerRange = "Range"
1818
headerVary = "Vary"
19+
headerUpgrade = "Upgrade"
1920

2021
headerForwardedFor = "X-Forwarded-For"
2122
headerRealIP = "X-Real-IP"

handler/middleware.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,27 @@ func staticMiddleware(next http.Handler) http.Handler {
121121

122122
func dynamicMiddleware(next http.Handler) http.Handler {
123123
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
124-
var ctx context.Context
124+
nr := r
125125
switch filepath.Ext(r.URL.EscapedPath()) {
126-
case ".css", ".ico", ".jpeg", ".jpg", ".webp":
127-
ctx = context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
126+
case ".css", ".ico", ".jpeg", ".jpg", ".js", ".webp":
127+
ctx := context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
128128
Prefix: cachePrefixStatic,
129129
})
130+
nr = r.WithContext(ctx)
130131
case ".m3u8", ".ts":
131-
ctx = r.Context()
132+
break
132133
default:
133134
if rh := r.Header.Get(headerRange); rh != "" {
134-
ctx = r.Context()
135+
break
136+
} else if upgrade := r.Header.Get(headerUpgrade); upgrade == "websocket" {
135137
break
136138
}
137-
ctx = context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
139+
ctx := context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
138140
Prefix: cachePrefixDynamic,
139141
})
142+
nr = r.WithContext(ctx)
140143
}
141-
cacheMiddleware(next).ServeHTTP(w, r.WithContext(ctx))
144+
cacheMiddleware(next).ServeHTTP(w, nr)
142145
})
143146
}
144147

0 commit comments

Comments
 (0)