Skip to content

Commit

Permalink
refactor: simplify serveCodec to use serveRawBlock iff data encoded i…
Browse files Browse the repository at this point in the history
…n output encoding
  • Loading branch information
hacdias committed Oct 13, 2022
1 parent 1fa5c75 commit af21ef0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
return
case "application/vnd.ipld.raw":
logger.Debugw("serving raw block", "path", contentPath)
i.serveRawBlock(r.Context(), w, r, resolvedPath, contentPath, begin)
i.serveRawBlock(r.Context(), w, r, resolvedPath, contentPath, begin, responseFormat)
return
case "application/vnd.ipld.car":
logger.Debugw("serving car stream", "path", contentPath)
Expand Down
4 changes: 2 additions & 2 deletions core/corehttp/gateway_handler_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// serveRawBlock returns bytes behind a raw block
func (i *gatewayHandler) serveRawBlock(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time) {
func (i *gatewayHandler) serveRawBlock(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time, contentType string) {
ctx, span := tracing.Span(ctx, "Gateway", "ServeRawBlock", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
defer span.End()
blockCid := resolvedPath.Cid()
Expand All @@ -41,7 +41,7 @@ func (i *gatewayHandler) serveRawBlock(ctx context.Context, w http.ResponseWrite

// Set remaining headers
modtime := addCacheControlHeaders(w, r, contentPath, blockCid)
w.Header().Set("Content-Type", "application/vnd.ipld.raw")
w.Header().Set("Content-Type", contentType)
w.Header().Set("X-Content-Type-Options", "nosniff") // no funny business in the browsers :^)

// ServeContent will take care of
Expand Down
17 changes: 9 additions & 8 deletions core/corehttp/gateway_handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ func (i *gatewayHandler) serveCodec(ctx context.Context, w http.ResponseWriter,
return
}

// If the data is already encoded with the possible codecs, we can defer execution to
// serveRawBlock, which will simply stream the raw data of this block.
for _, codec := range codecs {
if resolvedPath.Cid().Prefix().Codec == codec {
i.serveRawBlock(ctx, w, r, resolvedPath, contentPath, begin, contentType)
return
}
}

// Set Cache-Control and read optional Last-Modified time
modtime := addCacheControlHeaders(w, r, contentPath, resolvedPath.Cid())

Expand All @@ -61,14 +70,6 @@ func (i *gatewayHandler) serveCodec(ctx context.Context, w http.ResponseWriter,
return
}

// If the data is already encoded with the possible codecs, just stream it out.
for _, codec := range codecs {
if resolvedPath.Cid().Prefix().Codec == codec {
_, _ = w.Write(obj.RawData())
return
}
}

universal, ok := obj.(ipldlegacy.UniversalNode)
if !ok {
webError(w, "todo", fmt.Errorf("%T is not a valid IPLD node", obj), http.StatusInternalServerError)
Expand Down

0 comments on commit af21ef0

Please sign in to comment.