/
apimiddleware.go
339 lines (303 loc) · 9.94 KB
/
apimiddleware.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) 2018-2019, The Decred-Next developers
// Copyright (c) 2017, The dcrdata developers
// See LICENSE for details.
package insight
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
apitypes "github.com/Decred-Next/dcrndata/v8/api/types"
m "github.com/Decred-Next/dcrndata/v8/middleware"
"github.com/go-chi/chi"
)
type contextKey int
const (
ctxFrom contextKey = iota
ctxTo
ctxNoAsm
ctxNoScriptSig
ctxNoSpent
ctxNoTxList
ctxAddrCmd
ctxNbBlocks
)
// BlockHashPathAndIndexCtx is a middleware that embeds the value at the url
// part {blockhash}, and the corresponding block index, into a request context.
func (iapi *InsightApi) BlockHashPathAndIndexCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := m.BlockHashPathAndIndexCtx(r, iapi.BlockData)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// StatusInfoCtx is a middleware that embeds into the request context the data
// for the "?q=x" URL query, where x is "getInfo" or "getDifficulty" or
// "getBestBlockHash" or "getLastBlockHash".
func (iapi *InsightApi) StatusInfoCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := m.StatusInfoCtx(r, iapi.BlockData)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// GetToCtx retrieves the ctxTo data ("to") from the request context. If not
// set, the return value ok is false.
func GetToCtx(r *http.Request) (int64, bool) {
to, ok := r.Context().Value(ctxTo).(int)
if !ok {
return int64(0), false
}
return int64(to), true
}
// GetFromCtx retrieves the ctxFrom data ("from") from the request context.
// If not set, the return value is 0
func GetFromCtx(r *http.Request) int64 {
from, ok := r.Context().Value(ctxFrom).(int)
if !ok {
return int64(0)
}
return int64(from)
}
// GetNoAsmCtx retrieves the ctxNoAsm data ("noAsm") from the request context.
// If not set, the return value is false.
func GetNoAsmCtx(r *http.Request) bool {
noAsm, ok := r.Context().Value(ctxNoAsm).(bool)
if !ok {
return false
}
return noAsm
}
// GetNoScriptSigCtx retrieves the ctxNoScriptSig data ("noScriptSig") from the
// request context. If not set, the return value is false.
func GetNoScriptSigCtx(r *http.Request) bool {
noScriptSig, ok := r.Context().Value(ctxNoScriptSig).(bool)
if !ok {
return false
}
return noScriptSig
}
// GetNoSpentCtx retrieves the ctxNoSpent data ("noSpent") from the
// request context. If not set, the return value is false.
func GetNoSpentCtx(r *http.Request) bool {
noSpent, ok := r.Context().Value(ctxNoSpent).(bool)
if !ok {
return false
}
return noSpent
}
// FromToPaginationCtx will parse the query parameters for from/to values.
func FromToPaginationCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
to, from := r.FormValue("to"), r.FormValue("from")
fromint, err := strconv.Atoi(from)
if err == nil {
ctx = context.WithValue(r.Context(), ctxFrom, fromint)
}
toint, err := strconv.Atoi(to)
if err == nil {
ctx = context.WithValue(ctx, ctxTo, toint)
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// ValidatePostCtx will confirm Post content length is valid.
func (iapi *InsightApi) ValidatePostCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
contentLengthString := r.Header.Get("Content-Length")
contentLength, err := strconv.Atoi(contentLengthString)
if err != nil {
writeInsightError(w, "Content-Length Header must be set")
return
}
// Broadcast Tx has the largest possible body. Cap max content length
// to iapi.params.MaxTxSize * 2 plus some arbitrary extra for JSON
// encapsulation.
maxPayload := (iapi.params.MaxTxSize * 2) + 50
if contentLength > maxPayload {
writeInsightError(w, fmt.Sprintf("Maximum Content-Length is %d", maxPayload))
return
}
next.ServeHTTP(w, r)
})
}
// PostAddrsTxsCtx middleware processes parameters given in the POST request
// body for an addrs endpoint. While the addresses list, "addrs", must be in the
// POST body JSON, the other parameters may be specified as URL queries. POST
// body values take priority.
func PostAddrsTxsCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
writeInsightError(w, fmt.Sprintf("error reading JSON message: %v", err))
return
}
// The request body must be JSON.
var req apitypes.InsightMultiAddrsTx
err = json.Unmarshal(body, &req)
if err != nil {
writeInsightError(w, fmt.Sprintf("Failed to parse request: %v", err))
return
}
// addrs must come from POST body.
addrs := strings.Split(req.Addresses, ",")
ctx := context.WithValue(r.Context(), m.CtxAddress, addrs)
// Other parameters may come from the POST body or URL query values.
// from
from, err := req.From.Int64()
if err == nil {
ctx = context.WithValue(ctx, ctxFrom, int(from))
} else {
fromStr := r.FormValue("from")
from, _ := strconv.Atoi(fromStr) // shadow
ctx = context.WithValue(ctx, ctxFrom, from)
}
// to
to, err := req.To.Int64()
if err == nil {
ctx = context.WithValue(ctx, ctxTo, int(to))
} else {
toStr := r.FormValue("to")
to, _ := strconv.Atoi(toStr)
ctx = context.WithValue(ctx, ctxTo, to)
}
// noAsm
noAsm, err := req.NoAsm.Int64()
if err == nil {
ctx = context.WithValue(ctx, ctxNoAsm, noAsm != 0)
} else {
noAsmStr := r.FormValue("noAsm")
noAsm, _ := strconv.ParseBool(noAsmStr)
ctx = context.WithValue(ctx, ctxNoAsm, noAsm)
}
// noScriptSig
noScriptSig, err := req.NoScriptSig.Int64()
if err == nil {
ctx = context.WithValue(ctx, ctxNoScriptSig, noScriptSig != 0)
} else {
noScriptSigStr := r.FormValue("noScriptSig")
noScriptSig, _ := strconv.ParseBool(noScriptSigStr)
ctx = context.WithValue(ctx, ctxNoScriptSig, noScriptSig)
}
// noSpent
noSpent, err := req.NoSpent.Int64()
if err == nil {
ctx = context.WithValue(ctx, ctxNoSpent, noSpent != 0)
} else {
noSpentStr := r.FormValue("noSpent")
noSpent, _ := strconv.ParseBool(noSpentStr)
ctx = context.WithValue(ctx, ctxNoSpent, noSpent)
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// PostAddrsUtxoCtx middleware processes parameters given in the POST request
// body for an addrs utxo endpoint.
func PostAddrsUtxoCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := apitypes.InsightAddr{}
body, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
writeInsightError(w, fmt.Sprintf("error reading JSON message: %v", err))
return
}
err = json.Unmarshal(body, &req)
if err != nil {
writeInsightError(w, fmt.Sprintf("Failed to parse request: %v", err))
return
}
// Successful extraction of Body JSON
addrs := strings.Split(req.Addrs, ",")
ctx := context.WithValue(r.Context(), m.CtxAddress, addrs)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// AddressCommandCtx returns a http.HandlerFunc that embeds the value at the url
// part {command} into the request context.
func AddressCommandCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
command := chi.URLParam(r, "command")
ctx := context.WithValue(r.Context(), ctxAddrCmd, command)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// GetAddressCommandCtx retrieves the ctxAddrCmd data from the request context.
// If not set the return value is "" and false.
func GetAddressCommandCtx(r *http.Request) (string, bool) {
command, ok := r.Context().Value(ctxAddrCmd).(string)
if !ok {
return "", false
}
return command, true
}
// NoTxListCtx returns a http.Handlerfunc that embeds the {noTxList} value in
// the request into the request context.
func NoTxListCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
notxlist := r.FormValue("noTxList")
notxlistint, err := strconv.Atoi(notxlist)
if err != nil {
notxlistint = 0
}
ctx := context.WithValue(r.Context(), ctxNoTxList, notxlistint)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// GetLimitCtx retrieves the ctxLimit data from the request context. If not set,
// the return value is 0 which is interpreted as no limit.
func GetLimitCtx(r *http.Request) int {
limit, ok := r.Context().Value(m.CtxLimit).(string)
if !ok {
return 0
}
intValue, err := strconv.Atoi(limit)
if err != nil {
return 0
}
return intValue
}
// GetNoTxListCtx retrieves the ctxNoTxList data ("noTxList") from the request context.
// If not set, the return value is false.
func GetNoTxListCtx(r *http.Request) int {
notxlist, ok := r.Context().Value(ctxNoTxList).(int)
if !ok {
return 0
}
return notxlist
}
// BlockDateLimitQueryCtx returns a http.Handlerfunc that embeds the
// {blockdate,limit} value in the request into the request context.
func BlockDateLimitQueryCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
blockDate := r.FormValue("blockDate")
ctx := context.WithValue(r.Context(), m.CtxBlockDate, blockDate)
limit := r.FormValue("limit")
ctx = context.WithValue(ctx, m.CtxLimit, limit)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// GetNbBlocksCtx retrieves the ctxNbBlocks data from the request context. If not
// set, the return value is 0.
func GetNbBlocksCtx(r *http.Request) int {
nbBlocks, ok := r.Context().Value(ctxNbBlocks).(int)
if !ok {
return 0
}
return nbBlocks
}
// NbBlocksCtx will parse the query parameters for nbBlocks.
func NbBlocksCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
nbBlocks := r.FormValue("nbBlocks")
nbBlocksint, err := strconv.Atoi(nbBlocks)
if err == nil {
ctx = context.WithValue(r.Context(), ctxNbBlocks, nbBlocksint)
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}