diff --git a/net/ghttp/ghttp_server_handler.go b/net/ghttp/ghttp_server_handler.go index a4f27dcfb30..09c66be3420 100644 --- a/net/ghttp/ghttp_server_handler.go +++ b/net/ghttp/ghttp_server_handler.go @@ -33,18 +33,6 @@ 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 { @@ -111,7 +99,10 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // Search the dynamic service handler. - request.handlers, request.serveHandler, request.hasHookHandler, request.hasServeHandler = s.getHandlersWithCache(request) + request.handlers, + request.serveHandler, + request.hasHookHandler, + request.hasServeHandler = s.getHandlersWithCache(request) // Check the service type static or dynamic for current request. if request.StaticFile != nil && request.StaticFile.IsDir && request.hasServeHandler { diff --git a/net/ghttp/ghttp_server_router_serve.go b/net/ghttp/ghttp_server_router_serve.go index 682c24da8fa..a4f5b814ac5 100644 --- a/net/ghttp/ghttp_server_router_serve.go +++ b/net/ghttp/ghttp_server_router_serve.go @@ -47,6 +47,18 @@ func (s *Server) getHandlersWithCache(r *Request) (parsedItems []*HandlerItemPar path = r.URL.Path host = r.GetHost() ) + // 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 != "" { + path = r.URL.RawPath + } // Special http method OPTIONS handling. // It searches the handler with the request method instead of OPTIONS method. if method == http.MethodOptions { diff --git a/net/ghttp/ghttp_z_unit_issue_test.go b/net/ghttp/ghttp_z_unit_issue_test.go index 02951453eeb..1af95fa636c 100644 --- a/net/ghttp/ghttp_z_unit_issue_test.go +++ b/net/ghttp/ghttp_z_unit_issue_test.go @@ -13,6 +13,7 @@ import ( "time" "github.com/gogf/gf/v2/encoding/gjson" + "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/test/gtest" @@ -420,3 +421,21 @@ func Test_Issue2890(t *testing.T) { ) }) } + +// https://github.com/gogf/gf/issues/2963 +func Test_Issue2963(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := g.Server(guid.S()) + s.SetServerRoot(gtest.DataPath("issue2963")) + s.SetDumpRouterMap(false) + s.Start() + defer s.Shutdown() + time.Sleep(100 * time.Millisecond) + + c := g.Client() + c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) + t.Assert(c.GetContent(ctx, "/1.txt"), `1`) + t.Assert(c.GetContent(ctx, "/中文G146(1)-icon.txt"), `中文G146(1)-icon`) + t.Assert(c.GetContent(ctx, "/"+gurl.Encode("中文G146(1)-icon.txt")), `中文G146(1)-icon`) + }) +} diff --git a/net/ghttp/testdata/issue2963/1.txt b/net/ghttp/testdata/issue2963/1.txt new file mode 100644 index 00000000000..56a6051ca2b --- /dev/null +++ b/net/ghttp/testdata/issue2963/1.txt @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git "a/net/ghttp/testdata/issue2963/\344\270\255\346\226\207G146(1)-icon.txt" "b/net/ghttp/testdata/issue2963/\344\270\255\346\226\207G146(1)-icon.txt" new file mode 100644 index 00000000000..5e3985dad0c --- /dev/null +++ "b/net/ghttp/testdata/issue2963/\344\270\255\346\226\207G146(1)-icon.txt" @@ -0,0 +1 @@ +中文G146(1)-icon \ No newline at end of file