Skip to content

Commit 98a06f0

Browse files
authored
feat(core): disable vary (#599)
* feat(core): disable vary if needed through configuration * feat(documentation): refers the disable_vary directive * fix(ci): plugin tests * fix(lint): disable staticcheck on cre.DISABLE_VARY_CTX * fix(caddy): downgrade to 2.8.4 due to regression on returned errror because of the timeout * bump(version): from v1.7.5 to v1.7.6
1 parent a870ef2 commit 98a06f0

File tree

158 files changed

+8104
-5103
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+8104
-5103
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ generate-workflow: ## Generate plugin workflow
147147
bash .github/workflows/workflow_plugins_generator.sh
148148

149149
golangci-lint: ## Run golangci-lint to ensure the code quality
150-
docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:v1.59.1 golangci-lint run -v --timeout 180s ./...
150+
docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:latest golangci-lint run -v --timeout 180s ./...
151151
for plugin in $(PLUGINS_LIST) ; do \
152-
echo "Starting lint $$plugin \n" && docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:v1.59.1 golangci-lint run -v --exclude-dirs=override --timeout 240s ./plugins/$$plugin; \
152+
echo "Starting lint $$plugin \n" && docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:latest golangci-lint run -v --exclude-dirs=override --timeout 240s ./plugins/$$plugin; \
153153
done
154154
cd plugins/caddy && go mod tidy && go mod download
155155

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ cache_keys:
9090
disable_method: true # Prevent the method from being used in the cache key
9191
disable_query: true # Prevent the query string from being used in the cache key
9292
disable_scheme: true # request scheme the query string from being used in the cache key
93+
disable_vary: true # Prevent the varied headers string from being used in the cache key
9394
hash: true # Hash the cache key instead of a plaintext one
9495
hide: true # Prevent the cache key to be in the response Cache-Status header
9596
headers: # Add headers to the key
@@ -117,6 +118,7 @@ default_cache:
117118
disable_method: true # Prevent the method from being used in the cache key
118119
disable_query: true # Prevent the query string from being used in the cache key
119120
disable_scheme: true # Prevent the request scheme string from being used in the cache key
121+
disable_vary: true # Prevent the varied headers string from being used in the cache key
120122
hash: true # Hash the cache key instead of a plaintext one
121123
hide: true # Prevent the cache key to be in the response Cache-Status header
122124
headers: # Add headers to the key
@@ -185,6 +187,7 @@ surrogate_keys:
185187
| `cache_keys.{your regexp}.disable_method` | Disable the method part in the key matching the regexp | `true`<br/><br/>`(default: false)` |
186188
| `cache_keys.{your regexp}.disable_query` | Disable the query string part in the key matching the regexp | `true`<br/><br/>`(default: false)` |
187189
| `cache_keys.{your regexp}.disable_scheme` | Disable the request scheme string part in the key matching the regexp | `true`<br/><br/>`(default: false)` |
190+
| `cache_keys.{your regexp}.disable_vary` | Disable the vary string part in the key matching the regexp | `true`<br/><br/>`(default: false)` |
188191
| `cache_keys.{your regexp}.hash` | Hash the key matching the regexp | `true`<br/><br/>`(default: false)` |
189192
| `cache_keys.{your regexp}.headers` | Add headers to the key matching the regexp | `- Authorization`<br/><br/>`- Content-Type`<br/><br/>`- X-Additional-Header` |
190193
| `cache_keys.{your regexp}.hide` | Prevent the key from being exposed in the `Cache-Status` HTTP response header | `true`<br/><br/>`(default: false)` |
@@ -199,7 +202,7 @@ surrogate_keys:
199202
| `cdn.service_id` | The service id if required, depending the provider | `123456_id` |
200203
| `cdn.zone_id` | The zone id if required, depending the provider | `anywhere_zone` |
201204
| `default_cache.allowed_http_verbs` | The HTTP verbs to support cache | `- GET`<br/><br/>`- POST`<br/><br/>`(default: GET, HEAD)` |
202-
| `default_cache.allowed_additional_status_codes` | The additional HTTP status code to support cache | `- 200`<br/><br/>`- 404` |
205+
| `default_cache.allowed_additional_status_codes` | The additional HTTP status code to support cache | `- 200`<br/><br/>`- 404` |
203206
| `default_cache.badger` | Configure the Badger cache storage | |
204207
| `default_cache.badger.path` | Configure Badger with a file | `/anywhere/badger_configuration.json` |
205208
| `default_cache.badger.configuration` | Configure Badger directly in the Caddyfile or your JSON caddy configuration | [See the Badger configuration for the options](https://dgraph.io/docs/badger/get-started/) |
@@ -213,6 +216,7 @@ surrogate_keys:
213216
| `default_cache.key.disable_method` | Disable the method part in the key | `true`<br/><br/>`(default: false)` |
214217
| `default_cache.key.disable_query` | Disable the query string part in the key | `true`<br/><br/>`(default: false)` |
215218
| `default_cache.key.disable_scheme` | Disable the request scheme string part in the key | `true`<br/><br/>`(default: false)` |
219+
| `default_cache.key.disable_vary` | Disable the request vary string part in the key | `true`<br/><br/>`(default: false)` |
216220
| `default_cache.key.hash` | Hash the key name in the storage | `true`<br/><br/>`(default: false)` |
217221
| `default_cache.key.headers` | Add headers to the key matching the regexp | `- Authorization`<br/><br/>`- Content-Type`<br/><br/>`- X-Additional-Header` |
218222
| `default_cache.key.hide` | Prevent the key from being exposed in the `Cache-Status` HTTP response header | `true`<br/><br/>`(default: false)` |
@@ -450,6 +454,7 @@ There is the fully configuration below
450454
disable_method
451455
disable_query
452456
disable_scheme
457+
disable_vary
453458
headers X-Token Authorization
454459
hide
455460
hash
@@ -472,6 +477,7 @@ There is the fully configuration below
472477
disable_method
473478
disable_query
474479
disable_scheme
480+
disable_vary
475481
hash
476482
hide
477483
headers Content-Type Authorization
@@ -960,7 +966,7 @@ experimental:
960966
plugins:
961967
souin:
962968
moduleName: github.com/darkweak/souin
963-
version: v1.7.5
969+
version: v1.7.6
964970
```
965971
After that you can declare either the whole configuration at once in the middleware block or by service. See the examples below.
966972
```yaml

configurationtypes/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/darkweak/storages/core"
13-
yaml "gopkg.in/yaml.v3"
13+
"gopkg.in/yaml.v3"
1414
)
1515

1616
type CacheKey map[RegValue]Key
@@ -52,6 +52,9 @@ func (c *CacheKeys) parseJSON(rootDecoder *json.Decoder) {
5252
case "disable_scheme":
5353
val, _ := rootDecoder.Token()
5454
key.DisableScheme, _ = strconv.ParseBool(fmt.Sprint(val))
55+
case "disable_vary":
56+
val, _ := rootDecoder.Token()
57+
key.DisableVary, _ = strconv.ParseBool(fmt.Sprint(val))
5558
case "hash":
5659
val, _ := rootDecoder.Token()
5760
key.Hash, _ = strconv.ParseBool(fmt.Sprint(val))
@@ -222,6 +225,7 @@ type Key struct {
222225
DisableMethod bool `json:"disable_method,omitempty" yaml:"disable_method,omitempty"`
223226
DisableQuery bool `json:"disable_query,omitempty" yaml:"disable_query,omitempty"`
224227
DisableScheme bool `json:"disable_scheme,omitempty" yaml:"disable_scheme,omitempty"`
228+
DisableVary bool `json:"disable_vary,omitempty" yaml:"disable_vary,omitempty"`
225229
Hash bool `json:"hash,omitempty" yaml:"hash,omitempty"`
226230
Hide bool `json:"hide,omitempty" yaml:"hide,omitempty"`
227231
Template string `json:"template,omitempty" yaml:"template,omitempty"`

context/key.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/caddyserver/caddy/v2"
99
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
1010
"github.com/darkweak/souin/configurationtypes"
11+
"github.com/darkweak/storages/core"
1112
)
1213

1314
const (
@@ -22,6 +23,7 @@ type keyContext struct {
2223
disable_host bool
2324
disable_method bool
2425
disable_query bool
26+
disable_vary bool
2527
disable_scheme bool
2628
displayable bool
2729
hash bool
@@ -43,6 +45,7 @@ func (g *keyContext) SetupContext(c configurationtypes.AbstractConfigurationInte
4345
g.disable_method = k.DisableMethod
4446
g.disable_query = k.DisableQuery
4547
g.disable_scheme = k.DisableScheme
48+
g.disable_vary = k.DisableVary
4649
g.hash = k.Hash
4750
g.displayable = !k.Hide
4851
g.template = k.Template
@@ -58,6 +61,7 @@ func (g *keyContext) SetupContext(c configurationtypes.AbstractConfigurationInte
5861
disable_method: v.DisableMethod,
5962
disable_query: v.DisableQuery,
6063
disable_scheme: v.DisableScheme,
64+
disable_vary: v.DisableVary,
6165
hash: v.Hash,
6266
displayable: !v.Hide,
6367
template: v.Template,
@@ -154,9 +158,13 @@ func (g *keyContext) SetContext(req *http.Request) *http.Request {
154158
context.WithValue(
155159
context.WithValue(
156160
context.WithValue(
157-
req.Context(),
158-
Key,
159-
key,
161+
context.WithValue(
162+
req.Context(),
163+
Key,
164+
key,
165+
),
166+
core.DISABLE_VARY_CTX, //nolint:staticcheck // we don't care about collision
167+
g.disable_vary,
160168
),
161169
Hashed,
162170
hash,

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ go 1.22.1
55
require (
66
github.com/caddyserver/caddy/v2 v2.8.4
77
github.com/cespare/xxhash/v2 v2.2.0
8-
github.com/darkweak/storages/core v0.0.11
8+
github.com/darkweak/storages/core v0.0.13
99
github.com/google/uuid v1.6.0
10-
github.com/pierrec/lz4/v4 v4.1.21
10+
github.com/pierrec/lz4/v4 v4.1.22
1111
github.com/pquerna/cachecontrol v0.2.0
1212
github.com/prometheus/client_golang v1.19.1
1313
github.com/prometheus/client_model v0.5.0
1414
go.uber.org/zap v1.27.0
1515
golang.org/x/sync v0.7.0
16-
google.golang.org/protobuf v1.34.2
16+
google.golang.org/protobuf v1.36.5
1717
gopkg.in/yaml.v3 v3.0.1
1818
)
1919

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
9898
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
9999
github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns=
100100
github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654=
101-
github.com/darkweak/storages/core v0.0.11 h1:IwvpAtkhOmxC5pIffJ8opW6erpTnIi5zqPveiAQs8ew=
102-
github.com/darkweak/storages/core v0.0.11/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY=
101+
github.com/darkweak/storages/core v0.0.13 h1:T2nA306M9eXBEpIlSf8czCzXrJ4MAS62fJhOV7ijI9U=
102+
github.com/darkweak/storages/core v0.0.13/go.mod h1:3qJqrenCLpu+0bWPOAq36CmGpzL3SGWAz6KGZGnur1U=
103103
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
104104
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
105105
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -300,8 +300,8 @@ github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYG
300300
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
301301
github.com/peterbourgon/diskv/v3 v3.0.1 h1:x06SQA46+PKIUftmEujdwSEpIx8kR+M9eLYsUxeYveU=
302302
github.com/peterbourgon/diskv/v3 v3.0.1/go.mod h1:kJ5Ny7vLdARGU3WUuy6uzO6T0nb/2gWcT1JiBvRmb5o=
303-
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
304-
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
303+
github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU=
304+
github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
305305
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
306306
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
307307
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -576,8 +576,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 h1:
576576
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
577577
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
578578
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
579-
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
580-
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
579+
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
580+
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
581581
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
582582
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
583583
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

pkg/rfc/vary.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"net/url"
77
"slices"
88
"strings"
9+
10+
"github.com/darkweak/storages/core"
911
)
1012

1113
const (
@@ -15,6 +17,11 @@ const (
1517

1618
// GetVariedCacheKey returns the varied cache key for req and resp.
1719
func GetVariedCacheKey(rq *http.Request, headers []string) string {
20+
isVaryDisabled := rq.Context().Value(core.DISABLE_VARY_CTX)
21+
if isVaryDisabled != nil && isVaryDisabled.(bool) {
22+
return ""
23+
}
24+
1825
if len(headers) == 0 {
1926
return ""
2027
}

0 commit comments

Comments
 (0)