From fa1281002ef72f2b3c31be0c5c62d45bb9b4883e Mon Sep 17 00:00:00 2001 From: WithGJR Date: Thu, 16 Oct 2014 18:26:01 +0800 Subject: [PATCH 1/2] fix router bug: when the request is PUT or DELETE, router can't find the actual route and will throw 404 page to user --- router.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/router.go b/router.go index 0d51410b..7194bd29 100644 --- a/router.go +++ b/router.go @@ -648,7 +648,21 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) } if !findrouter { - if t, ok := p.routers[r.Method]; ok { + http_method := "" + + if r.Method == "POST" && context.Input.Query("_method") == "PUT" { + http_method = "PUT" + } + + if r.Method == "POST" && context.Input.Query("_method") == "DELETE" { + http_method = "DELETE" + } + + if http_method != "PUT" && http_method != "DELETE" { + http_method = r.Method + } + + if t, ok := p.routers[http_method]; ok { runObject, p := t.Match(urlPath) if r, ok := runObject.(*controllerInfo); ok { routerInfo = r From efc14a1e8df6e71540a75fd09c8002b0728c986f Mon Sep 17 00:00:00 2001 From: WithGJR Date: Thu, 16 Oct 2014 18:58:12 +0800 Subject: [PATCH 2/2] fix router bug with more better way --- router.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/router.go b/router.go index 7194bd29..2a88490d 100644 --- a/router.go +++ b/router.go @@ -648,20 +648,16 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) } if !findrouter { - http_method := "" + http_method := r.Method - if r.Method == "POST" && context.Input.Query("_method") == "PUT" { + if http_method == "POST" && context.Input.Query("_method") == "PUT" { http_method = "PUT" } - if r.Method == "POST" && context.Input.Query("_method") == "DELETE" { + if http_method == "POST" && context.Input.Query("_method") == "DELETE" { http_method = "DELETE" } - if http_method != "PUT" && http_method != "DELETE" { - http_method = r.Method - } - if t, ok := p.routers[http_method]; ok { runObject, p := t.Match(urlPath) if r, ok := runObject.(*controllerInfo); ok {