api/router: support pcre and google.api pattern matching (#1549)

* api/router: support pcre and google.api pattern matching

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-04-19 00:31:34 +03:00
parent 2f5eaa0127
commit 930b329310

View File

@ -23,6 +23,7 @@ import (
type endpoint struct { type endpoint struct {
hostregs []*regexp.Regexp hostregs []*regexp.Regexp
pathregs []util.Pattern pathregs []util.Pattern
pcreregs []*regexp.Regexp
} }
// router is the default router // router is the default router
@ -185,13 +186,23 @@ func (r *registryRouter) store(services []*registry.Service) {
} }
for _, p := range ep.Endpoint.Path { for _, p := range ep.Endpoint.Path {
var pcreok bool
pcrereg, err := regexp.CompilePOSIX(p)
if err == nil {
cep.pcreregs = append(cep.pcreregs, pcrereg)
pcreok = true
}
rule, err := util.Parse(p) rule, err := util.Parse(p)
if err != nil { if err != nil && !pcreok {
if logger.V(logger.TraceLevel, logger.DefaultLogger) { if logger.V(logger.TraceLevel, logger.DefaultLogger) {
logger.Tracef("endpoint have invalid path pattern: %v", err) logger.Tracef("endpoint have invalid path pattern: %v", err)
} }
continue continue
} else if err != nil && pcreok {
continue
} }
tpl := rule.Compile() tpl := rule.Compile()
pathreg, err := util.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, "") pathreg, err := util.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, "")
if err != nil { if err != nil {
@ -303,11 +314,10 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
ep := e.Endpoint ep := e.Endpoint
var mMatch, hMatch, pMatch bool var mMatch, hMatch, pMatch bool
// 1. try method // 1. try method
methodLoop:
for _, m := range ep.Method { for _, m := range ep.Method {
if m == req.Method { if m == req.Method {
mMatch = true mMatch = true
break methodLoop break
} }
} }
if !mMatch { if !mMatch {
@ -321,15 +331,14 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
if len(ep.Host) == 0 { if len(ep.Host) == 0 {
hMatch = true hMatch = true
} else { } else {
hostLoop:
for idx, h := range ep.Host { for idx, h := range ep.Host {
if h == "" || h == "*" { if h == "" || h == "*" {
hMatch = true hMatch = true
break hostLoop break
} else { } else {
if cep.hostregs[idx].MatchString(req.URL.Host) { if cep.hostregs[idx].MatchString(req.URL.Host) {
hMatch = true hMatch = true
break hostLoop break
} }
} }
} }
@ -341,14 +350,12 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
logger.Debugf("api host match %s", req.URL.Host) logger.Debugf("api host match %s", req.URL.Host)
} }
// 3. try path // 3. try path via google.api path matching
// 3. try path
pathLoop:
for _, pathreg := range cep.pathregs { for _, pathreg := range cep.pathregs {
matches, err := pathreg.Match(path, "") matches, err := pathreg.Match(path, "")
if err != nil { if err != nil {
if logger.V(logger.DebugLevel, logger.DefaultLogger) { if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("api path not match %s != %v", path, pathreg) logger.Debugf("api gpath not match %s != %v", path, pathreg)
} }
continue continue
} }
@ -363,8 +370,21 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
} }
md["x-api-body"] = ep.Body md["x-api-body"] = ep.Body
*req = *req.Clone(metadata.NewContext(ctx, md)) *req = *req.Clone(metadata.NewContext(ctx, md))
break pathLoop break
} }
// 4. try path via pcre path matching
for _, pathreg := range cep.pcreregs {
if !pathreg.MatchString(req.URL.Path) {
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("api pcre path not match %s != %v", path, pathreg)
}
continue
}
pMatch = true
break
}
if !pMatch { if !pMatch {
continue continue
} }