From dc7c64404d2c247ac092a88e3676494e9d3e94ad Mon Sep 17 00:00:00 2001 From: Bart Vercoulen Date: Mon, 12 Jul 2021 23:04:43 +0200 Subject: [PATCH] Add debug logs. --- router.go | 12 ++++++++++-- router_test.go | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/router.go b/router.go index a8210f4..f2554dd 100644 --- a/router.go +++ b/router.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "log" "net/http" + "net/url" "regexp" "sort" "time" @@ -89,7 +90,7 @@ func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { fullUrl := fmt.Sprintf("%s%s", req.URL.Host, req.URL.Path) - log.Println(fmt.Sprintf("Resolving header for %s", fullUrl)) + log.Println(fmt.Sprintf("Resolving for %s", fullUrl)) requestBody, err := json.Marshal(map[string]string{ "request": fullUrl, @@ -160,7 +161,13 @@ func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { if check.Match([]byte(path)) { newpath := check.ReplaceAll([]byte(path), t) - req.URL.Path = string(newpath) + req.URL.Path, err = url.PathUnescape(string(newpath)) + if err != nil { + log.Println("Could not rewrite Path.") + continue + } + log.Println(fmt.Sprintf("Apply rewrite: %s -> %s", path, string(newpath))) + req.RequestURI = req.URL.RequestURI() break } } @@ -169,6 +176,7 @@ func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { timeDiff := time.Now().Sub(startTime) log.Println(fmt.Sprintf("%s took %s", a.name, timeDiff)) } + a.next.ServeHTTP(rw, req) } diff --git a/router_test.go b/router_test.go index b9c8944..45400a3 100644 --- a/router_test.go +++ b/router_test.go @@ -182,6 +182,7 @@ func TestRouter(t *testing.T) { handler.ServeHTTP(recorder, req) assertCode(t, recorder, 404) + fmt.Println(req.RequestURI) } func assertHeader(t *testing.T, req *http.Request, key, expected string) {