From 04defb0d9fdf17b3b6fec44cfd67919cf9aa6df3 Mon Sep 17 00:00:00 2001 From: darkweak Date: Mon, 22 Apr 2024 09:15:12 +0200 Subject: [PATCH] fix(review): from hussam-almarzoq --- README.md | 15 +++++++++++++++ pkg/api/souin.go | 8 +++++++- pkg/storage/redisProvider.go | 13 ++++++++----- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 167344c26..2ec57bca6 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,9 @@ cache_keys: disable_host: true # Prevent the host from being used in the cache key disable_method: true # Prevent the method from being used in the cache key disable_query: true # Prevent the query string from being used in the cache key + disable_scheme: true # request scheme the query string from being used in the cache key + hash: true # Hash the cache key instead of a plaintext one + hide: true # Prevent the cache key to be in the response Cache-Status header headers: # Add headers to the key - Authorization # Add the header value in the key - Content-Type # Add the header value in the key @@ -107,6 +110,9 @@ default_cache: disable_host: true # Prevent the host from being used in the cache key disable_method: true # Prevent the method from being used in the cache key disable_query: true # Prevent the query string from being used in the cache key + disable_scheme: true # Prevent the request scheme string from being used in the cache key + hash: true # Hash the cache key instead of a plaintext one + hide: true # Prevent the cache key to be in the response Cache-Status header headers: # Add headers to the key - Authorization # Add the header value in the key - Content-Type # Add the header value in the key @@ -172,6 +178,8 @@ surrogate_keys: | `cache_keys.{your regexp}.disable_host` | Disable the host part in the key matching the regexp | `true`

`(default: false)` | | `cache_keys.{your regexp}.disable_method` | Disable the method part in the key matching the regexp | `true`

`(default: false)` | | `cache_keys.{your regexp}.disable_query` | Disable the query string part in the key matching the regexp | `true`

`(default: false)` | +| `cache_keys.{your regexp}.disable_scheme` | Disable the request scheme string part in the key matching the regexp | `true`

`(default: false)` | +| `cache_keys.{your regexp}.hash` | Hash the key matching the regexp | `true`

`(default: false)` | | `cache_keys.{your regexp}.headers` | Add headers to the key matching the regexp | `- Authorization`

`- Content-Type`

`- X-Additional-Header` | | `cache_keys.{your regexp}.hide` | Prevent the key from being exposed in the `Cache-Status` HTTP response header | `true`

`(default: false)` | | `cdn` | The CDN management, if you use any cdn to proxy your requests Souin will handle that | | @@ -197,6 +205,8 @@ surrogate_keys: | `default_cache.key.disable_host` | Disable the host part in the key | `true`

`(default: false)` | | `default_cache.key.disable_method` | Disable the method part in the key | `true`

`(default: false)` | | `default_cache.key.disable_query` | Disable the query string part in the key | `true`

`(default: false)` | +| `default_cache.key.disable_scheme` | Disable the request scheme string part in the key | `true`

`(default: false)` | +| `default_cache.key.hash` | Hash the key name in the storage | `true`

`(default: false)` | | `default_cache.key.headers` | Add headers to the key matching the regexp | `- Authorization`

`- Content-Type`

`- X-Additional-Header` | | `default_cache.key.hide` | Prevent the key from being exposed in the `Cache-Status` HTTP response header | `true`

`(default: false)` | | `default_cache.mode` | RFC respect tweaking | One of `bypass` `bypass_request` `bypass_response` `strict` (default `strict`) | @@ -468,8 +478,10 @@ There is the fully configuration below disable_host disable_method disable_query + disable_scheme headers X-Token Authorization hide + hash } } cdn { @@ -488,6 +500,9 @@ There is the fully configuration below disable_host disable_method disable_query + disable_scheme + hash + hide headers Content-Type Authorization } log_level debug diff --git a/pkg/api/souin.go b/pkg/api/souin.go index 7c36c169f..2c32eddea 100644 --- a/pkg/api/souin.go +++ b/pkg/api/souin.go @@ -164,7 +164,13 @@ func (s *SouinAPI) purgeMapping() { } if updated { - _ = current.Set(storage.MappingKeyPrefix+k, []byte{}, configurationtypes.URL{}, infiniteStoreDuration) + buf := new(bytes.Buffer) + e = gob.NewEncoder(buf).Encode(mapping) + if e != nil { + fmt.Println("Impossible to re-encode the mapping", storage.MappingKeyPrefix+k) + current.Delete(storage.MappingKeyPrefix + k) + } + _ = current.Set(storage.MappingKeyPrefix+k, buf.Bytes(), configurationtypes.URL{}, infiniteStoreDuration) } } } diff --git a/pkg/storage/redisProvider.go b/pkg/storage/redisProvider.go index 551e3da56..236f418d7 100644 --- a/pkg/storage/redisProvider.go +++ b/pkg/storage/redisProvider.go @@ -96,12 +96,15 @@ func (provider *Redis) ListKeys() []string { for _, element := range scan.Elements { value := provider.Get(element) mapping, err := decodeMapping(value) - if err == nil { - for _, v := range mapping.Mapping { - if !v.FreshTime.Before(time.Now()) || !v.StaleTime.Before(time.Now()) { - elements = append(elements, v.RealKey) - } + if err != nil { + continue + } + + for _, v := range mapping.Mapping { + if v.FreshTime.Before(time.Now()) && v.StaleTime.Before(time.Now()) { + continue } + elements = append(elements, v.RealKey) } } }