Skip to content

Commit

Permalink
fix: souin. prefix on context keys and throw error on caddy directive…
Browse files Browse the repository at this point in the history
… error
  • Loading branch information
darkweak committed Jul 13, 2023
1 parent 70f8d8b commit a6b6a14
Show file tree
Hide file tree
Showing 28 changed files with 93 additions and 61 deletions.
4 changes: 2 additions & 2 deletions context/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

const (
CacheName ctxKey = "CACHE_NAME"
RequestCacheControl ctxKey = "REQUEST_CACHE_CONTROL"
CacheName ctxKey = "souin_ctx.CACHE_NAME"
RequestCacheControl ctxKey = "souin_ctx.REQUEST_CACHE_CONTROL"
)

var defaultCacheName string = "Souin"
Expand Down
6 changes: 3 additions & 3 deletions context/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

const (
GraphQL ctxKey = "GRAPHQL"
HashBody ctxKey = "HASH_BODY"
IsMutationRequest ctxKey = "IS_MUTATION_REQUEST"
GraphQL ctxKey = "souin_ctx.GRAPHQL"
HashBody ctxKey = "souin_ctx.HASH_BODY"
IsMutationRequest ctxKey = "souin_ctx.IS_MUTATION_REQUEST"
)

type graphQLContext struct {
Expand Down
6 changes: 3 additions & 3 deletions context/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

const (
Key ctxKey = "CACHE_KEY"
DisplayableKey ctxKey = "DISPLAYABLE_KEY"
IgnoredHeaders ctxKey = "IGNORE_HEADERS"
Key ctxKey = "souin_ctx.CACHE_KEY"
DisplayableKey ctxKey = "souin_ctx.DISPLAYABLE_KEY"
IgnoredHeaders ctxKey = "souin_ctx.IGNORE_HEADERS"
)

type keyContext struct {
Expand Down
2 changes: 1 addition & 1 deletion context/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/darkweak/souin/configurationtypes"
)

const SupportedMethod ctxKey = "SUPPORTED_METHOD"
const SupportedMethod ctxKey = "souin_ctx.SUPPORTED_METHOD"

var defaultVerbs []string = []string{http.MethodGet, http.MethodHead}

Expand Down
2 changes: 1 addition & 1 deletion context/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/darkweak/souin/configurationtypes"
)

const Mode ctxKey = "MODE"
const Mode ctxKey = "souin_ctx.MODE"

type ModeContext struct {
Strict, Bypass_request, Bypass_response bool
Expand Down
2 changes: 1 addition & 1 deletion context/now.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/darkweak/souin/configurationtypes"
)

const Now ctxKey = "NOW"
const Now ctxKey = "souin_ctx.NOW"

type nowContext struct{}

Expand Down
4 changes: 2 additions & 2 deletions context/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

const (
TimeoutCache ctxKey = "TIMEOUT_CACHE"
TimeoutCancel ctxKey = "TIMEOUT_CANCEL"
TimeoutCache ctxKey = "souin_ctx.TIMEOUT_CACHE"
TimeoutCancel ctxKey = "souin_ctx.TIMEOUT_CANCEL"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion context/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type (
}
)

const CacheControlCtx ctxKey = "CACHE-CONTROL-CTX"
const CacheControlCtx ctxKey = "souin_ctx.CACHE-CONTROL-CTX"

