-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
143 lines (131 loc) · 4.02 KB
/
server.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cassette
import (
"context"
"errors"
"net/http"
"github.com/ipni/go-libipni/apierror"
"github.com/ipni/go-libipni/find/model"
"github.com/ipni/go-libipni/rwriter"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-varint"
)
const ipniCascadeQueryKey = "cascade"
var (
cascadeContextID = []byte("legacy-cascade")
cascadeMetadata = varint.ToUvarint(uint64(multicodec.TransportBitswap))
)
func (c *Cassette) serveMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/cid", c.handleMh)
mux.HandleFunc("/cid/", c.handleMhSubtree)
mux.HandleFunc("/multihash", c.handleMh)
mux.HandleFunc("/multihash/", c.handleMhSubtree)
mux.HandleFunc("/ready", c.handleReady)
mux.HandleFunc("/", c.handleCatchAll)
return mux
}
func (c *Cassette) handleMh(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodOptions:
c.handleLookupOptions(w)
default:
w.Header().Set("Allow", http.MethodOptions)
http.Error(w, "", http.StatusMethodNotAllowed)
}
}
func (c *Cassette) handleMhSubtree(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
rspWriter, err := rwriter.New(w, r, rwriter.WithPreferJson(c.httpResponsePreferJson))
if err != nil {
var apiErr *apierror.Error
if errors.As(err, &apiErr) {
http.Error(w, apiErr.Error(), apiErr.Status())
return
}
http.Error(w, "", http.StatusBadRequest)
return
}
if c.ipniRequireCascadeQueryParam {
present, matched := rwriter.MatchQueryParam(r, ipniCascadeQueryKey, c.ipniCascadeLabel)
if !present {
logger.Debugw("Rejected request with unspecified cascade query parameter.")
http.Error(w, "", http.StatusNotFound)
return
}
if !matched {
labels := r.URL.Query()[ipniCascadeQueryKey]
logger.Infow("Rejected request with mismatching cascade label.", "want", c.ipniCascadeLabel, "got", labels)
http.Error(w, "", http.StatusNotFound)
return
}
}
c.handleLookup(rwriter.NewProviderResponseWriter(rspWriter), r)
case http.MethodOptions:
c.handleLookupOptions(w)
default:
w.Header().Set("Allow", http.MethodGet)
w.Header().Add("Allow", http.MethodOptions)
http.Error(w, "", http.StatusMethodNotAllowed)
}
}
func (c *Cassette) handleLookup(w *rwriter.ProviderResponseWriter, r *http.Request) {
ctx, cancel := context.WithCancel(r.Context())
pch := c.Find(ctx, w.Cid())
defer cancel()
LOOP:
for {
select {
case <-c.ctx.Done():
logger.Debugw("Interrupted while responding to lookup", "key", w.Cid(), "err", ctx.Err())
break LOOP
case provider, ok := <-pch:
if !ok {
logger.Debugw("No more provider records", "key", w.Cid())
break LOOP
}
err := w.WriteProviderResult(model.ProviderResult{
ContextID: cascadeContextID,
Metadata: cascadeMetadata,
Provider: &peer.AddrInfo{
ID: provider.ID,
Addrs: provider.Addrs,
},
})
if err != nil {
logger.Errorw("Failed to encode provider record", "err", err)
break LOOP
}
}
}
if err := w.Close(); err != nil {
var apiErr *apierror.Error
if errors.As(err, &apiErr) {
http.Error(w, apiErr.Error(), apiErr.Status())
} else {
logger.Errorw("Failed to finalize lookup results", "err", err)
http.Error(w, "", http.StatusInternalServerError)
}
return
}
}
func (c *Cassette) handleLookupOptions(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", c.httpAllowOrigin)
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("X-IPNI-Allow-Cascade", c.ipniCascadeLabel)
w.WriteHeader(http.StatusAccepted)
}
func (c *Cassette) handleReady(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, "", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Cache-Control", "no-cache")
http.Error(w, Version, http.StatusOK)
}
func (c *Cassette) handleCatchAll(w http.ResponseWriter, r *http.Request) {
http.Error(w, "", http.StatusNotFound)
}