Skip to content

Commit

Permalink
Merge pull request ipfs/kubo#7330 from cloudflare/fix/dir-listing-con…
Browse files Browse the repository at this point in the history
…tent-type

fix(gateway): ensure directory listings have Content-Type text/html

This commit was moved from ipfs/kubo@2380343
  • Loading branch information
Stebalien committed May 20, 2020
2 parents 184edbb + c6fc093 commit 8a01dd5
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions gateway/core/corehttp/gateway_handler.go
Expand Up @@ -291,6 +291,18 @@ 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
}

// 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
}
Expand Down Expand Up @@ -350,15 +362,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)
Expand Down

0 comments on commit 8a01dd5

Please sign in to comment.