From 85cba15eadd784e7b7b75fb4a3046f0ccdf65e3d Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Wed, 20 May 2020 13:21:50 +0200 Subject: [PATCH 1/2] fix(gateway): fix status code for HEAD on redirects Report a consistent status code for HEAD requests that end up in a redirect. This commit was moved from ipfs/kubo@7c7888c4db5a0d552ec174687275549fdd0908fd --- gateway/core/corehttp/gateway_handler.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index f5624f5a9..e24a999d5 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -291,6 +291,15 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return } + // See statusResponseWriter.WriteHeader + // and https://github.com/ipfs/go-ipfs/issues/7164 + // Note: this needs to occur before listingTemplate.Execute otherwise we get + // superfluous response.WriteHeader call from prometheus/client_golang + if w.Header().Get("Location") != "" { + w.WriteHeader(http.StatusMovedPermanently) + return + } + if r.Method == http.MethodHead { return } @@ -350,15 +359,6 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request Hash: hash, } - // See statusResponseWriter.WriteHeader - // and https://github.com/ipfs/go-ipfs/issues/7164 - // Note: this needs to occur before listingTemplate.Execute otherwise we get - // superfluous response.WriteHeader call from prometheus/client_golang - if w.Header().Get("Location") != "" { - w.WriteHeader(http.StatusMovedPermanently) - return - } - err = listingTemplate.Execute(w, tplData) if err != nil { internalWebError(w, err) From c6fc093eea96657cf58dfcf649663ab38c15e809 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 18 May 2020 13:58:09 +0200 Subject: [PATCH 2/2] fix(gateway): ensure directory listings have Content-Type text/html Files already have an explicit Content-Type set. Be sure to do this for directory listings as well to avoid a fallback to autodetection in net/http. That fallback fails when a ResponseWriter is installed that performs compression. This commit was moved from ipfs/kubo@d4952f2a73b2dd33d8c35ab1bae3bc381fdb5088 --- gateway/core/corehttp/gateway_handler.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index e24a999d5..78afb2bf7 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -300,6 +300,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return } + // A HTML directory index will be presented, be sure to set the correct + // type instead of relying on autodetection (which may fail). + w.Header().Set("Content-Type", "text/html") if r.Method == http.MethodHead { return }