diff --git a/net/ghttp/ghttp_server_handler.go b/net/ghttp/ghttp_server_handler.go index 0d92527bb..952d8b9d4 100644 --- a/net/ghttp/ghttp_server_handler.go +++ b/net/ghttp/ghttp_server_handler.go @@ -33,7 +33,18 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { if s.config.ClientMaxBodySize > 0 { r.Body = http.MaxBytesReader(w, r.Body, s.config.ClientMaxBodySize) } - + // In case of, eg: + // Case 1: + // GET /net/http + // r.URL.Path : /net/http + // r.URL.RawPath : (empty string) + // Case 2: + // GET /net%2Fhttp + // r.URL.Path : /net/http + // r.URL.RawPath : /net%2Fhttp + if r.URL.RawPath != "" { + r.URL.Path = r.URL.RawPath + } // Rewrite feature checks. if len(s.config.Rewrites) > 0 { if rewrite, ok := s.config.Rewrites[r.URL.Path]; ok { diff --git a/net/ghttp/ghttp_server_router_serve.go b/net/ghttp/ghttp_server_router_serve.go index 048bec1af..76b9f02b3 100644 --- a/net/ghttp/ghttp_server_router_serve.go +++ b/net/ghttp/ghttp_server_router_serve.go @@ -13,6 +13,7 @@ import ( "strings" "github.com/gogf/gf/v2/container/glist" + "github.com/gogf/gf/v2/encoding/gurl" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/internal/intlog" "github.com/gogf/gf/v2/internal/json" @@ -179,7 +180,7 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*Han parsedItem.Values = make(map[string]string) // It there repeated names, it just overwrites the same one. for i, name := range item.Router.RegNames { - parsedItem.Values[name] = match[i+1] + parsedItem.Values[name], _ = gurl.Decode(match[i+1]) } } } diff --git a/net/ghttp/ghttp_z_unit_test.go b/net/ghttp/ghttp_z_unit_test.go index 06d0185fe..a004f25f5 100644 --- a/net/ghttp/ghttp_z_unit_test.go +++ b/net/ghttp/ghttp_z_unit_test.go @@ -13,6 +13,7 @@ import ( "testing" "time" + "github.com/gogf/gf/v2/encoding/gurl" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/genv" @@ -115,3 +116,26 @@ func Test_GetListenedAddressWithHost(t *testing.T) { t.Assert(fmt.Sprintf(`127.0.0.1:%d`, s.GetListenedPort()), s.GetListenedAddress()) }) } + +func Test_RoutePathParams(t *testing.T) { + s := g.Server(guid.S()) + s.BindHandler("/:param", func(r *ghttp.Request) { + r.Response.Write(r.Get("param"), ",", r.Get("c")) + }) + s.SetAddr("127.0.0.1:0") + s.SetDumpRouterMap(false) + s.Start() + defer s.Shutdown() + + time.Sleep(100 * time.Millisecond) + gtest.C(t, func(t *gtest.T) { + c := g.Client() + param := "net/http/get" + c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) + t.Assert(c.GetContent( + ctx, + "/"+gurl.Encode(param)+"?a=1&b=2&c="+gurl.Encode(param)), + "net/http/get,net/http/get", + ) + }) +}