api/router: avoid unneeded loops and fix path match (#1594)
* api/router: avoid unneeded loops and fix path match * if match found in google api path syntax, not try pcre loop * if path is not ending via $ sign, append it to pcre to avoid matching other strings like /api/account/register can be matched to /api/account * api: add tests and validations Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
930b329310
commit
750283cefd
11
registry.go
11
registry.go
@ -187,11 +187,14 @@ func (r *registryRouter) store(services []*registry.Service) {
|
|||||||
|
|
||||||
for _, p := range ep.Endpoint.Path {
|
for _, p := range ep.Endpoint.Path {
|
||||||
var pcreok bool
|
var pcreok bool
|
||||||
|
|
||||||
|
if p[0] == '^' && p[len(p)-1] != '$' {
|
||||||
pcrereg, err := regexp.CompilePOSIX(p)
|
pcrereg, err := regexp.CompilePOSIX(p)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cep.pcreregs = append(cep.pcreregs, pcrereg)
|
cep.pcreregs = append(cep.pcreregs, pcrereg)
|
||||||
pcreok = true
|
pcreok = true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rule, err := util.Parse(p)
|
rule, err := util.Parse(p)
|
||||||
if err != nil && !pcreok {
|
if err != nil && !pcreok {
|
||||||
@ -359,6 +362,9 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api gpath match %s = %v", path, pathreg)
|
||||||
|
}
|
||||||
pMatch = true
|
pMatch = true
|
||||||
ctx := req.Context()
|
ctx := req.Context()
|
||||||
md, ok := metadata.FromContext(ctx)
|
md, ok := metadata.FromContext(ctx)
|
||||||
@ -373,6 +379,7 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !pMatch {
|
||||||
// 4. try path via pcre path matching
|
// 4. try path via pcre path matching
|
||||||
for _, pathreg := range cep.pcreregs {
|
for _, pathreg := range cep.pcreregs {
|
||||||
if !pathreg.MatchString(req.URL.Path) {
|
if !pathreg.MatchString(req.URL.Path) {
|
||||||
@ -381,9 +388,13 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
|
logger.Debugf("api pcre path match %s != %v", path, pathreg)
|
||||||
|
}
|
||||||
pMatch = true
|
pMatch = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !pMatch {
|
if !pMatch {
|
||||||
continue
|
continue
|
||||||
|
Loading…
Reference in New Issue
Block a user