Skip to content

Commit f63985c

Browse files
committed
refactor: do not use serveRawBlock inside serveCodec bc headers and other options
1 parent 7a42615 commit f63985c

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

core/corehttp/gateway_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
423423
return
424424
case "application/vnd.ipld.raw":
425425
logger.Debugw("serving raw block", "path", contentPath)
426-
i.serveRawBlock(r.Context(), w, r, resolvedPath, contentPath, begin, responseFormat)
426+
i.serveRawBlock(r.Context(), w, r, resolvedPath, contentPath, begin)
427427
return
428428
case "application/vnd.ipld.car":
429429
logger.Debugw("serving car stream", "path", contentPath)

core/corehttp/gateway_handler_block.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

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

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

4747
// ServeContent will take care of

core/corehttp/gateway_handler_codec.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package corehttp
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"html"
8+
"io"
79
"net/http"
810
"time"
911

@@ -42,28 +44,43 @@ func (i *gatewayHandler) serveCodec(ctx context.Context, w http.ResponseWriter,
4244
return
4345
}
4446

45-
// If the data is already encoded with the possible codecs, we can defer execution to
46-
// serveRawBlock, which will simply stream the raw data of this block.
47+
// Set Cache-Control and read optional Last-Modified time
48+
modtime := addCacheControlHeaders(w, r, contentPath, resolvedPath.Cid())
49+
name := addContentDispositionHeader(w, r, contentPath)
50+
w.Header().Set("Content-Type", contentType)
51+
w.Header().Set("X-Content-Type-Options", "nosniff")
52+
53+
// If the data is already encoded with the possible codecs, we can just stream the raw
54+
// data. serveRawBlock cannot be directly used here as it sets different headers.
4755
for _, codec := range codecs {
4856
if resolvedPath.Cid().Prefix().Codec == codec {
49-
i.serveRawBlock(ctx, w, r, resolvedPath, contentPath, begin, contentType)
57+
58+
blockCid := resolvedPath.Cid()
59+
blockReader, err := i.api.Block().Get(ctx, resolvedPath)
60+
if err != nil {
61+
webError(w, "ipfs block get "+blockCid.String(), err, http.StatusInternalServerError)
62+
return
63+
}
64+
block, err := io.ReadAll(blockReader)
65+
if err != nil {
66+
webError(w, "ipfs block get "+blockCid.String(), err, http.StatusInternalServerError)
67+
return
68+
}
69+
content := bytes.NewReader(block)
70+
71+
// ServeContent will take care of
72+
// If-None-Match+Etag, Content-Length and range requests
73+
_, _, _ = ServeContent(w, r, name, modtime, content)
5074
return
5175
}
5276
}
5377

54-
// Set Cache-Control and read optional Last-Modified time
55-
modtime := addCacheControlHeaders(w, r, contentPath, resolvedPath.Cid())
56-
5778
// Sets correct Last-Modified header. This code is borrowed from the standard
5879
// library (net/http/server.go) as we cannot use serveFile.
5980
if !(modtime.IsZero() || modtime.Equal(unixEpochTime)) {
6081
w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
6182
}
6283

63-
addContentDispositionHeader(w, r, contentPath)
64-
w.Header().Set("Content-Type", contentType)
65-
w.Header().Set("X-Content-Type-Options", "nosniff")
66-
6784
obj, err := i.api.Dag().Get(ctx, resolvedPath.Cid())
6885
if err != nil {
6986
webError(w, "ipfs dag get "+html.EscapeString(resolvedPath.String()), err, http.StatusInternalServerError)

0 commit comments

Comments
 (0)