mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-13 22:52:20 +08:00
Compare commits
3 Commits
e5b52390b4
...
44bbb23fca
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44bbb23fca | ||
|
|
5fad976b37 | ||
|
|
8c587f3e59 |
24
gin.go
24
gin.go
@ -98,6 +98,10 @@ const (
|
||||
type Engine struct {
|
||||
RouterGroup
|
||||
|
||||
// routeTreesUpdated ensures that the initialization or update of the route trees
|
||||
// (used for routing HTTP requests) happens only once, even if called multiple times concurrently.
|
||||
routeTreesUpdated sync.Once
|
||||
|
||||
// RedirectTrailingSlash enables automatic redirection if the current route can't be matched but a
|
||||
// handler for the path with (without) the trailing slash exists.
|
||||
// For example if /foo/ is requested but a route only exists for /foo, the
|
||||
@ -137,10 +141,16 @@ type Engine struct {
|
||||
AppEngine bool
|
||||
|
||||
// UseRawPath if enabled, the url.RawPath will be used to find parameters.
|
||||
// The RawPath is only a hint, EscapedPath() should be use instead. (https://pkg.go.dev/net/url@master#URL)
|
||||
// Only use RawPath if you know what you are doing.
|
||||
UseRawPath bool
|
||||
|
||||
// UseEscapedPath if enable, the url.EscapedPath() will be used to find parameters
|
||||
// It overrides UseRawPath
|
||||
UseEscapedPath bool
|
||||
|
||||
// UnescapePathValues if true, the path value will be unescaped.
|
||||
// If UseRawPath is false (by default), the UnescapePathValues effectively is true,
|
||||
// If UseRawPath and UseEscapedPath are false (by default), the UnescapePathValues effectively is true,
|
||||
// as url.Path gonna be used, which is already unescaped.
|
||||
UnescapePathValues bool
|
||||
|
||||
@ -193,6 +203,7 @@ var _ IRouter = (*Engine)(nil)
|
||||
// - HandleMethodNotAllowed: false
|
||||
// - ForwardedByClientIP: true
|
||||
// - UseRawPath: false
|
||||
// - UseEscapedPath: false
|
||||
// - UnescapePathValues: true
|
||||
func New(opts ...OptionFunc) *Engine {
|
||||
debugPrintWARNINGNew()
|
||||
@ -210,6 +221,7 @@ func New(opts ...OptionFunc) *Engine {
|
||||
RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},
|
||||
TrustedPlatform: defaultPlatform,
|
||||
UseRawPath: false,
|
||||
UseEscapedPath: false,
|
||||
RemoveExtraSlash: false,
|
||||
UnescapePathValues: true,
|
||||
MaxMultipartMemory: defaultMultipartMemory,
|
||||
@ -654,6 +666,10 @@ func (engine *Engine) RunListener(listener net.Listener) (err error) {
|
||||
|
||||
// ServeHTTP conforms to the http.Handler interface.
|
||||
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
engine.routeTreesUpdated.Do(func() {
|
||||
engine.updateRouteTrees()
|
||||
})
|
||||
|
||||
c := engine.pool.Get().(*Context)
|
||||
c.writermem.reset(w)
|
||||
c.Request = req
|
||||
@ -681,7 +697,11 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
||||
httpMethod := c.Request.Method
|
||||
rPath := c.Request.URL.Path
|
||||
unescape := false
|
||||
if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {
|
||||
|
||||
if engine.UseEscapedPath {
|
||||
rPath = c.Request.URL.EscapedPath()
|
||||
unescape = engine.UnescapePathValues
|
||||
} else if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {
|
||||
rPath = c.Request.URL.RawPath
|
||||
unescape = engine.UnescapePathValues
|
||||
}
|
||||
|
||||
148
gin_test.go
148
gin_test.go
@ -720,6 +720,55 @@ func TestEngineHandleContextPreventsMiddlewareReEntry(t *testing.T) {
|
||||
assert.Equal(t, int64(1), handlerCounterV2)
|
||||
}
|
||||
|
||||
func TestEngineHandleContextUseEscapedPathPercentEncoded(t *testing.T) {
|
||||
r := New()
|
||||
r.UseEscapedPath = true
|
||||
r.UnescapePathValues = false
|
||||
|
||||
r.GET("/v1/:path", func(c *Context) {
|
||||
// Path is Escaped, the %25 is not interpreted as %
|
||||
assert.Equal(t, "foo%252Fbar", c.Param("path"))
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/v1/foo%252Fbar", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
func TestEngineHandleContextUseRawPathPercentEncoded(t *testing.T) {
|
||||
r := New()
|
||||
r.UseRawPath = true
|
||||
r.UnescapePathValues = false
|
||||
|
||||
r.GET("/v1/:path", func(c *Context) {
|
||||
// Path is used, the %25 is interpreted as %
|
||||
assert.Equal(t, "foo%2Fbar", c.Param("path"))
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/v1/foo%252Fbar", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
func TestEngineHandleContextUseEscapedPathOverride(t *testing.T) {
|
||||
r := New()
|
||||
r.UseEscapedPath = true
|
||||
r.UseRawPath = true
|
||||
r.UnescapePathValues = false
|
||||
|
||||
r.GET("/v1/:path", func(c *Context) {
|
||||
assert.Equal(t, "foo%25bar", c.Param("path"))
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
w := PerformRequest(r, http.MethodGet, "/v1/foo%25bar")
|
||||
assert.Equal(t, 200, w.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPrepareTrustedCIRDsWith(t *testing.T) {
|
||||
r := New()
|
||||
|
||||
@ -913,3 +962,102 @@ func TestMethodNotAllowedNoRoute(t *testing.T) {
|
||||
assert.NotPanics(t, func() { g.ServeHTTP(resp, req) })
|
||||
assert.Equal(t, http.StatusNotFound, resp.Code)
|
||||
}
|
||||
|
||||
// Test the fix for https://github.com/gin-gonic/gin/pull/4415
|
||||
func TestLiteralColonWithRun(t *testing.T) {
|
||||
SetMode(TestMode)
|
||||
router := New()
|
||||
|
||||
router.GET(`/test\:action`, func(c *Context) {
|
||||
c.JSON(http.StatusOK, H{"path": "literal_colon"})
|
||||
})
|
||||
|
||||
router.updateRouteTrees()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "literal_colon")
|
||||
}
|
||||
|
||||
func TestLiteralColonWithDirectServeHTTP(t *testing.T) {
|
||||
SetMode(TestMode)
|
||||
router := New()
|
||||
|
||||
router.GET(`/test\:action`, func(c *Context) {
|
||||
c.JSON(http.StatusOK, H{"path": "literal_colon"})
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "literal_colon")
|
||||
}
|
||||
|
||||
func TestLiteralColonWithHandler(t *testing.T) {
|
||||
SetMode(TestMode)
|
||||
router := New()
|
||||
|
||||
router.GET(`/test\:action`, func(c *Context) {
|
||||
c.JSON(http.StatusOK, H{"path": "literal_colon"})
|
||||
})
|
||||
|
||||
handler := router.Handler()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "literal_colon")
|
||||
}
|
||||
|
||||
func TestLiteralColonWithHTTPServer(t *testing.T) {
|
||||
SetMode(TestMode)
|
||||
router := New()
|
||||
|
||||
router.GET(`/test\:action`, func(c *Context) {
|
||||
c.JSON(http.StatusOK, H{"path": "literal_colon"})
|
||||
})
|
||||
|
||||
router.GET("/test/:param", func(c *Context) {
|
||||
c.JSON(http.StatusOK, H{"param": c.Param("param")})
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "literal_colon")
|
||||
|
||||
w2 := httptest.NewRecorder()
|
||||
req2, _ := http.NewRequest(http.MethodGet, "/test/foo", nil)
|
||||
router.ServeHTTP(w2, req2)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w2.Code)
|
||||
assert.Contains(t, w2.Body.String(), "foo")
|
||||
}
|
||||
|
||||
// Test that updateRouteTrees is called only once
|
||||
func TestUpdateRouteTreesCalledOnce(t *testing.T) {
|
||||
SetMode(TestMode)
|
||||
router := New()
|
||||
|
||||
router.GET(`/test\:action`, func(c *Context) {
|
||||
c.String(http.StatusOK, "ok")
|
||||
})
|
||||
|
||||
for range 5 {
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "ok", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user