diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 37fe1d5c5..4547efac8 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -135,11 +135,20 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Etag", etag) w.Header().Set("X-IPFS-Path", p) - w.Header().Set("Cache-Control", "public, max-age=29030400") dr, err := i.NewDagReader(nd) + if err != nil && err != uio.ErrIsDir { + // not a directory and still an error + internalWebError(w, err) + return + } + + // set these headers _after_ the error, for we may just not have it + // and dont want the client to cache a 500 response... + w.Header().Set("Etag", etag) + w.Header().Set("Cache-Control", "public, max-age=29030400") + if err == nil { defer dr.Close() _, name := path.Split(urlPath) @@ -149,12 +158,6 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - if err != uio.ErrIsDir { - // not a directory and still an error - internalWebError(w, err) - return - } - // storage for directory listing var dirListing []directoryItem // loop through files