generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 99
/
handler_ipns_record.go
93 lines (80 loc) · 2.61 KB
/
handler_ipns_record.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
package gateway
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/cespare/xxhash"
"github.com/gogo/protobuf/proto"
ipath "github.com/ipfs/boxo/coreiface/path"
ipns_pb "github.com/ipfs/boxo/ipns/pb"
"github.com/ipfs/go-cid"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)
func (i *handler) serveIpnsRecord(ctx context.Context, w http.ResponseWriter, r *http.Request, contentPath ipath.Path, begin time.Time, logger *zap.SugaredLogger) bool {
ctx, span := spanTrace(ctx, "Handler.ServeIPNSRecord", trace.WithAttributes(attribute.String("path", contentPath.String())))
defer span.End()
if contentPath.Namespace() != "ipns" {
err := fmt.Errorf("%s is not an IPNS link", contentPath.String())
webError(w, err, http.StatusBadRequest)
return false
}
key := contentPath.String()
key = strings.TrimSuffix(key, "/")
key = strings.TrimPrefix(key, "/ipns/")
if strings.Count(key, "/") != 0 {
err := errors.New("cannot find ipns key for subpath")
webError(w, err, http.StatusBadRequest)
return false
}
c, err := cid.Decode(key)
if err != nil {
webError(w, err, http.StatusBadRequest)
return false
}
rawRecord, err := i.api.GetIPNSRecord(ctx, c)
if err != nil {
webError(w, err, http.StatusInternalServerError)
return false
}
var record ipns_pb.IpnsEntry
err = proto.Unmarshal(rawRecord, &record)
if err != nil {
webError(w, err, http.StatusInternalServerError)
return false
}
// Set cache control headers based on the TTL set in the IPNS record. If the
// TTL is not present, we use the Last-Modified tag. We are tracking IPNS
// caching on: https://github.com/ipfs/kubo/issues/1818.
// TODO: use addCacheControlHeaders once #1818 is fixed.
recordEtag := strconv.FormatUint(xxhash.Sum64(rawRecord), 32)
w.Header().Set("Etag", recordEtag)
if record.Ttl != nil {
seconds := int(time.Duration(*record.Ttl).Seconds())
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", seconds))
} else {
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
}
// Set Content-Disposition
var name string
if urlFilename := r.URL.Query().Get("filename"); urlFilename != "" {
name = urlFilename
} else {
name = key + ".ipns-record"
}
setContentDispositionHeader(w, name, "attachment")
w.Header().Set("Content-Type", "application/vnd.ipfs.ipns-record")
w.Header().Set("X-Content-Type-Options", "nosniff")
_, err = w.Write(rawRecord)
if err == nil {
// Update metrics
i.ipnsRecordGetMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds())
return true
}
return false
}