Skip to content

Commit

Permalink
optim code (easegress-io#659)
Browse files Browse the repository at this point in the history
* optim code

* optim string match
  • Loading branch information
suchen-sci committed Jun 17, 2022
1 parent 7126f58 commit e7c2694
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 34 deletions.
70 changes: 68 additions & 2 deletions pkg/filters/builder/requestbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"
"testing"

sprig "github.com/go-task/slim-sprig"
"github.com/megaease/easegress/pkg/context"
"github.com/megaease/easegress/pkg/filters"
"github.com/megaease/easegress/pkg/protocols/httpprot"
Expand Down Expand Up @@ -204,6 +205,70 @@ func TestRequestHeader(t *testing.T) {
}
}

func TestTemplateFuncs(t *testing.T) {
assert := assert.New(t)

// test functions in "github.com/go-task/slim-sprig"
{
yml := `
template: |
body: {{ hello }}
`
spec := &RequestBuilderSpec{}
yaml.Unmarshal([]byte(yml), spec)
rb := getRequestBuilder(spec)
defer rb.Close()

ctx := context.New(nil)
ctx.UseNamespace("test")

res := rb.Handle(ctx)
assert.Empty(res)
testReq := ctx.GetRequest("test").(*httpprot.Request)
assert.Equal(sprig.GenericFuncMap()["hello"].(func() string)(), string(testReq.RawPayload()))
}

{
yml := `
template: |
body: {{ lower "HELLO" }}
`
spec := &RequestBuilderSpec{}
yaml.Unmarshal([]byte(yml), spec)
rb := getRequestBuilder(spec)
defer rb.Close()

ctx := context.New(nil)
ctx.UseNamespace("test")

res := rb.Handle(ctx)
assert.Empty(res)
testReq := ctx.GetRequest("test").(*httpprot.Request)
assert.Equal(sprig.GenericFuncMap()["lower"].(func(s string) string)("HELLO"), string(testReq.RawPayload()))
assert.Equal("hello", string(testReq.RawPayload()))
}

// test functions in "github.com/go-task/slim-sprig"
{
yml := `
template: |
body: '{{ hello }} W{{ lower "ORLD"}}!'
`
spec := &RequestBuilderSpec{}
yaml.Unmarshal([]byte(yml), spec)
rb := getRequestBuilder(spec)
defer rb.Close()

ctx := context.New(nil)
ctx.UseNamespace("test")

res := rb.Handle(ctx)
assert.Empty(res)
testReq := ctx.GetRequest("test").(*httpprot.Request)
assert.Equal("Hello! World!", string(testReq.RawPayload()))
}
}

