generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 75
/
handler_unixfs_file.go
95 lines (82 loc) · 3.39 KB
/
handler_unixfs_file.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package gateway
import (
"bytes"
"context"
"io"
"mime"
"net/http"
gopath "path"
"strings"
"time"
"github.com/gabriel-vasile/mimetype"
"github.com/ipfs/boxo/path"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// serveFile returns data behind a file along with HTTP headers based on
// the file itself, its CID and the contentPath used for accessing it.
func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath path.ImmutablePath, rq *requestData, fileSize int64, fileBytes io.ReadCloser, isSymlink bool, returnRangeStartsAtZero bool, fileContentType string) bool {
_, span := spanTrace(ctx, "Handler.ServeFile", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
defer span.End()
// Set Cache-Control and read optional Last-Modified time
modtime := addCacheControlHeaders(w, r, rq.contentPath, rq.ttl, rq.lastMod, resolvedPath.RootCid(), "")
// Set Content-Disposition
name := addContentDispositionHeader(w, r, rq.contentPath)
if fileSize == 0 {
// We override null files to 200 to avoid issues with fragment caching reverse proxies.
// Also whatever you are asking for, it's cheaper to just give you the complete file (nothing).
// TODO: remove this if clause once https://github.com/golang/go/issues/54794 is fixed in two latest releases of go
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
return true
}
var content io.Reader = fileBytes
// Calculate deterministic value for Content-Type HTTP header
// (we prefer to do it here, rather than using implicit sniffing in http.ServeContent)
var ctype string
if isSymlink {
// We should be smarter about resolving symlinks but this is the
// "most correct" we can be without doing that.
ctype = "inode/symlink"
} else {
ctype = mime.TypeByExtension(gopath.Ext(name))
if ctype == "" {
ctype = fileContentType
}
if ctype == "" && returnRangeStartsAtZero {
// uses https://github.com/gabriel-vasile/mimetype library to determine the content type.
// Fixes https://github.com/ipfs/kubo/issues/7252
// We read from a TeeReader into a buffer and then put the buffer in front of the original reader to
// simulate the behavior of being able to read from the start and then seek back to the beginning while
// only having a Reader and not a ReadSeeker
var buf bytes.Buffer
tr := io.TeeReader(fileBytes, &buf)
mimeType, err := mimetype.DetectReader(tr)
if err != nil {
http.Error(w, "cannot detect content-type: "+err.Error(), http.StatusInternalServerError)
return false
}
ctype = mimeType.String()
content = io.MultiReader(&buf, fileBytes)
}
// Strip the encoding from the HTML Content-Type header and let the
// browser figure it out.
//
// Fixes https://github.com/ipfs/kubo/issues/2203
if strings.HasPrefix(ctype, "text/html;") {
ctype = "text/html"
}
}
// Setting explicit Content-Type to avoid mime-type sniffing on the client
// (unifies behavior across gateways and web browsers)
w.Header().Set("Content-Type", ctype)
// ServeContent will take care of
// If-None-Match+Etag, Content-Length and range requests
_, dataSent, _ := serveContent(w, r, modtime, fileSize, content)
// Was response successful?
if dataSent {
// Update metrics
i.unixfsFileGetMetric.WithLabelValues(rq.contentPath.Namespace()).Observe(time.Since(rq.begin).Seconds())
}
return dataSent
}