mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
fix router parameters handling by auto url decoding (#2262)
improve router parameters handling by auto url decoding
This commit is contained in:
@ -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 {
|
||||
|
||||
@ -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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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",
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user