diff --git a/gateway/handler.go b/gateway/handler.go index 744c37d002..45163d9b30 100644 --- a/gateway/handler.go +++ b/gateway/handler.go @@ -88,26 +88,6 @@ func NewHandler(c Config, api IPFSBackend) http.Handler { return newHandlerWithMetrics(c, api) } -// StatusResponseWriter enables us to override HTTP Status Code passed to -// WriteHeader function inside of http.ServeContent. Decision is based on -// presence of HTTP Headers such as Location. -type statusResponseWriter struct { - http.ResponseWriter -} - -func (sw *statusResponseWriter) WriteHeader(code int) { - // Check if we need to adjust Status Code to account for scheduled redirect - // This enables us to return payload along with HTTP 301 - // for subdomain redirect in web browsers while also returning body for cli - // tools which do not follow redirects by default (curl, wget). - redirect := sw.ResponseWriter.Header().Get("Location") - if redirect != "" && code == http.StatusOK { - code = http.StatusMovedPermanently - log.Debugw("subdomain redirect", "location", redirect, "status", code) - } - sw.ResponseWriter.WriteHeader(code) -} - // ServeContent replies to the request using the content in the provided ReadSeeker // and returns the status code written and any error encountered during a write. // It wraps http.ServeContent which takes care of If-None-Match+Etag, @@ -828,13 +808,6 @@ func handleIpnsB58mhToCidRedirection(w http.ResponseWriter, r *http.Request) boo return false } - if w.Header().Get("Location") != "" { - // Ignore this if there is already a redirection in place. This happens - // if there is a subdomain redirection. In that case, the path is already - // converted to CIDv1. - return false - } - pathParts := strings.Split(r.URL.Path, "/") if len(pathParts) < 3 { return false diff --git a/gateway/handler_codec.go b/gateway/handler_codec.go index 84524ada4f..af3a252c67 100644 --- a/gateway/handler_codec.go +++ b/gateway/handler_codec.go @@ -154,12 +154,6 @@ func (i *handler) renderCodec(ctx context.Context, w http.ResponseWriter, r *htt } func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r *http.Request, blockCid cid.Cid, blockData io.ReadSeekCloser, resolvedPath ipath.Resolved, contentPath ipath.Path) bool { - // If a redirect is setup (e.g. subdomains), do it and do not render the HTML. - if w.Header().Get("Location") != "" { - w.WriteHeader(http.StatusMovedPermanently) - return true - } - // WithHostname may have constructed an IPFS (or IPNS) path using the Host header. // In this case, we need the original path for constructing the redirect. requestURI, err := url.ParseRequestURI(r.RequestURI) @@ -240,9 +234,6 @@ func parseNode(blockCid cid.Cid, blockData io.ReadSeekCloser) *assets.ParsedNode // serveCodecRaw returns the raw block without any conversion func (i *handler) serveCodecRaw(ctx context.Context, w http.ResponseWriter, r *http.Request, blockData io.ReadSeekCloser, contentPath ipath.Path, name string, modtime, begin time.Time) bool { - // Special fix around redirects. - w = &statusResponseWriter{w} - // ServeContent will take care of // If-None-Match+Etag, Content-Length and range requests _, dataSent, _ := ServeContent(w, r, name, modtime, blockData) @@ -257,9 +248,6 @@ func (i *handler) serveCodecRaw(ctx context.Context, w http.ResponseWriter, r *h // serveCodecConverted returns payload converted to codec specified in toCodec func (i *handler) serveCodecConverted(ctx context.Context, w http.ResponseWriter, r *http.Request, blockCid cid.Cid, blockData io.ReadSeekCloser, contentPath ipath.Path, toCodec mc.Code, modtime, begin time.Time) bool { - // Special fix around redirects. - w = &statusResponseWriter{w} - codec := blockCid.Prefix().Codec decoder, err := multicodec.LookupDecoder(codec) if err != nil { diff --git a/gateway/handler_unixfs_dir.go b/gateway/handler_unixfs_dir.go index ec77b01005..1fac5afd95 100644 --- a/gateway/handler_unixfs_dir.go +++ b/gateway/handler_unixfs_dir.go @@ -108,16 +108,6 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r * return false } - // See statusResponseWriter.WriteHeader - // and https://github.com/ipfs/kubo/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") != "" { - logger.Debugw("location moved permanently", "status", http.StatusMovedPermanently) - w.WriteHeader(http.StatusMovedPermanently) - return true - } - // 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") diff --git a/gateway/handler_unixfs_file.go b/gateway/handler_unixfs_file.go index 296bef450a..fec73505c2 100644 --- a/gateway/handler_unixfs_file.go +++ b/gateway/handler_unixfs_file.go @@ -91,9 +91,6 @@ func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http. // (unifies behavior across gateways and web browsers) w.Header().Set("Content-Type", ctype) - // special fixup around redirects - w = &statusResponseWriter{w} - // ServeContent will take care of // If-None-Match+Etag, Content-Length and range requests _, dataSent, _ := ServeContent(w, r, name, modtime, content) diff --git a/gateway/hostname.go b/gateway/hostname.go index 7c72787b60..782b5c4ad8 100644 --- a/gateway/hostname.go +++ b/gateway/hostname.go @@ -60,15 +60,8 @@ func WithHostname(c Config, api IPFSBackend, next http.Handler) http.HandlerFunc return } if newURL != "" { - // Set "Location" header with redirect destination. - // It is ignored by curl in default mode, but will - // be respected by user agents that follow - // redirects by default, namely web browsers - w.Header().Set("Location", newURL) - - // Note: we continue regular gateway processing: - // HTTP Status Code http.StatusMovedPermanently - // will be set later, in statusResponseWriter + http.Redirect(w, r, newURL, http.StatusMovedPermanently) + return } }