func GetContext() *Context {
return &Context{
Expand Down
19 changes: 11 additions & 8 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ func (s *SouinBaseHandler) Store(
customWriter.Headers.Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=INVALID-RESPONSE-CACHE-CONTROL", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context())))
return nil
}
if !rq.Context().Value(context.Mode).(*context.ModeContext).Bypass_request && (responseCc.PrivatePresent || rq.Header.Get("Authorization") != "") && !canBypassAuthorizationRestriction(customWriter.Header(), rq.Context().Value(context.IgnoredHeaders).([]string)) {

modeContext := rq.Context().Value(context.Mode).(*context.ModeContext)
if !modeContext.Bypass_request && (responseCc.PrivatePresent || rq.Header.Get("Authorization") != "") && !canBypassAuthorizationRestriction(customWriter.Header(), rq.Context().Value(context.IgnoredHeaders).([]string)) {
customWriter.Headers.Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=PRIVATE-OR-AUTHENTICATED-RESPONSE", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context())))
return nil
}
Expand Down Expand Up @@ -207,8 +209,8 @@ func (s *SouinBaseHandler) Store(
}

status := fmt.Sprintf("%s; fwd=uri-miss", rq.Context().Value(context.CacheName))
if (rq.Context().Value(context.Mode).(*context.ModeContext).Bypass_request || !requestCc.NoStore) &&
(rq.Context().Value(context.Mode).(*context.ModeContext).Bypass_response || !responseCc.NoStore) {
if (modeContext.Bypass_request || !requestCc.NoStore) &&
(modeContext.Bypass_response || !responseCc.NoStore) {
headers := customWriter.Headers.Clone()
for hname, shouldDelete := range responseCc.NoCache {
if shouldDelete {
Expand Down Expand Up @@ -360,7 +362,8 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n

requestCc, coErr := cacheobject.ParseRequestCacheControl(rq.Header.Get("Cache-Control"))

if !rq.Context().Value(context.Mode).(*context.ModeContext).Bypass_request && (coErr != nil || requestCc == nil) {
modeContext := rq.Context().Value(context.Mode).(*context.ModeContext)
if !modeContext.Bypass_request && (coErr != nil || requestCc == nil) {
rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=CACHE-CONTROL-EXTRACTION-ERROR")

return next(rw, rq)
Expand All @@ -385,11 +388,11 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n
crw.mutex.Unlock()
}(rq, customWriter)
s.Configuration.GetLogger().Sugar().Debugf("Request cache-control %+v", requestCc)
if rq.Context().Value(context.Mode).(*context.ModeContext).Bypass_request || !requestCc.NoCache {
if modeContext.Bypass_request || !requestCc.NoCache {
validator := rfc.ParseRequest(rq)
response := s.Storer.Prefix(cachedKey, rq, validator)

if response != nil && (!rq.Context().Value(context.Mode).(*context.ModeContext).Strict || rfc.ValidateCacheControl(response, requestCc)) {
if response != nil && (!modeContext.Strict || rfc.ValidateCacheControl(response, requestCc)) {
if validator.NeedRevalidation {
err := s.Revalidate(validator, next, customWriter, rq, requestCc, cachedKey)
_, _ = io.Copy(customWriter.Buf, response.Body)
Expand All @@ -398,7 +401,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n
return err
}
rfc.SetCacheStatusHeader(response)
if !rq.Context().Value(context.Mode).(*context.ModeContext).Strict || rfc.ValidateMaxAgeCachedResponse(requestCc, response) != nil {
if !modeContext.Strict || rfc.ValidateMaxAgeCachedResponse(requestCc, response) != nil {
customWriter.Headers = response.Header
customWriter.statusCode = response.StatusCode
s.Configuration.GetLogger().Sugar().Debugf("Serve from cache %+v", rq)
Expand All @@ -409,7 +412,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n
}
} else if response == nil && !requestCc.OnlyIfCached && (requestCc.MaxStaleSet || requestCc.MaxStale > -1) {
response = s.Storer.Prefix(storage.StalePrefix+cachedKey, rq, validator)
if nil != response && (!rq.Context().Value(context.Mode).(*context.ModeContext).Strict || rfc.ValidateCacheControl(response, requestCc)) {
if nil != response && (!modeContext.Strict || rfc.ValidateCacheControl(response, requestCc)) {
addTime, _ := time.ParseDuration(response.Header.Get(rfc.StoredTTLHeader))
rfc.SetCacheStatusHeader(response)

Expand Down
35 changes: 32 additions & 3 deletions plugins/caddy/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
switch directive {
case "basepath":
apiConfiguration.Debug.BasePath = h.RemainingArgs()[0]
default:
return h.Errf("unsupported debug directive: %s", directive)
}
}
case "prometheus":
Expand All @@ -291,6 +293,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
switch directive {
case "basepath":
apiConfiguration.Prometheus.BasePath = h.RemainingArgs()[0]
default:
return h.Errf("unsupported prometheus directive: %s", directive)
}
}
case "souin":
Expand All @@ -301,8 +305,12 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
switch directive {
case "basepath":
apiConfiguration.Souin.BasePath = h.RemainingArgs()[0]
default:
return h.Errf("unsupported souin directive: %s", directive)
}
}
default:
return h.Errf("unsupported api directive: %s", directive)
}
}
cfg.API = apiConfiguration
Expand All @@ -317,6 +325,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
case "configuration":
provider.Configuration = parseCaddyfileRecursively(h)
provider.Configuration = parseBadgerConfiguration(provider.Configuration.(map[string]interface{}))
default:
return h.Errf("unsupported badger directive: %s", directive)
}
}
cfg.DefaultCache.Badger = provider
Expand Down Expand Up @@ -344,6 +354,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
ck.Hide = true
case "headers":
ck.Headers = h.RemainingArgs()
default:
return h.Errf("unsupported cache_keys (%s) directive: %s", rg, directive)
}
}

Expand Down Expand Up @@ -374,6 +386,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
cdn.Provider = h.RemainingArgs()[0]
case "strategy":
cdn.Strategy = h.RemainingArgs()[0]
default:
return h.Errf("unsupported cdn directive: %s", directive)
}
}
cfg.DefaultCache.CDN = cdn
Expand All @@ -388,6 +402,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
switch directive {
case "configuration":
provider.Configuration = parseCaddyfileRecursively(h)
default:
return h.Errf("unsupported etcd directive: %s", directive)
}
}
cfg.DefaultCache.Etcd = provider
Expand All @@ -410,13 +426,18 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
config_key.Hide = true
case "headers":
config_key.Headers = h.RemainingArgs()
default:
return h.Errf("unsupported key directive: %s", directive)
}
}
cfg.DefaultCache.Key = config_key
case "log_level":
args := h.RemainingArgs()
cfg.LogLevel = args[0]
case "mode":
if len(h.RemainingArgs()) > 1 {
return h.Errf("mode must contains only one arg: %s given", h.RemainingArgs())
}
cfg.DefaultCache.Mode = strings.Join(h.RemainingArgs(), "")
case "nuts":
provider := configurationtypes.CacheProvider{}
Expand All @@ -431,6 +452,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
provider.Path = urlArgs[0]
case "configuration":
provider.Configuration = parseCaddyfileRecursively(h)
default:
return h.Errf("unsupported nuts directive: %s", directive)
}
}
cfg.DefaultCache.Nuts = provider
Expand All @@ -448,6 +471,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
provider.Path = urlArgs[0]
case "configuration":
provider.Configuration = parseCaddyfileRecursively(h)
default:
return h.Errf("unsupported olric directive: %s", directive)
}
}
cfg.DefaultCache.Olric = provider
Expand All @@ -466,6 +491,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
case "configuration":
provider.Configuration = parseCaddyfileRecursively(h)
provider.Configuration = parseRedisConfiguration(provider.Configuration.(map[string]interface{}))
default:
return h.Errf("unsupported redis directive: %s", directive)
}
}
cfg.DefaultCache.Redis = provider
Expand All @@ -475,6 +502,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
switch directive {
case "exclude":
cfg.DefaultCache.Regex.Exclude = h.RemainingArgs()[0]
default:
return h.Errf("unsupported regex directive: %s", directive)
}
}
case "stale":
Expand Down Expand Up @@ -502,6 +531,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
d.Duration = ttl
}
timeout.Cache = d
default:
return h.Errf("unsupported timeout directive: %s", directive)
}
}
cfg.DefaultCache.Timeout = timeout
Expand All @@ -512,9 +543,7 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
cfg.DefaultCache.TTL.Duration = ttl
}
default:
if isBlocking {
return h.Errf("unsupported root directive: %s", rootOption)
}
return h.Errf("unsupported root directive: %s", rootOption)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/traefik/override/context/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

