Skip to content

Commit

Permalink
fix(chore/multiple-globals): bypass ignored global, redis waste, max-…
Browse files Browse the repository at this point in the history
…age not honored with ttl (#430)

* fix(chore/multiple-globals): bypass ignored global, redis waste, max-age not honored with ttl

* fix(middleware): handle properly request context done and empty response

* fix(chore): POST request rewrite body

* fix(middleware): handle vary coalescing when contacting upstream

* fix(plugins): bump rr deps

* fix(plugins): Tyk context sync

* fix(plugins): Go-Zero

* fix(plugins): Beego & Tyk

* fix(ci): remove Skipper from plugins tests
  • Loading branch information
darkweak authored Jan 2, 2024
1 parent c7183b1 commit 7fb48f5
Show file tree
Hide file tree
Showing 22 changed files with 428 additions and 425 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,6 @@ jobs:
CAPITALIZED_NAME: Roadrunner
LOWER_NAME: roadrunner
GO_VERSION: '1.21'
build-skipper-validator:
name: Check that Souin build as middleware
uses: ./.github/workflows/plugin_template.yml
secrets: inherit
with:
CAPITALIZED_NAME: Skipper
LOWER_NAME: skipper
GO_VERSION: '1.21'
build-souin-validator:
name: Check that Souin build as middleware
uses: ./.github/workflows/plugin_template.yml
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/workflow_plugins_generator.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

plugins=("beego" "chi" "dotweb" "echo" "fiber" "gin" "goa" "go-zero" "hertz" "kratos" "roadrunner" "skipper" "souin" "traefik" "tyk" "webgo")
plugins=("beego" "chi" "dotweb" "echo" "fiber" "gin" "goa" "go-zero" "hertz" "kratos" "roadrunner" "souin" "traefik" "tyk" "webgo")
go_version=1.21

IFS= read -r -d '' tpl <<EOF
Expand Down
4 changes: 4 additions & 0 deletions context/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type cacheContext struct {
cacheName string
}

func (*cacheContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request {
return req
}

func (cc *cacheContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) {
cc.cacheName = defaultCacheName
if c.GetDefaultCache().GetCacheName() != "" {
Expand Down
26 changes: 26 additions & 0 deletions context/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ type graphQLContext struct {
custom bool
}

func (g *graphQLContext) SetContextWithBaseRequest(req *http.Request, baseRq *http.Request) *http.Request {
ctx := req.Context()
ctx = context.WithValue(ctx, GraphQL, g.custom)
ctx = context.WithValue(ctx, HashBody, "")
ctx = context.WithValue(ctx, IsMutationRequest, false)

if g.custom && req.Body != nil {
b := bytes.NewBuffer([]byte{})
_, _ = io.Copy(b, req.Body)
req.Body = io.NopCloser(b)
baseRq.Body = io.NopCloser(b)

if b.Len() > 0 {
if isMutation(b.Bytes()) {
ctx = context.WithValue(ctx, IsMutationRequest, true)
} else {
h := sha256.New()
h.Write(b.Bytes())
ctx = context.WithValue(ctx, HashBody, fmt.Sprintf("-%x", h.Sum(nil)))
}
}
}

return req.WithContext(ctx)
}

func (g *graphQLContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) {
if len(c.GetDefaultCache().GetAllowedHTTPVerbs()) != 0 {
g.custom = true
Expand Down
4 changes: 4 additions & 0 deletions context/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type keyContext struct {
overrides []map[*regexp.Regexp]keyContext
}

func (*keyContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request {
return req
}

func (g *keyContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) {
k := c.GetDefaultCache().GetKey()
g.disable_body = k.DisableBody
Expand Down
4 changes: 4 additions & 0 deletions context/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type methodContext struct {
custom bool
}

func (*methodContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request {
return req
}

func (m *methodContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) {
m.allowedVerbs = defaultVerbs
if len(c.GetDefaultCache().GetAllowedHTTPVerbs()) != 0 {
Expand Down
6 changes: 5 additions & 1 deletion context/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ type ModeContext struct {
Strict, Bypass_request, Bypass_response bool
}

func (*ModeContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request {
return req
}

func (mc *ModeContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) {
mode := c.GetDefaultCache().GetMode()
mc.Bypass_request = mode == "bypass" || mode == "bypass_request"
Expand All @@ -25,4 +29,4 @@ func (mc *ModeContext) SetContext(req *http.Request) *http.Request {
return req.WithContext(context.WithValue(req.Context(), Mode, mc))
}

var _ ctx = (*cacheContext)(nil)
var _ ctx = (*ModeContext)(nil)
4 changes: 4 additions & 0 deletions context/now.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const Now ctxKey = "souin_ctx.NOW"

type nowContext struct{}

func (*nowContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request {
return req
}

func (cc *nowContext) SetupContext(_ configurationtypes.AbstractConfigurationInterface) {}

func (cc *nowContext) SetContext(req *http.Request) *http.Request {
Expand Down
6 changes: 5 additions & 1 deletion context/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type timeoutContext struct {
timeoutCache, timeoutBackend time.Duration
}

func (*timeoutContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request {
return req
}

func (t *timeoutContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) {
t.timeoutBackend = defaultTimeoutBackend
t.timeoutCache = defaultTimeoutCache
Expand All @@ -40,4 +44,4 @@ func (t *timeoutContext) SetContext(req *http.Request) *http.Request {
return req.WithContext(context.WithValue(context.WithValue(ctx, TimeoutCancel, cancel), TimeoutCache, t.timeoutCache))
}

var _ ctx = (*cacheContext)(nil)
var _ ctx = (*timeoutContext)(nil)
5 changes: 3 additions & 2 deletions context/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type (
ctx interface {
SetupContext(c configurationtypes.AbstractConfigurationInterface)
SetContext(req *http.Request) *http.Request
SetContextWithBaseRequest(req *http.Request, baseRq *http.Request) *http.Request
}

Context struct {
Expand Down Expand Up @@ -53,6 +54,6 @@ func (c *Context) SetBaseContext(req *http.Request) *http.Request {
return c.Mode.SetContext(c.Timeout.SetContext(c.Method.SetContext(c.CacheName.SetContext(c.Now.SetContext(req)))))
}

func (c *Context) SetContext(req *http.Request) *http.Request {
return c.Key.SetContext(c.GraphQL.SetContext(req))
func (c *Context) SetContext(req *http.Request, baseRq *http.Request) *http.Request {
return c.Key.SetContext(c.GraphQL.SetContextWithBaseRequest(req, baseRq))
}
2 changes: 1 addition & 1 deletion context/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test_Context_SetContext(t *testing.T) {

co.Init(&c)
req := httptest.NewRequest(http.MethodGet, "http://domain.com", nil)
req = co.SetContext(req)
req = co.SetContext(req, req)
if req.Context().Value(Key) != "GET-http-domain.com-" {
t.Errorf("The Key context must be equal to GET-http-domain.com-, %s given.", req.Context().Value(Key))
}
Expand Down
Loading

0 comments on commit 7fb48f5

Please sign in to comment.