-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_server.go
165 lines (155 loc) · 4.58 KB
/
api_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"context"
"encoding/json"
"errors"
"net/http"
"sort"
"strconv"
"strings"
"github.com/libp2p/go-libp2p/core/peer"
)
func (hf *heyFil) startApiServer() error {
mux := http.NewServeMux()
mux.HandleFunc(`/sp`, hf.handleSPRoot)
mux.HandleFunc(`/sp/`, hf.handleSPSubtree)
mux.HandleFunc(`/`, handleDefault)
hf.apiServer = http.Server{
Addr: hf.apiListenAddr,
Handler: mux,
}
go func() {
err := hf.apiServer.ListenAndServe()
switch {
case errors.Is(err, http.ErrServerClosed):
logger.Info("server stopped")
default:
logger.Infow("server failed", "")
}
}()
return nil
}
func (hf *heyFil) handleSPRoot(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodOptions:
w.Header().Set(httpHeaderAllow(http.MethodGet, http.MethodHead, http.MethodOptions))
case http.MethodHead:
// Do nothing for HEAD request.
case http.MethodGet:
pid := r.URL.Query().Get("peerid")
filterByPeerID := pid != ""
var pidFilter peer.ID
if filterByPeerID {
var err error
pidFilter, err = peer.Decode(pid)
if err != nil {
http.Error(w, `The "peerid" query parameter is not a valid peer ID`, http.StatusBadRequest)
return
}
}
status := r.URL.Query().Get("status")
filterByStatus := status != ""
var statusFilter func(t *Target) bool
if filterByStatus {
statusNum := strings.TrimPrefix(status, "!")
negate := status != statusNum
statusTarget, err := strconv.ParseInt(statusNum, 10, 32)
if err != nil {
http.Error(w, `The "status" query parameter cannot be parsed.`, http.StatusBadRequest)
return
}
statusFilter = func(t *Target) bool {
return (t.Status == Status(statusTarget)) != negate
}
}
hf.targetsMutex.RLock()
spIDs := make([]string, 0, len(hf.targets))
for id, target := range hf.targets {
if filterByPeerID && !target.hasPeerID(pidFilter) {
continue
}
if statusFilter != nil && !statusFilter(target) {
continue
}
spIDs = append(spIDs, id)
}
sort.Strings(spIDs)
hf.targetsMutex.RUnlock()
if err := json.NewEncoder(w).Encode(spIDs); err != nil {
logger.Errorw("Failed to encode SP IDs", "err", err)
}
default:
hf.respondWithNotAllowed(w, http.MethodGet, http.MethodOptions)
}
}
func (hf *heyFil) handleSPSubtree(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodOptions:
w.Header().Set(httpHeaderAllow(http.MethodGet, http.MethodHead, http.MethodOptions))
case http.MethodHead:
// Do nothing for HEAD request.
case http.MethodGet:
pathSuffix := strings.TrimPrefix(r.URL.Path, "/sp/")
switch segments := strings.SplitN(pathSuffix, "/", 3); len(segments) {
case 1:
id := segments[0]
if id == "" {
// Path does not contain SP ID.
http.Error(w, "SP ID must be specified as URL parameter", http.StatusBadRequest)
} else {
hf.handleGetSP(w, id)
}
case 2:
id := segments[0]
subreq := segments[1]
if id == "" || subreq != "recentPiece" {
// Path does not contain SP ID.
http.Error(w, "SP ID must be specified as URL parameter", http.StatusBadRequest)
} else {
hf.recentPiecesMutex.RLock()
rp, ok := hf.recentPieces[id]
hf.recentPiecesMutex.RUnlock()
if !ok {
http.Error(w, "No piece found for provided id", http.StatusNotFound)
} else if err := json.NewEncoder(w).Encode(rp); err != nil {
logger.Errorw("Failed to encode piece id", "id", rp, "err", err)
}
}
default:
// Path has multiple segments and therefore 404
http.NotFound(w, r)
}
default:
hf.respondWithNotAllowed(w, http.MethodGet, http.MethodOptions)
}
}
func (hf *heyFil) handleGetSP(w http.ResponseWriter, id string) {
hf.targetsMutex.RLock()
target, ok := hf.targets[id]
hf.targetsMutex.RUnlock()
if !ok {
http.Error(w, "SP not found", http.StatusNotFound)
} else if err := json.NewEncoder(w).Encode(target); err != nil {
logger.Errorw("Failed to encode SP info", "id", id, "err", err)
}
}
func handleDefault(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodOptions:
w.Header().Set(httpHeaderAllow(http.MethodHead, http.MethodOptions))
case http.MethodHead:
// Do nothing for HEAD request.
default:
http.NotFound(w, r)
}
}
func httpHeaderAllow(methods ...string) (string, string) {
return "Allow", strings.Join(methods, ",")
}
func (hf *heyFil) respondWithNotAllowed(w http.ResponseWriter, allowedMethods ...string) {
w.Header().Set(httpHeaderAllow(allowedMethods...))
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
func (hf *heyFil) shutdownApiServer(ctx context.Context) error {
return hf.apiServer.Shutdown(ctx)
}