Skip to content

Commit

Permalink
remove url decode from middleware, now handled by codegen bind (#2700)
Browse files Browse the repository at this point in the history
  • Loading branch information
woutslakhorst authored Dec 19, 2023
1 parent 43a46b7 commit a022c1d
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 61 deletions.
22 changes: 0 additions & 22 deletions http/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
Expand Down Expand Up @@ -205,24 +204,6 @@ func (h *Engine) Shutdown() error {
return h.server.Shutdown(context.Background())
}

// decodeURIPath is echo middleware that decodes path parameters
func decodeURIPath(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// FIXME: This is a hack because of https://github.com/labstack/echo/issues/1258
newValues := make([]string, len(c.ParamValues()))
for i, value := range c.ParamValues() {
path, err := url.PathUnescape(value)
if err != nil {
path = value
}
newValues[i] = path
}
c.SetParamNames(c.ParamNames()...)
c.SetParamValues(newValues...)
return next(c)
}
}

// matchesPath checks whether the request URI path hierarchically matches the given path.
// Examples:
// / matches /
Expand All @@ -244,9 +225,6 @@ func matchesPath(requestURI string, path string) bool {
}

func (h Engine) applyGlobalMiddleware(echoServer core.EchoRouter, serverConfig core.ServerConfig) {
// Use middleware to decode URL encoded path parameters like did%3Anuts%3A123 -> did:nuts:123
echoServer.Use(decodeURIPath)

// Always enabled in strict mode
if serverConfig.Strictmode || serverConfig.InternalRateLimiter {
echoServer.Use(newInternalRateLimiter(map[string][]string{
Expand Down
39 changes: 0 additions & 39 deletions http/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
Expand Down Expand Up @@ -690,44 +689,6 @@ func TestEngine_LoggingMiddleware(t *testing.T) {
})
}

func TestDecodeURIPath(t *testing.T) {
rawParam := "urn:oid:2.16.840.1.113883.2.4.6.1:87654321"
encodedParam := "urn%3Aoid%3A2.16.840.1.113883.2.4.6.1%3A87654321"

t.Run("without middleware, it returns the encoded param", func(t *testing.T) {
e := echo.New()
r := e.Router()
r.Add(http.MethodGet, "/api/:someparam", func(context echo.Context) error {
param := context.Param("someparam")
return context.Blob(200, "text/plain", []byte(param))
})

req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/%v", encodedParam), nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
defer rec.Result().Body.Close()
bodyBytes, _ := io.ReadAll(rec.Result().Body)
assert.Equal(t, encodedParam, string(bodyBytes))
})

t.Run("with middleware, it return the decoded param", func(t *testing.T) {
e := echo.New()
r := e.Router()
e.Use(decodeURIPath)
r.Add(http.MethodGet, "/api/:someparam", func(context echo.Context) error {
param := context.Param("someparam")
return context.Blob(200, "text/plain", []byte(param))
})

req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/%v", encodedParam), nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
defer rec.Result().Body.Close()
bodyBytes, _ := io.ReadAll(rec.Result().Body)
assert.Equal(t, rawParam, string(bodyBytes))
})
}

func assertServerStarted(t *testing.T, address string) {
t.Helper()
var err error
Expand Down

0 comments on commit a022c1d

Please sign in to comment.