Skip to content

Commit 71ade83

Browse files
committed
fix(chore): surrogate use Content-Location instead of generated key
1 parent bf786a2 commit 71ade83

File tree

14 files changed

+88
-24
lines changed

14 files changed

+88
-24
lines changed

.github/workflows/generate_release.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
plugins=("beego" "caddy" "chi" "dotweb" "echo" "fiber" "gin" "go-zero" "goa" "goyave" "hertz" "kratos" "roadrunner" "skipper" "souin" "traefik" "tyk" "webgo")
3+
plugins=("beego" "caddy" "chi" "dotweb" "echo" "fiber" "gin" "go-zero" "goa" "goyave" "hertz" "kratos" "roadrunner" "skipper" "souin" "souin/storages" "traefik" "tyk" "webgo")
44

55
IFS= read -r -d '' tpl <<EOF
66
name: Tag submodules on release

.github/workflows/release_plugins.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ jobs:
177177
ref: 'refs/tags/plugins/souin/${{ github.ref_name }}',
178178
sha: context.sha
179179
})
180+
-
181+
name: Create Souin/storages tag
182+
uses: actions/github-script@v7
183+
with:
184+
script: |
185+
github.rest.git.createRef({
186+
owner: context.repo.owner,
187+
repo: context.repo.repo,
188+
ref: 'refs/tags/plugins/souin/storages/${{ github.ref_name }}',
189+
sha: context.sha
190+
})
180191
-
181192
name: Create Traefik tag
182193
uses: actions/github-script@v7

