From 05625d2df485a2999f5dc27aa6c3235e017afc85 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 27 Dec 2022 23:59:02 +0300 Subject: [PATCH] util/http: trie support method not allowed Signed-off-by: Vasiliy Tolstov --- go.mod | 2 +- go.sum | 4 ++-- handler.go | 14 ++++++++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index defb117..d25b367 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module go.unistack.org/micro-server-http/v3 go 1.16 require ( - go.unistack.org/micro/v3 v3.9.17 + go.unistack.org/micro/v3 v3.9.18 golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 0dd7c25..0d399d1 100644 --- a/go.sum +++ b/go.sum @@ -74,8 +74,8 @@ github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQ go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.unistack.org/micro-proto/v3 v3.3.1 h1:nQ0MtWvP2G3QrpOgawVOPhpZZYkq6umTGDqs8FxJYIo= go.unistack.org/micro-proto/v3 v3.3.1/go.mod h1:cwRyv8uInM2I7EbU7O8Fx2Ls3N90Uw9UCCcq4olOdfE= -go.unistack.org/micro/v3 v3.9.17 h1:EJ9/XR9OTo/up/3aqWjaKS2YsWMA66b0dx+pc/0vIl8= -go.unistack.org/micro/v3 v3.9.17/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI= +go.unistack.org/micro/v3 v3.9.18 h1:FNsCCJJKyyP9gScc1K3HxGxFjub+c4ZQpL+QwVvIMro= +go.unistack.org/micro/v3 v3.9.18/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= diff --git a/handler.go b/handler.go index fbd6987..426e979 100644 --- a/handler.go +++ b/handler.go @@ -62,7 +62,7 @@ func (h *httpHandler) Options() server.HandlerOptions { func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { // check for http.HandlerFunc handlers - if ph, _, ok := h.pathHandlers.Search(r.Method, r.URL.Path); ok { + if ph, _, err := h.pathHandlers.Search(r.Method, r.URL.Path); err == nil { ph.(http.HandlerFunc)(w, r) return } @@ -106,8 +106,8 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { for _, shdlr := range h.handlers { hdlr := shdlr.(*httpHandler) - fh, mp, ok := hdlr.handlers.Search(r.Method, path) - if ok { + fh, mp, err := hdlr.handlers.Search(r.Method, path) + if err == nil { match = true for k, v := range mp { matches[k] = v @@ -115,6 +115,8 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { hldr = fh.(*patHandler) handler = hdlr break + } else if err == rhttp.ErrMethodNotAllowed && !h.registerRPC { + h.errorHandler(ctx, nil, w, r, fmt.Errorf("not matching route found"), http.StatusNotFound) } } @@ -125,8 +127,8 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { if len(serviceMethod) == 2 { if shdlr, ok := h.handlers[serviceMethod[0]]; ok { hdlr := shdlr.(*httpHandler) - fh, mp, ok := hdlr.handlers.Search(http.MethodPost, "/"+microMethod) - if ok { + fh, mp, err := hdlr.handlers.Search(http.MethodPost, "/"+microMethod) + if err == nil { match = true for k, v := range mp { matches[k] = v @@ -216,7 +218,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { // define the handler func fn := func(fctx context.Context, req server.Request, rsp interface{}) (err error) { - returnValues = function.Call([]reflect.Value{hldr.rcvr, hldr.mtype.prepareContext(fctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)}) + returnValues = function.Call([]reflect.Value{hldr.rcvr, hldr.mtype.prepareContext(fctx), argv, reflect.ValueOf(rsp)}) // The return value for the method is an error. if rerr := returnValues[0].Interface(); rerr != nil {