Skip to content

Commit

Permalink
feat(gateway)!: do not send body when redirecting
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jun 1, 2023
1 parent 26c8643 commit 0a177e9
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 61 deletions.
27 changes: 0 additions & 27 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions gateway/handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
10 changes: 0 additions & 10 deletions gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 0 additions & 3 deletions gateway/handler_unixfs_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 2 additions & 9 deletions gateway/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 0a177e9

Please sign in to comment.