Skip to content

Commit

Permalink
Expose route for context
Browse files Browse the repository at this point in the history
  • Loading branch information
xxx7xxxx committed Nov 1, 2023
1 parent 202f38e commit 72332c6
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
12 changes: 12 additions & 0 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"runtime/debug"

"github.com/megaease/easegress/v2/pkg/logger"
"github.com/megaease/easegress/v2/pkg/object/httpserver/routers"
"github.com/megaease/easegress/v2/pkg/protocols"
"github.com/megaease/easegress/v2/pkg/tracing"
)
Expand Down Expand Up @@ -73,6 +74,7 @@ type Context struct {

activeNs string

route routers.Route
requests map[string]*requestRef
responses map[string]*responseRef

Expand All @@ -92,6 +94,16 @@ func New(span *tracing.Span) *Context {
return ctx
}

// SetRoute sets the route.
func (ctx *Context) SetRoute(route routers.Route) {
ctx.route = route
}

// GetRoute returns the route.
func (ctx *Context) GetRoute() routers.Route {
return ctx.route
}

// Span returns the span of this Context.
func (ctx *Context) Span() *tracing.Span {
return ctx.span
Expand Down
13 changes: 7 additions & 6 deletions pkg/filters/redirectorv2/redirectorv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/megaease/easegress/v2/pkg/context"
"github.com/megaease/easegress/v2/pkg/filters"
"github.com/megaease/easegress/v2/pkg/logger"
"github.com/megaease/easegress/v2/pkg/protocols/httpprot"

gwapis "sigs.k8s.io/gateway-api/apis/v1beta1"
Expand Down Expand Up @@ -76,7 +77,6 @@ type (
Spec struct {
filters.BaseSpec `json:",inline"`

PathPrefix *string `json:"pathPrefix,omitempty"`
gwapis.HTTPRequestRedirectFilter `json:",inline"`
}
)
Expand Down Expand Up @@ -108,10 +108,6 @@ func (s *Spec) Validate() error {
return errors.New("invalid path of Redirector, replaceFullPath can't be empty")
}
case gwapis.PrefixMatchHTTPPathModifier:
if s.PathPrefix == nil || *s.PathPrefix == "" {
return errors.New("invalid path of Redirector, pathPrefix can't be empty")
}

if s.Path.ReplacePrefixMatch == nil {
return errors.New("invalid path of Redirector, replacePrefixMatch can't be empty")
}
Expand Down Expand Up @@ -178,8 +174,13 @@ func (r *Redirector) Handle(ctx *context.Context) string {
case gwapis.FullPathHTTPPathModifier:
redirectURL.Path = string(*r.spec.Path.ReplaceFullPath)
case gwapis.PrefixMatchHTTPPathModifier:
prefix := ctx.GetRoute().GetPathPrefix()
if prefix == "" {
logger.Warnf("route %+v has no path prefix", ctx.GetRoute())
}

redirectURL.Path = r.subPrefix(redirectURL.Path,
*r.spec.PathPrefix, string(*r.spec.Path.ReplacePrefixMatch))
prefix, string(*r.spec.Path.ReplacePrefixMatch))
}
}

Expand Down
14 changes: 11 additions & 3 deletions pkg/filters/redirectorv2/redirectorv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ import (
"testing"

"github.com/megaease/easegress/v2/pkg/context"
"github.com/megaease/easegress/v2/pkg/object/httpserver/routers"
"github.com/megaease/easegress/v2/pkg/protocols/httpprot"
"github.com/stretchr/testify/assert"

gwapis "sigs.k8s.io/gateway-api/apis/v1beta1"
)

type fakeMuxPath struct {
routers.Path
}

func (mp *fakeMuxPath) Rewrite(context *routers.RouteContext) {}

func TestSpecValidate(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -123,7 +130,7 @@ func TestSpecValidate(t *testing.T) {
},
},
expectErr: true,
errMsg: "invalid path of Redirector, pathPrefix can't be empty",
errMsg: "invalid path of Redirector, replacePrefixMatch can't be empty",
},
}

Expand Down Expand Up @@ -169,7 +176,6 @@ func TestRedirectorHandle2(t *testing.T) {
name: "Redirect prefix",
reqURL: "http://localhost/user/data/profile",
spec: &Spec{
PathPrefix: new(string),
HTTPRequestRedirectFilter: gwapis.HTTPRequestRedirectFilter{
Path: &gwapis.HTTPPathModifier{
Type: gwapis.PrefixMatchHTTPPathModifier,
Expand Down Expand Up @@ -232,13 +238,15 @@ func TestRedirectorHandle2(t *testing.T) {
req, _ := httpprot.NewRequest(stdReq)
ctx := context.New(nil)
ctx.SetInputRequest(req)
ctx.SetRoute(&fakeMuxPath{
Path: routers.Path{PathPrefix: "/user/"},
})

if tt.spec.Path != nil && tt.spec.Path.ReplaceFullPath != nil {
*tt.spec.Path.ReplaceFullPath = "/newpath"
}

if tt.spec.Path != nil && tt.spec.Path.ReplacePrefixMatch != nil {
*tt.spec.PathPrefix = "/user/"
*tt.spec.Path.ReplacePrefixMatch = "/account/"
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/object/httpserver/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ func (mi *muxInstance) putRouteToCache(req *httpprot.Request, rc *cachedRoute) {
}

func newMux(httpStat *httpstat.HTTPStat, topN *httpstat.TopN,
metrics *metrics, mapper context.MuxMapper) *mux {
metrics *metrics, mapper context.MuxMapper,
) *mux {
m := &mux{
httpStat: httpStat,
topN: topN,
Expand Down Expand Up @@ -267,6 +268,8 @@ func (mi *muxInstance) serveHTTP(stdw http.ResponseWriter, stdr *http.Request) {

routeCtx := routers.NewContext(req)
route := mi.search(routeCtx)
ctx.SetRoute(route.route)

var respHeader http.Header

defer func() {
Expand Down
10 changes: 10 additions & 0 deletions pkg/object/httpserver/routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ type (
GetBackend() string
// GetClientMaxBodySize is used to get the clientMaxBodySize corresponding to the route.
GetClientMaxBodySize() int64

// NOTE: Currently we only support path information in readonly.
// Without further requirements, we choose not to expose too much information.

// GetExactPath is used to get the exact path corresponding to the route.
GetExactPath() string
// GetPathPrefix is used to get the path prefix corresponding to the route.
GetPathPrefix() string
// GetPathRegexp is used to get the path regexp corresponding to the route.
GetPathRegexp() string
}

// Params are used to store the variables in the search path and their corresponding values.
Expand Down
15 changes: 15 additions & 0 deletions pkg/object/httpserver/routers/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ func (p *Path) GetClientMaxBodySize() int64 {
return p.ClientMaxBodySize
}

// GetExactPath returns the exact path of the route.
func (p *Path) GetExactPath() string {
return p.Path
}

// GetPathPrefix returns the path prefix of the route.
func (p *Path) GetPathPrefix() string {
return p.PathPrefix
}

// GetPathRegexp returns the path regexp of the route.
func (p *Path) GetPathRegexp() string {
return p.PathRegexp
}

func (hs Headers) init() {
for _, h := range hs {
if h.Regexp != "" {
Expand Down

0 comments on commit 72332c6

Please sign in to comment.