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

Fix: prevent caching js when http code isn't 200 #814

Merged
merged 3 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func enrichSwaggerObject(swo *spec.Swagger) {
}

func (s *restServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
staticFilters := []utils.FilterFunction{}
var staticFilters []utils.FilterFunction
if features.APIServerFeatureGate.Enabled(features.APIServerEnableCacheJSFile) {
staticFilters = append(staticFilters, filters.JSCache)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/server/utils/filters/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func Gzip(req *http.Request, res http.ResponseWriter, chain *utils.FilterChain)
if doCompress {
w, err := restful.NewCompressingResponseWriter(res, encoding)
if err != nil {
klog.Errorf("failed to create the compressing writer, err: %s", err.Error())
res.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/server/utils/filters/js-cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ func JSCache(req *http.Request, res http.ResponseWriter, chain *utils.FilterChai
if matchCacheCondition(req) {
if value, ok := jsFileCache.Get(req.URL.String()); ok {
if cacheData, ok := value.(*cacheData); ok {
cacheData.Write(res)
return
if cacheData.data.Len() == 0 {
klog.Warningf("Cache data is empty")
jsFileCache.Remove(req.URL.String())
} else {
cacheData.Write(res)
return
}
}
}
}
Expand All @@ -51,7 +56,11 @@ func JSCache(req *http.Request, res http.ResponseWriter, chain *utils.FilterChai
res.Header().Set(HeaderHitCache, "false")
cacheWriter := &CacheWriter{writer: res, cacheData: &cacheData{}}
chain.ProcessFilter(req, cacheWriter)
jsFileCache.Add(req.URL.String(), cacheWriter.cacheData)
if cacheWriter.cacheData.code == http.StatusOK {
jsFileCache.Add(req.URL.String(), cacheWriter.cacheData)
} else {
klog.Warningf("Skip cache the js file, code: %d", cacheWriter.cacheData.code)
}
return
}
chain.ProcessFilter(req, res)
Expand Down
23 changes: 20 additions & 3 deletions pkg/server/utils/filters/js-cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestJSCache(t *testing.T) {
chain.ProcessFilter(&http.Request{Method: "GET", URL: u}, res1)
assert.Equal(t, res1.Code, 200)
assert.Equal(t, res1.Body.String(), jsContent)
assert.Equal(t, res1.HeaderMap.Get(HeaderHitCache), "false")
assert.Equal(t, res1.Header().Get(HeaderHitCache), "false")
assert.Equal(t, jsFileCache.Len(), 1)
data, ok := jsFileCache.Get(u.String())
assert.Equal(t, ok, true)
Expand All @@ -49,7 +49,7 @@ func TestJSCache(t *testing.T) {
assert.Equal(t, res2.Code, 200)
assert.Equal(t, res2.Body.String(), jsContent)

assert.Equal(t, res2.HeaderMap.Get(HeaderHitCache), "true")
assert.Equal(t, res2.Header().Get(HeaderHitCache), "true")

u2, err := url.Parse("/test.js?v=2")
assert.Equal(t, err, nil)
Expand All @@ -58,7 +58,18 @@ func TestJSCache(t *testing.T) {
chain.ProcessFilter(&http.Request{Method: "GET", URL: u2}, res3)
assert.Equal(t, res3.Code, 200)
assert.Equal(t, res3.Body.String(), jsContent)
assert.Equal(t, res3.HeaderMap.Get(HeaderHitCache), "false")
assert.Equal(t, res3.Header().Get(HeaderHitCache), "false")
assert.Equal(t, jsFileCache.Len(), 2)

u3, err := url.Parse("/test.js?v=3")
assert.NoError(t, err)
res4 := httptest.NewRecorder()
chain = utils.NewFilterChain(loadJSNotOK, JSCache)
chain.ProcessFilter(&http.Request{Method: "GET", URL: u3}, res4)
assert.Equal(t, res4.Code, 304)
assert.Equal(t, res4.Body.String(), "")
assert.Equal(t, res4.Header().Get(HeaderHitCache), "false")
assert.Equal(t, jsFileCache.Len(), 2)
}

var jsContent = "console.log(\"hello\")"
Expand All @@ -69,3 +80,9 @@ func loadJS(req *http.Request, res http.ResponseWriter) {
res.Write([]byte(jsContent))
res.Header().Add(restful.HEADER_ContentType, "application/javascript")
}

func loadJSNotOK(req *http.Request, res http.ResponseWriter) {
fmt.Printf("return not 200,path:%s \n", req.URL.String())
res.WriteHeader(304)
res.Header().Add(restful.HEADER_ContentType, "application/javascript")
}