Skip to content

Commit

Permalink
Revert "add special case for text/html+codec+range"
Browse files Browse the repository at this point in the history
This reverts commit 6fe39c8.
  • Loading branch information
hacdias committed Apr 11, 2024
1 parent ffd4fe8 commit 84c7d89
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
24 changes: 6 additions & 18 deletions gateway/handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func (i *handler) serveCodec(ctx context.Context, w http.ResponseWriter, r *http
return false
}

return i.renderCodec(ctx, w, r, rq, blockSize, data, false)
return i.renderCodec(ctx, w, r, rq, blockSize, data)
}

func (i *handler) renderCodec(ctx context.Context, w http.ResponseWriter, r *http.Request, rq *requestData, blockSize int64, blockData io.ReadSeekCloser, isRangeRequest bool) bool {
func (i *handler) renderCodec(ctx context.Context, w http.ResponseWriter, r *http.Request, rq *requestData, blockSize int64, blockData io.ReadSeekCloser) bool {
resolvedPath := rq.pathMetadata.LastSegment
ctx, span := spanTrace(ctx, "Handler.RenderCodec", trace.WithAttributes(attribute.String("path", resolvedPath.String()), attribute.String("requestedContentType", rq.responseFormat)))
defer span.End()
Expand Down Expand Up @@ -124,7 +124,7 @@ func (i *handler) renderCodec(ctx context.Context, w http.ResponseWriter, r *htt
download := r.URL.Query().Get("download") == "true"

if isDAG && acceptsHTML && !download {
return i.serveCodecHTML(ctx, w, r, blockCid, blockData, resolvedPath, rq.contentPath, isRangeRequest)
return i.serveCodecHTML(ctx, w, r, blockCid, blockData, resolvedPath, rq.contentPath)
} else {
// This covers CIDs with codec 'json' and 'cbor' as those do not have
// an explicit requested content type.
Expand Down Expand Up @@ -156,7 +156,7 @@ func (i *handler) renderCodec(ctx context.Context, w http.ResponseWriter, r *htt
return i.serveCodecConverted(ctx, w, r, blockCid, blockData, rq.contentPath, toCodec, modtime, rq.begin)
}

func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r *http.Request, blockCid cid.Cid, blockData io.Reader, resolvedPath path.ImmutablePath, contentPath path.Path, isRangeRequest bool) bool {
func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r *http.Request, blockCid cid.Cid, blockData io.Reader, resolvedPath path.ImmutablePath, contentPath path.Path) bool {
// 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 @@ -201,7 +201,7 @@ func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r *
CID: resolvedPath.RootCid().String(),
CodecName: cidCodec.String(),
CodecHex: fmt.Sprintf("0x%x", uint64(cidCodec)),
Node: i.parseNode(ctx, blockCid, blockData, isRangeRequest),
Node: parseNode(blockCid, blockData),
})
if err != nil {
_, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err)))
Expand All @@ -213,7 +213,7 @@ func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r *
// parseNode does a best effort attempt to parse this request's block such that
// a preview can be displayed in the gateway. If something fails along the way,
// returns nil, therefore not displaying the preview.
func (i *handler) parseNode(ctx context.Context, blockCid cid.Cid, blockData io.Reader, tryGetBlockIfFailed bool) *assets.ParsedNode {
func parseNode(blockCid cid.Cid, blockData io.Reader) *assets.ParsedNode {
codec := blockCid.Prefix().Codec
decoder, err := multicodec.LookupDecoder(codec)
if err != nil {
Expand All @@ -222,18 +222,6 @@ func (i *handler) parseNode(ctx context.Context, blockCid cid.Cid, blockData io.

nodeBuilder := basicnode.Prototype.Any.NewBuilder()
err = decoder(nodeBuilder, blockData)
if err != nil && tryGetBlockIfFailed {
// It is possible we don't have the whole data for this block, e.g.,
// if range request is made from a browser where we want to display HTML.
// This does one attempt of fetching the data.
_, blockData, err = i.backend.GetBlock(ctx, path.FromCid(blockCid))
if err != nil {
return nil
}

nodeBuilder = basicnode.Prototype.Any.NewBuilder()
err = decoder(nodeBuilder, blockData)
}
if err != nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion gateway/handler_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (i *handler) serveDefaults(ctx context.Context, w http.ResponseWriter, r *h
dataToRender = dataAsReadSeekCloser
}

return i.renderCodec(r.Context(), w, r, rq, blockSize, dataToRender, len(ranges) > 0)
return i.renderCodec(r.Context(), w, r, rq, blockSize, dataToRender)
default:
rq.logger.Debugw("serving unixfs", "path", rq.contentPath)
ctx, span := spanTrace(ctx, "Handler.ServeUnixFS", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
Expand Down

0 comments on commit 84c7d89

Please sign in to comment.