From 5ebf897f6f3b86d9dc84273d5e1d3fefedbd8066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 22 Jun 2017 16:59:59 +0200 Subject: [PATCH] Show escaped url in gateway 404 message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/corehttp/gateway_handler.go | 9 +++++---- core/corehttp/gateway_test.go | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 113fe52b5ff..c7b4c6e6f0f 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -134,6 +134,7 @@ func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request) func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { urlPath := r.URL.Path + escapedURLPath := r.URL.EscapedPath() // If the gateway is behind a reverse proxy and mounted at a sub-path, // the prefix header can be set to signal this sub-path. @@ -173,12 +174,12 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr case nil: case coreiface.ErrOffline: if !i.node.OnlineMode() { - webError(w, "ipfs resolve -r "+urlPath, err, http.StatusServiceUnavailable) + webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusServiceUnavailable) return } fallthrough default: - webError(w, "ipfs resolve -r "+urlPath, err, http.StatusNotFound) + webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusNotFound) return } @@ -191,7 +192,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr case coreiface.ErrIsDir: dir = true default: - webError(w, "ipfs cat "+urlPath, err, http.StatusNotFound) + webError(w, "ipfs cat "+escapedURLPath, err, http.StatusNotFound) return } @@ -278,7 +279,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ixnd, err := dirr.Find(ctx, "index.html") switch { case err == nil: - log.Debugf("found index.html link for %s", urlPath) + log.Debugf("found index.html link for %s", escapedURLPath) dirwithoutslash := urlPath[len(urlPath)-1] != '/' goget := r.URL.Query().Get("go-get") == "1" diff --git a/core/corehttp/gateway_test.go b/core/corehttp/gateway_test.go index da60e8a65ac..3a4760e7174 100644 --- a/core/corehttp/gateway_test.go +++ b/core/corehttp/gateway_test.go @@ -140,6 +140,7 @@ func TestGatewayGet(t *testing.T) { {"localhost:5001", "/" + k, http.StatusNotFound, "404 page not found\n"}, {"localhost:5001", "/ipfs/" + k, http.StatusOK, "fnord"}, {"localhost:5001", "/ipns/nxdomain.example.com", http.StatusNotFound, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"}, + {"localhost:5001", "/ipns/%0D%0A%0D%0Ahello", http.StatusNotFound, "ipfs resolve -r /ipns/%0D%0A%0D%0Ahello: " + namesys.ErrResolveFailed.Error() + "\n"}, {"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"}, {"example.com", "/", http.StatusOK, "fnord"}, } {