pkg/middleware/middleware.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ func (s *SouinBaseHandler) Store(
353353

354354
wg.Wait()
355355
if len(fails) < s.storersLen {
356-
go func(rs http.Response, key string, basekey string) {
357-
_ = s.SurrogateKeyStorer.Store(&rs, key, uri, basekey)
358-
}(res, variedKey, cachedKey)
356+
go func(rs http.Response, key string) {
357+
_ = s.SurrogateKeyStorer.Store(&rs, key, uri)
358+
}(res, variedKey)
359359
status += "; stored"
360360
}
361361

pkg/surrogate/providers/akamai.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ func (*AkamaiSurrogateStorage) getHeaderSeparator() string {
3939
}
4040

4141
// Store stores the response tags located in the first non empty supported header
42-
func (a *AkamaiSurrogateStorage) Store(response *http.Response, cacheKey, uri, basekey string) error {
42+
func (a *AkamaiSurrogateStorage) Store(response *http.Response, cacheKey, uri string) error {
4343
defer func() {
4444
response.Header.Del(surrogateKey)
4545
response.Header.Del(surrogateControl)
4646
}()
47-
e := a.baseStorage.Store(response, cacheKey, uri, basekey)
47+
e := a.baseStorage.Store(response, cacheKey, uri)
4848
response.Header.Set(edgeCacheTag, response.Header.Get(surrogateKey))
4949

5050
return e

pkg/surrogate/providers/cloudflare.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ func (*CloudflareSurrogateStorage) getHeaderSeparator() string {
3838
}
3939

4040
// Store stores the response tags located in the first non empty supported header
41-
func (c *CloudflareSurrogateStorage) Store(response *http.Response, cacheKey, uri, basekey string) error {
41+
func (c *CloudflareSurrogateStorage) Store(response *http.Response, cacheKey, uri string) error {
4242
defer func() {
4343
response.Header.Del(surrogateKey)
4444
response.Header.Del(surrogateControl)
4545
}()
46-
e := c.baseStorage.Store(response, cacheKey, uri, basekey)
46+
e := c.baseStorage.Store(response, cacheKey, uri)
4747
response.Header.Set(cacheTag, strings.Join(c.ParseHeaders(response.Header.Get(surrogateKey)), c.getHeaderSeparator()))
4848

4949
return e

pkg/surrogate/providers/common.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (s *baseStorage) purgeTag(tag string) []string {
210210
}
211211

212212
// Store will take the lead to store the cache key for each provided Surrogate-key
213-
func (s *baseStorage) Store(response *http.Response, cacheKey, uri, basekey string) error {
213+
func (s *baseStorage) Store(response *http.Response, cacheKey, uri string) error {
214214
h := response.Header
215215

216216
cacheKey = url.QueryEscape(cacheKey)
@@ -238,8 +238,11 @@ func (s *baseStorage) Store(response *http.Response, cacheKey, uri, basekey stri
238238
}
239239
}
240240

241-
urlRegexp = regexp.MustCompile("(^|" + regexp.QuoteMeta(souinStorageSeparator) + ")" + regexp.QuoteMeta(basekey) + "(" + regexp.QuoteMeta(souinStorageSeparator) + "|$)")
242-
s.storeTag(uri, basekey, urlRegexp)
241+
if h.Get("Content-Location") != "" {
242+
location := h.Get("Content-Location")
243+
urlRegexp = regexp.MustCompile("(^|" + regexp.QuoteMeta(souinStorageSeparator) + ")" + regexp.QuoteMeta(location) + "(" + regexp.QuoteMeta(souinStorageSeparator) + "|$)")
244+
s.storeTag(uri, location, urlRegexp)
245+
}
243246
s.storeTag(uri, cacheKey, urlRegexp)
244247

245248
return nil

pkg/surrogate/providers/common_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestBaseStorage_Store(t *testing.T) {
106106

107107
bs := mockCommonProvider()
108108

109-
e := bs.Store(&res, "((((invalid_key_but_escaped", "", "")
109+
e := bs.Store(&res, "((((invalid_key_but_escaped", "")
110110
if e != nil {
111111
t.Error("It shouldn't throw an error with a valid key.")
112112
}
@@ -116,7 +116,7 @@ func TestBaseStorage_Store(t *testing.T) {
116116
_ = bs.Storage.Set("test5", []byte("first,second,fifth"), storageToInfiniteTTLMap[bs.Storage.Name()])
117117
_ = bs.Storage.Set("testInvalid", []byte("invalid"), storageToInfiniteTTLMap[bs.Storage.Name()])
118118

119-
if e = bs.Store(&res, "stored", "", ""); e != nil {
119+
if e = bs.Store(&res, "stored", ""); e != nil {
120120
t.Error("It shouldn't throw an error with a valid key.")
121121
}
122122

@@ -133,10 +133,10 @@ func TestBaseStorage_Store(t *testing.T) {
133133
}
134134

135135
res.Header.Set(surrogateKey, "something")
136-
_ = bs.Store(&res, "/something", "", "")
137-
_ = bs.Store(&res, "/something", "", "")
136+
_ = bs.Store(&res, "/something", "")
137+
_ = bs.Store(&res, "/something", "")
138138
res.Header.Set(surrogateKey, "something")
139-
_ = bs.Store(&res, "/some", "", "")
139+
_ = bs.Store(&res, "/some", "")
140140

141141
_ = len(bs.Storage.MapKeys(surrogatePrefix))
142142
// if storageSize != 6 {
@@ -161,7 +161,7 @@ func TestBaseStorage_Store_Load(t *testing.T) {
161161
wg.Add(1)
162162
go func(r http.Response, iteration int, group *sync.WaitGroup) {
163163
defer wg.Done()
164-
_ = bs.Store(&r, fmt.Sprintf("my_dynamic_cache_key_%d", iteration), "", "")
164+
_ = bs.Store(&r, fmt.Sprintf("my_dynamic_cache_key_%d", iteration), "")
165165
}(res, i, &wg)
166166
}
167167

pkg/surrogate/providers/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type SurrogateInterface interface {
1616
Purge(http.Header) (cacheKeys []string, surrogateKeys []string)
1717
Invalidate(method string, h http.Header)
1818
purgeTag(string) []string
19-
Store(*http.Response, string, string, string) error
19+
Store(*http.Response, string, string) error
2020
storeTag(string, string, *regexp.Regexp)
2121
ParseHeaders(string) []string
2222
List() map[string]string

plugins/caddy/httpcache_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,3 +1038,51 @@ func TestExpires(t *testing.T) {
10381038
cacheChecker(caddyTester, "/expires-with-max-age", "Hello, expires-with-max-age!", 59)
10391039
cacheChecker(caddyTester, "/expires-with-s-maxage", "Hello, expires-with-s-maxage!", 4)
10401040
}
1041+
1042+
func TestComplexQuery(t *testing.T) {
1043+
caddyTester := caddytest.NewTester(t)
1044+
caddyTester.InitServer(`
1045+
{
1046+
admin localhost:2999
1047+
http_port 9080
1048+
https_port 9443
1049+
cache {
1050+
ttl 10s
1051+
}
1052+
}
1053+
localhost:9080 {
1054+
route /complex-query {
1055+
cache
1056+
respond "Hello, {query}!"
1057+
}
1058+
}`, "caddyfile")
1059+
1060+
cacheChecker := func(tester *caddytest.Tester, query string, expectedDuration int) {
1061+
body := fmt.Sprintf("Hello, %s!", query)
1062+
resp1, _ := tester.AssertGetResponse("http://localhost:9080/complex-query?"+query, 200, body)
1063+
if resp1.Header.Get("Age") != "" {
1064+
t.Errorf("unexpected Age header %v", resp1.Header.Get("Age"))
1065+
}
1066+
1067+
if resp1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/complex-query?"+query {
1068+
t.Errorf("unexpected first Cache-Status header %v", resp1.Header.Get("Cache-Status"))
1069+
}
1070+
1071+
resp1, _ = tester.AssertGetResponse("http://localhost:9080/complex-query?"+query, 200, body)
1072+
1073+
if resp1.Header.Get("Age") != "1" {
1074+
t.Errorf("unexpected Age header %v", resp1.Header.Get("Age"))
1075+
}
1076+
1077+
if resp1.Header.Get("Cache-Status") != fmt.Sprintf("Souin; hit; ttl=%d; key=GET-http-localhost:9080-/complex-query?%s; detail=DEFAULT", expectedDuration, query) {
1078+
t.Errorf(
1079+
"unexpected second Cache-Status header %v, expected %s",
1080+
resp1.Header.Get("Cache-Status"),
1081+
fmt.Sprintf("Souin; hit; ttl=%d; key=GET-http-localhost:9080-/complex-query?%s; detail=DEFAULT", expectedDuration, query),
1082+
)
1083+
}
1084+
}
1085+
1086+
cacheChecker(caddyTester, "fields[]=id&pagination=true", 9)
1087+
cacheChecker(caddyTester, "fields[]=id&pagination=false", 9)
1088+
}

plugins/go-zero/examples/internal/handler/routes.go

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

0 commit comments

Comments
 (0)