Skip to content
Myr

feat(gin): add option to use escaped path (#4420)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Aeddis Desauw · committed by GitHub · parent 52ecf02 · Browse files · 2 files+63 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

2 files+63 added−2 removed

  • other:+63 added−2 removed

2 files+63 added−2 removed

modified:gin_test.go+49 added
718 unmodified lines
719 assert.Equal(t, int64(2), middlewareCounterV1)720 assert.Equal(t, int64(1), handlerCounterV2)721}722723func TestEngineHandleContextUseEscapedPathPercentEncoded(t *testing.T) {724 r := New()725 r.UseEscapedPath = true726 r.UnescapePathValues = false727728 r.GET("/v1/:path", func(c *Context) {729 // Path is Escaped, the %25 is not interpreted as %730 assert.Equal(t, "foo%252Fbar", c.Param("path"))731 c.Status(http.StatusOK)732 })733734 req := httptest.NewRequest(http.MethodGet, "/v1/foo%252Fbar", nil)735 w := httptest.NewRecorder()736 r.ServeHTTP(w, req)737}738739func TestEngineHandleContextUseRawPathPercentEncoded(t *testing.T) {740 r := New()741 r.UseRawPath = true742 r.UnescapePathValues = false743744 r.GET("/v1/:path", func(c *Context) {745 // Path is used, the %25 is interpreted as %746 assert.Equal(t, "foo%2Fbar", c.Param("path"))747 c.Status(http.StatusOK)748 })749750 req := httptest.NewRequest(http.MethodGet, "/v1/foo%252Fbar", nil)751 w := httptest.NewRecorder()752 r.ServeHTTP(w, req)753}754755func TestEngineHandleContextUseEscapedPathOverride(t *testing.T) {756 r := New()757 r.UseEscapedPath = true758 r.UseRawPath = true759 r.UnescapePathValues = false760761 r.GET("/v1/:path", func(c *Context) {762 assert.Equal(t, "foo%25bar", c.Param("path"))763 c.Status(http.StatusOK)764 })765766 assert.NotPanics(t, func() {767 w := PerformRequest(r, http.MethodGet, "/v1/foo%25bar")768 assert.Equal(t, 200, w.Code)769 })770}771772func TestPrepareTrustedCIRDsWith(t *testing.T) {773 r := New()774775 // valid ipv4 cidr
288 unmodified lines
modified:gin.go+14 added-2 removed
133 unmodified lines
134 // 'X-AppEngine...' for better integration with that PaaS.135 AppEngine bool136137 // UseRawPath if enabled, the url.RawPath will be used to find parameters.138 // The RawPath is only a hint, EscapedPath() should be use instead. (https://pkg.go.dev/net/url@master#URL)139 // Only use RawPath if you know what you are doing.140 UseRawPath bool141142 // UseEscapedPath if enable, the url.EscapedPath() will be used to find parameters143 // It overrides UseRawPath144 UseEscapedPath bool145146 // UnescapePathValues if true, the path value will be unescaped.141 // If UseRawPath is false (by default), the UnescapePathValues effectively is true,147 // If UseRawPath and UseEscapedPath are false (by default), the UnescapePathValues effectively is true,148 // as url.Path gonna be used, which is already unescaped.149 UnescapePathValues bool150151 // RemoveExtraSlash a parameter can be parsed from the URL even with extra slashes.
44 unmodified lines
196// - RedirectFixedPath: false197// - HandleMethodNotAllowed: false198// - ForwardedByClientIP: true199// - UseRawPath: false200// - UseEscapedPath: false201// - UnescapePathValues: true202func New(opts ...OptionFunc) *Engine {203 debugPrintWARNINGNew()204 engine := &Engine{
9 unmodified lines
214 ForwardedByClientIP: true,215 RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},216 TrustedPlatform: defaultPlatform,217 UseRawPath: false,218 UseEscapedPath: false,219 RemoveExtraSlash: false,220 UnescapePathValues: true,221 MaxMultipartMemory: defaultMultipartMemory,222 trees: make(methodTrees, 0, 9),
467 unmodified lines
690func (engine *Engine) handleHTTPRequest(c *Context) {691 httpMethod := c.Request.Method692 rPath := c.Request.URL.Path693 unescape := false694695 if engine.UseEscapedPath {696 rPath = c.Request.URL.EscapedPath()697 unescape = engine.UnescapePathValues686 if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {698 } else if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {699 rPath = c.Request.URL.RawPath700 unescape = engine.UnescapePathValues701 }702
130 unmodified lines