Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nginx conf #1130

Merged
merged 23 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
httpserver spec support wildcards
  • Loading branch information
suchen-sci committed Oct 31, 2023
commit 52cddb082468fd94b15c3ce974873b6429bbee5c
21 changes: 21 additions & 0 deletions pkg/object/httpserver/routers/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"net/url"
"regexp"
"strings"

"github.com/megaease/easegress/v2/pkg/logger"
"github.com/megaease/easegress/v2/pkg/util/ipfilter"
Expand All @@ -38,6 +39,8 @@ type Paths []*Path
type Host struct {
IsRegexp bool `json:"isRegexp" jsonschema:"omitempty"`
Value string `json:"value" jsonschema:"required"`
prefix string `json:"-"`
suffix string `json:"-"`
re *regexp.Regexp
}

Expand Down Expand Up @@ -124,6 +127,20 @@ func (rule *Rule) Init() {
for i := range rule.Hosts {
h := &rule.Hosts[i]
if !h.IsRegexp {
if h.Value != "" {
count := strings.Count(h.Value, "*")
if count > 1 {
logger.Errorf("invalid host %s, only one wildcard is allowed", h.Value)
continue
}
if h.Value[0] == '*' {
h.suffix = h.Value[1:]
} else if h.Value[len(h.Value)-1] == '*' {
h.prefix = h.Value[:len(h.Value)-1]
} else {
logger.Errorf("invalid host %s, only wildcard prefix or suffix is allowed", h.Value)
}
}
continue
}
if re, err := regexp.Compile(h.Value); err != nil {
Expand Down Expand Up @@ -154,6 +171,10 @@ func (rule *Rule) MatchHost(ctx *RouteContext) bool {
}
} else if host == h.Value {
return true
} else if h.prefix != "" && strings.HasPrefix(host, h.prefix) {
return true
} else if h.suffix != "" && strings.HasSuffix(host, h.suffix) {
return true
}
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/object/httpserver/routers/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,29 @@ func TestRuleMatch(t *testing.T) {
rule.Init()
assert.NotNil(rule)
assert.False(rule.MatchHost(ctx))

testCases := []struct {
request string
value string
result bool
}{
{request: "http:https://www.megaease.com:8080", value: "www.megaease.com", result: true},
{request: "http:https://www.megaease.com:8080", value: "*.megaease.com", result: true},
{request: "http:https://www.sub.megaease.com:8080", value: "*.megaease.com", result: true},
{request: "http:https://www.example.megaease.com:8080", value: "*.megaease.com", result: true},
{request: "http:https://www.megaease.com:8080", value: "www.megaease.*", result: true},
{request: "http:https://www.megaease.cn:8080", value: "www.megaease.*", result: true},
{request: "http:https://www.google.com:8080", value: "*.megaease.com", result: false},
}
for _, tc := range testCases {
stdr, _ := http.NewRequest(http.MethodGet, tc.request, nil)
req, _ := httpprot.NewRequest(stdr)
ctx := NewContext(req)

rule = &Rule{Hosts: []Host{{Value: tc.value}}}
rule.Init()
assert.Equal(tc.result, rule.MatchHost(ctx))
}
}

func TestRuleAllowIP(t *testing.T) {
Expand Down