Skip to content

Commit

Permalink
The spec of the httpserver supports the AND-OR strategy of the header…
Browse files Browse the repository at this point in the history
…'s match (#613)
  • Loading branch information
sodaRyCN authored and localvar committed Jun 13, 2022
1 parent 146490d commit 3376c03
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
50 changes: 33 additions & 17 deletions pkg/object/httpserver/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type (
backend string
headers []*Header
clientMaxBodySize int64
mathAllHeader bool
}

route struct {
Expand Down Expand Up @@ -222,16 +223,16 @@ func newMuxPath(parentIPFilters *ipfilter.IPFilters, path *Path) *MuxPath {
ipFilter: newIPFilter(path.IPFilter),
ipFilterChain: newIPFilterChain(parentIPFilters, path.IPFilter),

path: path.Path,
pathPrefix: path.PathPrefix,
pathRegexp: path.PathRegexp,
pathRE: pathRE,
rewriteTarget: path.RewriteTarget,
methods: path.Methods,
backend: path.Backend,
headers: path.Headers,

path: path.Path,
pathPrefix: path.PathPrefix,
pathRegexp: path.PathRegexp,
pathRE: pathRE,
rewriteTarget: path.RewriteTarget,
methods: path.Methods,
backend: path.Backend,
headers: path.Headers,
clientMaxBodySize: path.ClientMaxBodySize,
mathAllHeader: path.MatchAllHeader,
}
}

Expand Down Expand Up @@ -286,18 +287,33 @@ func (mp *MuxPath) matchMethod(r *httpprot.Request) bool {
}

func (mp *MuxPath) matchHeaders(r *httpprot.Request) bool {
for _, h := range mp.headers {
v := r.HTTPHeader().Get(h.Key)
if stringtool.StrInSlice(v, h.Values) {
return true
if mp.mathAllHeader {
for _, h := range mp.headers {
v := r.HTTPHeader().Get(h.Key)
if !stringtool.StrInSlice(v, h.Values) {
return false
}

if h.Regexp != "" && !h.headerRE.MatchString(v) {
return false
}
}

if h.Regexp != "" && h.headerRE.MatchString(v) {
return true
return true
} else {
for _, h := range mp.headers {
v := r.HTTPHeader().Get(h.Key)
if stringtool.StrInSlice(v, h.Values) {
return true
}

if h.Regexp != "" && h.headerRE.MatchString(v) {
return true
}
}
}

return false
return false
}
}

func newMux(httpStat *httpstat.HTTPStat, topN *httpstat.TopN, mapper context.MuxMapper) *mux {
Expand Down
1 change: 1 addition & 0 deletions pkg/object/httpserver/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type (
Backend string `yaml:"backend" jsonschema:"required"`
Headers []*Header `yaml:"headers" jsonschema:"omitempty"`
ClientMaxBodySize int64 `yaml:"clientMaxBodySize" jsonschema:"omitempty"`
MatchAllHeader bool `yaml:"MatchAllHeader" jsonschema:"omitempty"`
}

// Header is the third level entry of router. A header entry is always under a specific path entry, that is to mean
Expand Down

0 comments on commit 3376c03

Please sign in to comment.