/
handler.go
389 lines (357 loc) · 12.2 KB
/
handler.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ffapi
import (
"bytes"
"context"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/url"
"path"
"reflect"
"regexp"
"strings"
"time"
"github.com/ghodss/yaml"
"github.com/gorilla/mux"
"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly-common/pkg/log"
"github.com/sirupsen/logrus"
)
const FFRequestIDHeader = "X-FireFly-Request-ID"
type (
CtxHeadersKey struct{}
CtxFFRequestIDKey struct{}
)
type HandlerFunction func(res http.ResponseWriter, req *http.Request) (status int, err error)
type HandlerFactory struct {
DefaultRequestTimeout time.Duration
MaxTimeout time.Duration
DefaultFilterLimit uint64
MaxFilterSkip uint64
MaxFilterLimit uint64
HandleYAML bool
PassthroughHeaders []string
AlwaysPaginate bool
SupportFieldRedaction bool
BasePath string
BasePathParams []*PathParam
}
var ffMsgCodeExtractor = regexp.MustCompile(`^(FF\d+):`)
type multipartState struct {
mpr *multipart.Reader
formParams map[string]string
part *Multipart
close func()
}
func (hs *HandlerFactory) getFilePart(req *http.Request) (*multipartState, error) {
formParams := make(map[string]string)
ctx := req.Context()
l := log.L(ctx)
mpr, err := req.MultipartReader()
if err != nil {
return nil, i18n.WrapError(ctx, err, i18n.MsgMultiPartFormReadError)
}
for {
part, err := mpr.NextPart()
if err != nil {
return nil, i18n.WrapError(ctx, err, i18n.MsgMultiPartFormReadError)
}
if part.FileName() == "" {
value, _ := io.ReadAll(part)
formParams[part.FormName()] = string(value)
} else {
l.Debugf("Processing multi-part upload. Field='%s' Filename='%s'", part.FormName(), part.FileName())
mp := &Multipart{
Data: part,
Filename: part.FileName(),
Mimetype: part.Header.Get("Content-Disposition"),
}
return &multipartState{
mpr: mpr,
formParams: formParams,
part: mp,
close: func() { _ = part.Close() },
}, nil
}
}
}
func (hs *HandlerFactory) getParams(req *http.Request, route *Route) (queryParams, pathParams map[string]string, queryArrayParams map[string][]string) {
queryParams = make(map[string]string)
pathParams = make(map[string]string)
queryArrayParams = make(map[string][]string)
v := mux.Vars(req)
for _, pp := range route.PathParams {
paramUnescaped, err := url.QueryUnescape(v[pp.Name]) // Gorilla mux assures this works
if err == nil {
pathParams[pp.Name] = paramUnescaped
}
}
for _, pp := range hs.BasePathParams {
paramUnescaped, err := url.QueryUnescape(v[pp.Name])
if err == nil {
pathParams[pp.Name] = paramUnescaped
}
}
for _, qp := range route.QueryParams {
val, exists := req.URL.Query()[qp.Name]
if qp.IsBool {
if exists && (len(val) == 0 || val[0] == "" || strings.EqualFold(val[0], "true")) {
val = []string{"true"}
} else {
val = []string{"false"}
}
}
if exists && len(val) > 0 {
if qp.IsArray {
queryArrayParams[qp.Name] = val
} else {
queryParams[qp.Name] = val[0]
}
}
}
return queryParams, pathParams, queryArrayParams
}
func (hs *HandlerFactory) RoutePath(route *Route) string {
return path.Join("/", hs.BasePath, route.Path)
}
func (hs *HandlerFactory) RouteHandler(route *Route) http.HandlerFunc {
// Check the mandatory parts are ok at startup time
return hs.APIWrapper(func(res http.ResponseWriter, req *http.Request) (int, error) {
var jsonInput interface{}
if route.JSONInputValue != nil {
jsonInput = route.JSONInputValue()
}
var queryParams, pathParams map[string]string
var queryArrayParams map[string][]string
var multipart *multipartState
contentType := req.Header.Get("Content-Type")
var err error
if req.Method != http.MethodGet && req.Method != http.MethodDelete {
switch {
case strings.HasPrefix(strings.ToLower(contentType), "multipart/form-data") && route.FormUploadHandler != nil:
multipart, err = hs.getFilePart(req)
if err != nil {
return 400, err
}
defer multipart.close()
case hs.HandleYAML && strings.HasPrefix(req.Header.Get("Content-Type"), "application/x-yaml"):
var jsonBytes []byte
yamlBytes, err := io.ReadAll(req.Body)
if err == nil {
jsonBytes, err = yaml.YAMLToJSON(yamlBytes)
}
if err != nil {
return 400, i18n.NewError(req.Context(), i18n.MsgRequestYAMLInvalid, err)
}
req.Body = io.NopCloser(bytes.NewBuffer(jsonBytes))
req.Header.Set("Content-Type", "application/json; charset=utf8")
fallthrough
case strings.HasPrefix(strings.ToLower(contentType), "application/json"):
if jsonInput != nil {
err = json.NewDecoder(req.Body).Decode(&jsonInput)
}
case strings.HasPrefix(strings.ToLower(contentType), "text/plain"):
default:
return 415, i18n.NewError(req.Context(), i18n.MsgInvalidContentType, contentType)
}
}
status := 400 // if fail parsing input
var output interface{}
if err == nil {
queryParams, pathParams, queryArrayParams = hs.getParams(req, route)
}
var filter AndFilter
if err == nil && route.FilterFactory != nil {
filter, err = hs.buildFilter(req, route.FilterFactory)
}
if err == nil {
r := &APIRequest{
Req: req,
PP: pathParams,
QP: queryParams,
QAP: queryArrayParams,
Filter: filter,
Input: jsonInput,
SuccessStatus: http.StatusOK,
AlwaysPaginate: hs.AlwaysPaginate,
// res.Header() returns a map which is a ref type so handler header edits are persisted
ResponseHeaders: res.Header(),
}
if len(route.JSONOutputCodes) > 0 {
r.SuccessStatus = route.JSONOutputCodes[0]
}
switch {
case multipart != nil:
r.FP = multipart.formParams
r.Part = multipart.part
output, err = route.FormUploadHandler(r)
case route.StreamHandler != nil:
output, err = route.StreamHandler(r)
default:
output, err = route.JSONHandler(r)
}
status = r.SuccessStatus // Can be updated by the route
}
if err == nil && multipart != nil {
// Catch the case that someone puts form fields after the file in a multi-part body.
// We don't support that, so that we can stream through the core rather than having
// to hold everything in memory.
trailing, expectEOF := multipart.mpr.NextPart()
if expectEOF == nil {
err = i18n.NewError(req.Context(), i18n.MsgFieldsAfterFile, trailing.FormName())
}
}
if err == nil {
status, err = hs.handleOutput(req.Context(), res, status, output)
}
return status, err
})
}
func (hs *HandlerFactory) handleOutput(ctx context.Context, res http.ResponseWriter, status int, output interface{}) (int, error) {
vOutput := reflect.ValueOf(output)
outputKind := vOutput.Kind()
isPointer := outputKind == reflect.Ptr
invalid := outputKind == reflect.Invalid
isNil := output == nil || invalid || (isPointer && vOutput.IsNil())
var reader io.ReadCloser
var marshalErr error
if !isNil && vOutput.CanInterface() {
reader, _ = vOutput.Interface().(io.ReadCloser)
}
switch {
case isNil:
if status != 204 {
return 404, i18n.NewError(ctx, i18n.Msg404NoResult)
}
res.WriteHeader(204)
case reader != nil:
defer reader.Close()
if res.Header().Get("Content-Type") == "" {
res.Header().Add("Content-Type", "application/octet-stream")
}
res.WriteHeader(status)
_, marshalErr = io.Copy(res, reader)
default:
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(status)
marshalErr = json.NewEncoder(res).Encode(output)
}
if marshalErr != nil {
err := i18n.WrapError(ctx, marshalErr, i18n.MsgResponseMarshalError)
log.L(ctx).Errorf(err.Error())
return 500, err
}
return status, nil
}
func CalcRequestTimeout(req *http.Request, defaultTimeout, maxTimeout time.Duration) time.Duration {
// Configure a server-side timeout on each request, to try and avoid cases where the API requester
// times out, and we continue to churn indefinitely processing the request.
// Long-running processes should be dispatched asynchronously (API returns 202 Accepted asap),
// and the caller can either listen on the websocket for updates, or poll the status of the affected object.
// This is dependent on the context being passed down through to all blocking operations down the stack
// (while avoiding passing the context to asynchronous tasks that are dispatched as a result of the request)
reqTimeout := defaultTimeout
reqTimeoutHeader := req.Header.Get("Request-Timeout")
if reqTimeoutHeader != "" {
customTimeout, err := fftypes.ParseDurationString(reqTimeoutHeader, time.Second /* default is seconds */)
if err != nil {
log.L(req.Context()).Warnf("Invalid Request-Timeout header '%s': %s", reqTimeoutHeader, err)
} else {
reqTimeout = time.Duration(customTimeout)
if reqTimeout > maxTimeout {
reqTimeout = maxTimeout
}
}
}
return reqTimeout
}
func (hs *HandlerFactory) getTimeout(req *http.Request) time.Duration {
return CalcRequestTimeout(req, hs.DefaultRequestTimeout, hs.MaxTimeout)
}
func (hs *HandlerFactory) APIWrapper(handler HandlerFunction) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
reqTimeout := hs.getTimeout(req)
ctx, cancel := context.WithTimeout(req.Context(), reqTimeout)
httpReqID := req.Header.Get(FFRequestIDHeader)
if httpReqID == "" {
httpReqID = fftypes.ShortID()
}
ctx = withRequestID(ctx, httpReqID)
ctx = withPassthroughHeaders(ctx, req, hs.PassthroughHeaders)
ctx = log.WithLogField(ctx, "httpreq", httpReqID)
req = req.WithContext(ctx)
defer cancel()
// Wrap the request itself in a log wrapper, that gives minimal request/response and timing info
l := log.L(ctx)
l.Infof("--> %s %s", req.Method, req.URL.Path)
startTime := time.Now()
status, err := handler(res, req)
durationMS := float64(time.Since(startTime)) / float64(time.Millisecond)
if err != nil {
if ffe, ok := (interface{}(err)).(i18n.FFError); ok {
if logrus.IsLevelEnabled(logrus.DebugLevel) {
log.L(ctx).Debugf("%s:\n%s", ffe.Error(), ffe.StackTrace())
}
status = ffe.HTTPStatus()
} else {
// Routers don't need to tweak the status code when sending errors.
// .. either the FF12345 error they raise is mapped to a status hint
ffMsgCodeExtract := ffMsgCodeExtractor.FindStringSubmatch(err.Error())
if len(ffMsgCodeExtract) >= 2 {
if statusHint, ok := i18n.GetStatusHint(ffMsgCodeExtract[1]); ok {
status = statusHint
}
}
}
// If the context is done, we wrap in 408
if status != http.StatusRequestTimeout {
select {
case <-ctx.Done():
l.Errorf("Request failed and context is closed. Returning %d (overriding %d): %s", http.StatusRequestTimeout, status, err)
status = http.StatusRequestTimeout
err = i18n.WrapError(ctx, err, i18n.MsgRequestTimeout, httpReqID, durationMS)
default:
}
}
// ... or we default to 500
if status < 300 {
status = 500
}
l.Infof("<-- %s %s [%d] (%.2fms): %s", req.Method, req.URL.Path, status, durationMS, err)
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(status)
_ = json.NewEncoder(res).Encode(&fftypes.RESTError{
Error: err.Error(),
})
} else {
l.Infof("<-- %s %s [%d] (%.2fms)", req.Method, req.URL.Path, status, durationMS)
}
}
}
func withPassthroughHeaders(ctx context.Context, req *http.Request, passthroughHeaders []string) context.Context {
headers := http.Header{}
for _, key := range passthroughHeaders {
headers.Set(key, req.Header.Get(key))
}
return context.WithValue(ctx, CtxHeadersKey{}, headers)
}
func withRequestID(ctx context.Context, requestID string) context.Context {
return context.WithValue(ctx, CtxFFRequestIDKey{}, requestID)
}