Skip to content

Commit

Permalink
fix(handler): do not cache websocket connections
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Dec 28, 2023
1 parent ccf3a4e commit c69ee80
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions handler/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
headerContentType = "Content-Type"
headerRange = "Range"
headerVary = "Vary"
headerUpgrade = "Upgrade"

headerForwardedFor = "X-Forwarded-For"
headerRealIP = "X-Real-IP"
Expand Down
17 changes: 10 additions & 7 deletions handler/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,27 @@ func staticMiddleware(next http.Handler) http.Handler {

func dynamicMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var ctx context.Context
nr := r
switch filepath.Ext(r.URL.EscapedPath()) {
case ".css", ".ico", ".jpeg", ".jpg", ".webp":
ctx = context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
case ".css", ".ico", ".jpeg", ".jpg", ".js", ".webp":
ctx := context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
Prefix: cachePrefixStatic,
})
nr = r.WithContext(ctx)
case ".m3u8", ".ts":
ctx = r.Context()
break
default:
if rh := r.Header.Get(headerRange); rh != "" {
ctx = r.Context()
break
} else if upgrade := r.Header.Get(headerUpgrade); upgrade == "websocket" {
break
}
ctx = context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
ctx := context.WithValue(r.Context(), cacheInfoCtxKey, &cacheInfo{
Prefix: cachePrefixDynamic,
})
nr = r.WithContext(ctx)
}
cacheMiddleware(next).ServeHTTP(w, r.WithContext(ctx))
cacheMiddleware(next).ServeHTTP(w, nr)
})
}

Expand Down

0 comments on commit c69ee80

Please sign in to comment.