-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathhandler_block.go
More file actions
68 lines (55 loc) · 1.84 KB
/
handler_block.go
File metadata and controls
68 lines (55 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package gateway
import (
"context"
"fmt"
"io"
"net/http"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// serveRawBlock returns bytes behind a raw block
func (i *handler) serveRawBlock(ctx context.Context, w http.ResponseWriter, r *http.Request, rq *requestData) bool {
ctx, span := spanTrace(ctx, "Handler.ServeRawBlock", trace.WithAttributes(attribute.String("path", rq.immutablePath.String())))
defer span.End()
pathMetadata, data, err := i.backend.GetBlock(ctx, rq.mostlyResolvedPath())
if !i.handleRequestErrors(w, r, rq.contentPath, err) {
return false
}
defer data.Close()
setIpfsRootsHeader(w, rq, &pathMetadata)
blockCid := pathMetadata.LastSegment.RootCid()
// Set Content-Disposition
var name string
if urlFilename := r.URL.Query().Get("filename"); urlFilename != "" {
name = urlFilename
} else {
name = blockCid.String() + ".bin"
}
setContentDispositionHeader(w, name, "attachment")
// Set remaining headers
modtime := addCacheControlHeaders(w, r, rq.contentPath, rq.ttl, rq.lastMod, blockCid, rawResponseFormat)
w.Header().Set("Content-Type", rawResponseFormat)
w.Header().Set("X-Content-Type-Options", "nosniff") // no funny business in the browsers :^)
sz, err := data.Size()
if err != nil {
i.handleRequestErrors(w, r, rq.contentPath, err)
return false
}
s, ok := data.(io.Seeker)
if !ok {
i.webError(w, r, fmt.Errorf("block data does not support seeking"), http.StatusInternalServerError)
return false
}
if !i.seekToStartOfFirstRange(w, r, s, sz) {
return false
}
// ServeContent will take care of
// If-None-Match+Etag, Content-Length and range requests
_, dataSent, _ := serveContent(w, r, modtime, sz, data)
if dataSent {
// Update metrics
i.rawBlockGetMetric.WithLabelValues(rq.contentPath.Namespace()).Observe(time.Since(rq.begin).Seconds())
}
return dataSent
}