const (
CacheName ctxKey = "CACHE_NAME"
RequestCacheControl ctxKey = "REQUEST_CACHE_CONTROL"
CacheName ctxKey = "souin_ctx.CACHE_NAME"
RequestCacheControl ctxKey = "souin_ctx.REQUEST_CACHE_CONTROL"
)

var defaultCacheName string = "Souin"
Expand Down
6 changes: 3 additions & 3 deletions plugins/traefik/override/context/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

const (
GraphQL ctxKey = "GRAPHQL"
HashBody ctxKey = "HASH_BODY"
IsMutationRequest ctxKey = "IS_MUTATION_REQUEST"
GraphQL ctxKey = "souin_ctx.GRAPHQL"
HashBody ctxKey = "souin_ctx.HASH_BODY"
IsMutationRequest ctxKey = "souin_ctx.IS_MUTATION_REQUEST"
)

type graphQLContext struct {
Expand Down
6 changes: 3 additions & 3 deletions plugins/traefik/override/context/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

const (
Key ctxKey = "CACHE_KEY"
DisplayableKey ctxKey = "DISPLAYABLE_KEY"
IgnoredHeaders ctxKey = "IGNORE_HEADERS"
Key ctxKey = "souin_ctx.CACHE_KEY"
DisplayableKey ctxKey = "souin_ctx.DISPLAYABLE_KEY"
IgnoredHeaders ctxKey = "souin_ctx.IGNORE_HEADERS"
)

type keyContext struct {
Expand Down
2 changes: 1 addition & 1 deletion plugins/traefik/override/context/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/darkweak/souin/configurationtypes"
)

const SupportedMethod ctxKey = "SUPPORTED_METHOD"
const SupportedMethod ctxKey = "souin_ctx.SUPPORTED_METHOD"

var defaultVerbs []string = []string{http.MethodGet, http.MethodHead}

Expand Down
4 changes: 2 additions & 2 deletions plugins/traefik/override/context/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

const (
TimeoutCache ctxKey = "TIMEOUT_CACHE"
TimeoutCancel ctxKey = "TIMEOUT_CANCEL"
TimeoutCache ctxKey = "souin_ctx.TIMEOUT_CACHE"
TimeoutCancel ctxKey = "souin_ctx.TIMEOUT_CANCEL"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion plugins/traefik/override/context/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type (
}
)

const CacheControlCtx ctxKey = "CACHE-CONTROL-CTX"
const CacheControlCtx ctxKey = "souin_ctx.CACHE-CONTROL-CTX"

func GetContext() *Context {
return &Context{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a6b6a14

Please sign in to comment.