func TestRequestBody(t *testing.T) {
assert := assert.New(t)

Expand Down Expand Up @@ -291,7 +356,7 @@ func TestRequestBody(t *testing.T) {
yml = `template: |
method: Delete
url: http:https://www.facebook.com
body: body {{ .requests.request1.YAMLBody.field1 }} {{ .requests.request1.YAMLBody.field2 }}
body: body {{ .requests.request1.YAMLBody.field1.subfield }} {{ .requests.request1.YAMLBody.field2 }}
`
{
spec := &RequestBuilderSpec{}
Expand All @@ -302,7 +367,8 @@ func TestRequestBody(t *testing.T) {
ctx := context.New(nil)

req1, err := http.NewRequest(http.MethodDelete, "http:https://www.google.com", strings.NewReader(`
field1: value1
field1:
subfield: value1
field2: value2
`))
assert.Nil(err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/filters/headerlookup/headerlookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ func (hl *HeaderLookup) watchChanges() {
}
}
}()
return
}

// Close closes HeaderLookup.
Expand All @@ -268,7 +267,7 @@ func (hl *HeaderLookup) Handle(ctx *context.Context) string {
// this may need update later
if hl.spec.PathRegExp != "" {
path := req.Path()
if match := hl.pathRegExp.FindStringSubmatch(path); match != nil && len(match) > 1 {
if match := hl.pathRegExp.FindStringSubmatch(path); len(match) > 1 {
headerVal = headerVal + "-" + match[1]
}
}
Expand All @@ -278,7 +277,7 @@ func (hl *HeaderLookup) Handle(ctx *context.Context) string {
return ""
}
for hk, hv := range headersToAdd {
header.Set(http.CanonicalHeaderKey(hk), hv)
header.Set(hk, hv)
}
return ""
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/filters/proxy/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ type ServerPoolSpec struct {
SpanName string `yaml:"spanName" jsonschema:"omitempty"`
Filter *RequestMatcherSpec `yaml:"filter" jsonschema:"omitempty"`
ServerMaxBodySize int64 `yaml:"serverMaxBodySize" jsonschema:"omitempty"`
ServersTags []string `yaml:"serversTags" jsonschema:"omitempty,uniqueItems=true"`
ServerTags []string `yaml:"serverTags" jsonschema:"omitempty,uniqueItems=true"`
Servers []*Server `yaml:"servers" jsonschema:"omitempty"`
ServiceRegistry string `yaml:"serviceRegistry" jsonschema:"omitempty"`
ServiceName string `yaml:"serviceName" jsonschema:"omitempty"`
Expand Down Expand Up @@ -311,7 +311,7 @@ func (sp *ServerPool) useService(instances map[string]*serviceregistry.ServiceIn
servers := make([]*Server, 0)

for _, instance := range instances {
for _, tag := range sp.spec.ServersTags {
for _, tag := range sp.spec.ServerTags {
if stringtool.StrInSlice(tag, instance.Tags) {
servers = append(servers, &Server{
URL: instance.URL(),
Expand All @@ -325,7 +325,7 @@ func (sp *ServerPool) useService(instances map[string]*serviceregistry.ServiceIn

if len(servers) == 0 {
msgFmt := "%s/%s: no service instance satisfy tags: %v"
logger.Warnf(msgFmt, sp.spec.ServiceRegistry, sp.spec.ServiceName, sp.spec.ServersTags)
logger.Warnf(msgFmt, sp.spec.ServiceRegistry, sp.spec.ServiceName, sp.spec.ServerTags)
servers = sp.spec.Servers
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/filters/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const (
// Kind is the kind of Proxy.
Kind = "Proxy"

resultFallback = "fallback"
resultInternalError = "internalError"
resultClientError = "clientError"
resultServerError = "serverError"
Expand All @@ -54,7 +53,6 @@ var kind = &filters.Kind{
Name: Kind,
Description: "Proxy sets the proxy of proxy servers",
Results: []string{
resultFallback,
resultInternalError,
resultClientError,
resultServerError,
Expand Down
39 changes: 20 additions & 19 deletions pkg/filters/proxy/requestmatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,42 +179,33 @@ func (gm *generalMatcher) matchOneHeader(req *httpprot.Request) bool {
if rule.Match("") {
return true
}
continue
}

for _, v := range values {
if rule.Match(v) {
return true
} else {
for _, v := range values {
if rule.Match(v) {
return true
}
}
}
}

return false
}

func (gm *generalMatcher) matchAllHeader(req *httpprot.Request) bool {
h := req.HTTPHeader()

OUTER_LOOP:
for key, rule := range gm.headers {
values := h.Values(key)

if len(values) == 0 {
if rule.Match("") {
continue
if !rule.Match("") {
return false
}
return false
}

for _, v := range values {
if rule.Match(v) {
continue OUTER_LOOP
} else {
if !rule.MatchAny(values) {
return false
}
}

return false
}

return true
}

Expand Down Expand Up @@ -312,3 +303,13 @@ func (sm *StringMatcher) Match(value string) bool {

return sm.re.MatchString(value)
}

// MatchAny return true if any of the values matches.
func (sm *StringMatcher) MatchAny(values []string) bool {
for _, v := range values {
if sm.Match(v) {
return true
}
}
return false
}
2 changes: 0 additions & 2 deletions pkg/filters/requestadaptor/requestadaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const (
// Kind is the kind of RequestAdaptor.
Kind = "RequestAdaptor"

resultReadBodyFailed = "readBodyFailed"
resultDecompressFailed = "decompressFailed"
resultCompressFailed = "compressFailed"
)
Expand All @@ -45,7 +44,6 @@ var kind = &filters.Kind{
Results: []string{
resultDecompressFailed,
resultCompressFailed,
resultReadBodyFailed,
},
DefaultSpec: func() filters.Spec {
return &Spec{}
Expand Down
5 changes: 3 additions & 2 deletions pkg/protocols/protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ package protocols

import (
"io"
"strings"
)

var registry = map[string]Protocol{}

// Register registers a new protocol with name.
func Register(name string, p Protocol) {
registry[name] = p
registry[strings.ToUpper(name)] = p
}

// Get returns protocol by name.
func Get(name string) Protocol {
return registry[name]
return registry[strings.ToUpper(name)]
}

// Request is the protocol independent interface of a request.
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/signer/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import "time"

// Spec defines the configuration of a Signer
type Spec struct {
Literal *Literal `yaml:"literial,omitempty" json:"literial,omitempty" jsonschema:"omitempty"`
Literal *Literal `yaml:"literal,omitempty" json:"literal,omitempty" jsonschema:"omitempty"`
HeaderHoisting *HeaderHoisting `yaml:"headerHoisting,omitempty" json:"headerHoisting,omitempty" jsonschema:"omitempty"`
IgnoredHeaders []string `yaml:"ignoredHeaders" json:"ignoredHeaders" jsonschema:"omitempty,uniqueItems=true"`
ExcludeBody bool `yaml:"excludeBody" json:"excludeBody" jsonschema:"omitempty"`
Expand Down

0 comments on commit e7c2694

Please sign in to comment.