Skip to content

Commit

Permalink
fix issue #2963 (#3062)
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored Oct 19, 2023
1 parent 93964ee commit acf2307
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
17 changes: 4 additions & 13 deletions net/ghttp/ghttp_server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions net/ghttp/ghttp_server_router_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions net/ghttp/ghttp_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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`)
})
}
1 change: 1 addition & 0 deletions net/ghttp/testdata/issue2963/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions net/ghttp/testdata/issue2963/中文G146(1)-icon.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
中文G146(1)-icon

0 comments on commit acf2307

Please sign in to comment.