-
Notifications
You must be signed in to change notification settings - Fork 11
/
middleware.go
773 lines (702 loc) · 24.2 KB
/
middleware.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// Copyright (c) 2016 Pani Networks
// All Rights Reserved.
//
// 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 common
// Things related to the REST framework.
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
"reflect"
"strings"
"github.com/K-Phoen/negotiation"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/pborman/uuid"
"github.com/romana/core/common/log/trace"
log "github.com/romana/rlog"
// "log"
"net/http"
)
// RestContext contains the context of the REST request other
// than the body data that has been unmarshaled.
type RestContext struct {
// Path variables as described in https://godoc.org/code.google.com/p/gorilla/mux
PathVariables map[string]string
// QueryVariables stores key-value-list map of query variables, see url.Values
// for more details.
QueryVariables url.Values
// Unique identifier for a request.
RequestToken string
User User
// Output of the hook if any run before the execution of the handler.
HookOutput string
}
// RestHandler specifies type of a function that each Route provides.
// It takes (for now) an interface as input, and returns any
// interface. The middleware provided in this file takes care
// of unmarshalling the data from the wire to the input object
// (the type of the object created will be determined by the
// type of the instance provided in Consumes field of Route type, below),
// and of marshalling the returned object to the wire (the type of
// which is determined by type of the instance provided in Produces
// field of Route type, below).
type RestHandler func(input interface{}, context RestContext) (interface{}, error)
// UnwrappedRestHandlerInput is used to pass in
// http.Request and http.ResponseWriter, should some
// service like unfettered access directly to them. In
// such a case, the service's RestHandler's input will be of this type;
// and the return value will be ignored.
type UnwrappedRestHandlerInput struct {
ResponseWriter http.ResponseWriter
Request *http.Request
}
// MakeMessage is a factory function, which should return a pointer to
// an instance into which we will unmarshal wire data.
type MakeMessage func() interface{}
// Route determines an action taken on a URL pattern/HTTP method.
// Each service can define a route
// See routes.go and handlers.go in root package for a demonstration
// of use
type Route struct {
// REST method
Method string
// Pattern (see http://www.gorillatoolkit.org/pkg/mux)
Pattern string
// Handler (see documentation above)
Handler RestHandler
// This should return a POINTER to an instance which
// this route expects as an input.
MakeMessage MakeMessage
// Whether this route is using a request token. If true, the
// request token will be parsed out of the request and made
// available in RestContext. It can then
// used by the handler to achieve idempotence.
UseRequestToken bool
Hook *Hook
AuthZChecker AuthZChecker
}
// Routes provided by each service.
type Routes []Route
// RomanaHandler interface to comply with http.Handler
type RomanaHandler struct {
doServeHTTP func(writer http.ResponseWriter, request *http.Request)
}
// ServeHTTP is required by
// https://golang.org/pkg/net/http/#Handler
func (romanaHandler RomanaHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
romanaHandler.doServeHTTP(writer, request)
}
// For comparing to the type of Consumes field of Route struct.
var requestType = reflect.TypeOf(http.Request{})
// For comparing to the type of string.
var stringType = reflect.TypeOf("")
// write500 writes out a 500 error based on provided err
func write500(writer http.ResponseWriter, m Marshaller, err error) {
writer.WriteHeader(http.StatusInternalServerError)
httpErr := NewError500(err)
// Should never error out - it's a struct we know.
outData, _ := m.Marshal(httpErr)
log.Tracef(trace.Inside, "Made\n\t%+v\n\tfrom\n\t%+v\n\t%s", httpErr, err, string(outData))
writer.Write(outData)
}
// write400 writes out a 400 error based on provided err
func write400(writer http.ResponseWriter, m Marshaller, err error) {
writer.WriteHeader(http.StatusInternalServerError)
httpErr := NewError400(err)
// Should never error out - it's a struct we know.
outData, _ := m.Marshal(httpErr)
writer.Write(outData)
}
// write403 writes out a 403 error
func write403(writer http.ResponseWriter, m Marshaller) {
writer.WriteHeader(http.StatusForbidden)
httpErr := NewError403()
// Should never error out - it's a struct we know.
outData, _ := m.Marshal(httpErr)
writer.Write(outData)
}
// doHook runs the Executable specified in route.Hook
// if the before parameter provided here (before or after) matches that
// of before value in route.Hook. It returns stdout and stderr
// from the executable, or an error.
func doHook(before bool, route Route, restContext RestContext, body string) (string, error) {
hook := route.Hook
if hook == nil {
return "", nil
}
hookInfo := fmt.Sprintf("Hook for %s %s: %s", route.Method, route.Pattern, hook.Executable)
if before && strings.ToLower(hook.When) != "before" {
return "", nil
}
if !before && strings.ToLower(hook.When) != "after" {
return "", nil
}
// Path variables (e.g., id in /foo/{id}) will be passed to the executable as a
// list of key=value pairs.
pathVars := restContext.PathVariables
// Query variables from this URL call will also be provided in the
// above manner.
queryVars := restContext.QueryVariables
// One more element is needed - body=<body> of the request will be provided
// as CLI argument.
clArgs := make([]string, len(pathVars)+len(queryVars)+1)
// Add body argument.
clArgs[0] = fmt.Sprintf("%s=%s", HookExecutableBodyArgument, body)
// Add pathVars arguments.
i := 1
for k, v := range pathVars {
clArgs[i] = fmt.Sprintf("%s=%s", k, v)
i++
}
// Add query argument.
for k, v := range queryVars {
clArgs[i] = fmt.Sprintf("%s=%s", k, v)
i++
}
var writer io.Writer
var err error
writer = nil
if hook.Output != "" {
log.Tracef(trace.Inside, "doHook(): Writing output of %s to %s", hookInfo, hook.Output)
writer, err = os.Create(hook.Output)
if err != nil {
return "", err
}
} else {
log.Tracef(trace.Inside, "doHook(): No output specified for %s", hookInfo)
}
cmd := exec.Command(hook.Executable, clArgs...)
out, errProcess := cmd.CombinedOutput()
log.Tracef(trace.Inside, "doHook(): Running hook %s with %s: %s / %v %T", hook.Executable, clArgs, string(out), errProcess, errProcess)
outStr := string(out)
log.Tracef(trace.Inside, "\n---------------------------------\n%s\n---------------------------------\n", outStr)
if writer != nil {
_, err = writer.Write(out)
if err != nil {
return outStr, err
}
}
return outStr, errProcess
}
// wrapHandler wraps the RestHandler function, which deals
// with application logic into an instance of http.HandlerFunc
// which deals with raw HTTP request and response. The wrapper
// is intended to transparently deal with converting data to/from
// the wire format into internal representations.
func wrapHandler(restHandler RestHandler, route Route) http.Handler {
// TODO
// This function is very long. Could we please break it up into a few smaller functions
// (with self-documenting names), which are called from within this function?
makeMessage := route.MakeMessage
if route.Hook != nil {
log.Tracef(trace.Inside, "wrapHandler(): %s %s %s", route.Method, route.Pattern, route.Hook.Executable)
}
if makeMessage != nil && reflect.TypeOf(makeMessage()) == requestType {
// This would mean the handler actually wants access to raw request/response
// Fine, then...
httpHandler := func(writer http.ResponseWriter, request *http.Request) {
err := request.ParseForm()
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
writer.Write([]byte(err.Error()))
return
}
user := context.Get(request, ContextKeyUser).(User)
restContext := RestContext{PathVariables: mux.Vars(request), QueryVariables: request.Form, User: user}
respReq := UnwrappedRestHandlerInput{writer, request}
marshaller := ContentTypeMarshallers["application/json"]
log.Tracef(trace.Inside, "doHook() will be called before %s %s", route.Method, route.Pattern)
out, err := doHook(true, route, restContext, "")
if err != nil {
write500(writer, marshaller, err)
return
}
userOk := false
if route.AuthZChecker == nil {
for _, role := range user.Roles {
if role.Name == RoleAdmin || role.Name == RoleService {
userOk = true
break
}
}
} else {
userOk = route.AuthZChecker(restContext)
}
if !userOk {
write403(writer, marshaller)
return
}
restContext.HookOutput = out
restHandler(respReq, restContext)
log.Tracef(trace.Inside, "doHook() will be called after %s %s", route.Method, route.Pattern)
_, err = doHook(false, route, restContext, "")
if err != nil {
write500(writer, marshaller, err)
return
}
}
return RomanaHandler{httpHandler}
}
httpHandler := func(writer http.ResponseWriter, request *http.Request) {
bufStr := ""
var inData interface{}
if makeMessage == nil {
inData = nil
} else {
inData = makeMessage()
}
var err error
contentType := writer.Header().Get("Content-Type")
// This should be ok because the middleware took care of negotiating
// only the content types we support
marshaller := ContentTypeMarshallers[contentType]
defaultMarshaller := ContentTypeMarshallers["application/json"]
if marshaller == nil {
// This should never happen... Just in case...
log.Tracef(trace.Inside, "No marshaler for [%s] found in %s, %s\n", contentType, ContentTypeMarshallers, ContentTypeMarshallers["application/json"])
writer.WriteHeader(http.StatusUnsupportedMediaType)
sct := SupportedContentTypesMessage
dataOut, _ := defaultMarshaller.Marshal(sct)
writer.Write(dataOut)
return
}
if inData != nil {
log.Tracef(trace.Inside, "httpHandler %s %s: inData addr: %d\n", route.Method, route.Pattern, &inData)
ct := request.Header.Get("content-type")
buf, err := ioutil.ReadAll(request.Body)
if buf == nil || len(buf) == 0 {
// Null input
inData = nil
} else {
bufStr = string(buf)
log.Tracef(trace.Inside, "Read %s\n", bufStr)
if err != nil {
// Error reading...
write500(writer, marshaller, err)
}
if unmarshaller, ok := ContentTypeMarshallers[ct]; ok {
log.Tracef(trace.Inside, "httpHandler %s %s: Attempting to unmarshal [%s] into %T", route.Method, route.Pattern, string(buf), inData)
err = unmarshaller.Unmarshal(buf, inData)
if err != nil {
// Error unmarshalling...
write400(writer, marshaller, err)
return
}
} else {
// Cannot unmarshal
dataOut, _ := marshaller.Marshal(SupportedContentTypesMessage)
writer.WriteHeader(http.StatusNotAcceptable)
writer.Write(dataOut)
return
}
}
}
err = request.ParseForm()
if err != nil {
// Cannot parse form...
write400(writer, marshaller, err)
return
}
var token string
if route.UseRequestToken {
if inData != nil {
v := reflect.Indirect(reflect.ValueOf(inData)).FieldByName(RequestTokenQueryParameter)
if v.IsValid() {
token = v.String()
log.Tracef(trace.Inside, "Token from payload %s (path %s)\n", token, route.Pattern)
} else {
tokens := request.Form[RequestTokenQueryParameter]
if len(tokens) != 1 {
token = uuid.New()
log.Tracef(trace.Inside, "Token created %s (path %s)\n", token, route.Pattern)
} else {
log.Tracef(trace.Inside, "Token from query string %s (path %s)\n", token, route.Pattern)
}
if len(tokens) == 0 {
// Token was not sent, the caller does it at his own
// risk. There will be no idempotence.
token = "1"
} else {
token = tokens[0]
}
}
}
}
user := context.Get(request, ContextKeyUser).(User)
restContext := RestContext{PathVariables: mux.Vars(request),
QueryVariables: request.Form,
RequestToken: token,
User: user,
}
userOk := false
if route.AuthZChecker == nil {
for _, role := range user.Roles {
if role.Name == RoleAdmin || role.Name == RoleService {
userOk = true
break
}
}
} else {
userOk = route.AuthZChecker(restContext)
}
if !userOk {
write403(writer, marshaller)
return
}
if route.Hook != nil {
log.Tracef(trace.Inside, "doHook() will be called before %s %s: %s", route.Method, route.Pattern, route.Hook.Executable)
}
out, err := doHook(true, route, restContext, bufStr)
if err != nil {
write500(writer, marshaller, err)
return
}
restContext.HookOutput = out
outData, err := restHandler(inData, restContext)
if err == nil {
if route.Hook != nil {
log.Tracef(trace.Inside, "doHook() will be called after %s %s: %s", route.Method, route.Pattern, route.Hook.Executable)
}
out, err = doHook(false, route, restContext, bufStr)
if err != nil {
write500(writer, marshaller, err)
return
}
var wireData []byte
switch outData := outData.(type) {
case Raw:
wireData = []byte(outData.Body)
default:
wireData, err = marshaller.Marshal(outData)
}
if err == nil {
writer.WriteHeader(http.StatusOK)
writer.Write(wireData)
return
}
write500(writer, marshaller, err)
return
} else {
switch err := err.(type) {
case HttpError:
writer.WriteHeader(err.StatusCode)
// Should never error out - it's a struct we know.
outData, _ := marshaller.Marshal(err)
writer.Write(outData)
default:
// Error reading...
write500(writer, marshaller, err)
}
return
}
}
return RomanaHandler{httpHandler}
}
// notFoundHandler adds functionality to send the body of a 404
// error as a document parseable by the client in accordance with
// its "Accept" declaration.
type notFoundHandler struct{}
func (n notFoundHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
// TODO answer with a 406 here?
accept := request.Header.Get("accept")
// Default to JSON.
contentType := "application/json"
if accept == "*/*" || accept == "" {
// Force json if it can take anything.
accept = "application/json"
}
format, err := negotiation.NegotiateAccept(accept, SupportedContentTypes)
var marshaller Marshaller
defaultMarshaller := ContentTypeMarshallers["application/json"]
if err == nil {
contentType = format.Value
writer.Header().Set("Content-Type", contentType)
marshaller = ContentTypeMarshallers[contentType]
}
// Error in negotiation or marshaller not found.
if err != nil || marshaller == nil {
// This should never happen... Just in case...
log.Tracef(trace.Inside, "No marshaler for [%s] found in %s, %s\n", contentType, ContentTypeMarshallers, ContentTypeMarshallers["application/json"])
writer.WriteHeader(http.StatusUnsupportedMediaType)
sct := SupportedContentTypesMessage
dataOut, _ := defaultMarshaller.Marshal(sct)
writer.Write(dataOut)
return
}
reqURL := request.URL
resource := ""
if reqURL != nil {
resource = reqURL.Path
if reqURL.RawQuery != "" {
resource += "?..."
}
if reqURL.Fragment != "" {
resource += "#..."
}
}
dataOut, _ := marshaller.Marshal(NewError404("URI", resource))
http.Error(writer, string(dataOut), http.StatusNotFound)
return
}
// NewRouter creates router for a new service.
func newRouter(routes []Route) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
router.NotFoundHandler = notFoundHandler{}
for _, route := range routes {
handler := route.Handler
if route.Hook != nil {
log.Tracef(trace.Inside, "Calling wrapHandler with %s %s %s", route.Method, route.Pattern, route.Hook.Executable)
}
wrappedHandler := wrapHandler(handler, route)
router.
Methods(route.Method).
Path(route.Pattern).
Handler(wrappedHandler)
}
return router
}
// List of supported content types to return in a
// 406 response.
var SupportedContentTypes = []string{"text/plain", "application/vnd.romana.v1+json", "application/vnd.romana+json", "application/json", "application/x-www-form-urlencoded"}
// Above list of supported content types wrapped in a
// struct for converion to JSON.
var SupportedContentTypesMessage = struct {
SupportedContentTypes []string `json:"supported_content_types"`
}{
SupportedContentTypes,
}
// Marshaller is capable of marshalling and unmarshalling data to/from the wire.
type Marshaller interface {
Marshal(v interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
}
// jsonMarshaller provides functionality to marshal/unmarshal
// data to/from JSON format.
type jsonMarshaller struct{}
// Marshal takes the provided interface and return []byte
// of its JSON representation.
func (j jsonMarshaller) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// Unmarshal attempts to fill the fields of provided interface
// from the provided JSON sructure.
func (j jsonMarshaller) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
// formMarshaller provides functionality to marshal/unmarshal
// data to/from HTML form format.
type formMarshaller struct{}
func (j formMarshaller) Marshal(v interface{}) ([]byte, error) {
retval := ""
vPtr := reflect.ValueOf(v)
vVal := vPtr.Elem()
vType := reflect.TypeOf(vVal.Interface())
for i := 0; i < vVal.NumField(); i++ {
metaField := vType.Field(i)
field := vVal.Field(i)
formKey := metaField.Tag.Get("form")
if len(retval) > 0 {
retval += "&"
}
retval += formKey + "="
log.Tracef(trace.Inside, "form key of %s is %s\n", metaField.Name, formKey)
str := ""
if metaField.Type == stringType {
str = field.Interface().(string)
} else {
toString := field.MethodByName("String")
log.Tracef(trace.Inside, "Looking for method String on %s: %s\n", field, toString)
if reflect.Zero(reflect.TypeOf(toString)) != toString {
toStringResult := toString.Call(nil)
str = toStringResult[0].String()
} else {
log.Tracef(trace.Inside, "Ignoring field %s of %s\n", metaField.Name, v)
continue
}
}
str = strings.TrimSpace(str)
retval += str
}
return []byte(retval), nil
}
// Unmarshal attempts to take a payload of an HTML form
// (key=value pairs separated by &, application/x-www-form-urlencoded
// MIME) and fill the v structure from it. It is not a universal method,
// and right now is limited to this simple functionality:
// 1. No support for multiple values for the same key (though HTML forms allow it).
// 2. interface v must be one of:
// a. map[string]interface{}
// b. Contain string fields for every field in the form OR,
// implement a Set<Field> method. (Structure tag "form" can be
// used to map the form key to the structure field if they are
// different). Here is a supported example:
// type NetIf struct {
// Mac string `form:"mac_address"` // Will get set because it's a string.
// IP net.IP `form:"ip_address"` // Will get set because of SetIP() method below.
// }
//
//func (netif *NetIf) SetIP(ip string) error {
// netif.IP = net.ParseIP(ip)
// if netif.IP == nil {
// return failedToParseNetif()
// }
// return nil
//}
func (f formMarshaller) Unmarshal(data []byte, v interface{}) error {
log.Tracef(trace.Inside, "Entering formMarshaller.Unmarshal()\n")
var err error
dataStr := string(data)
// We'll keep it simple - make a map and use mapstructure
vPtr := reflect.ValueOf(v)
vVal := vPtr.Elem()
vType := reflect.TypeOf(vVal.Interface())
kvPairs := strings.Split(dataStr, "&")
var m map[string]interface{}
if vType.Kind() == reflect.Map {
// If the output wanted is a map, then just use it as a map.
m = *(v.(*map[string]interface{}))
} else {
// Otherwise, first make a temporary map
m = make(map[string]interface{})
}
for i := range kvPairs {
kv := strings.Split(kvPairs[i], "=")
// Of course we have to do checking etc...
key := string(kv[0])
val := string(kv[1])
val2, err := url.QueryUnescape(val)
if err != nil {
return err
}
m[key] = val2
}
log.Tracef(trace.Inside, "Unmarshaled form %s to map %s\n", dataStr, m)
if vType.Kind() == reflect.Map {
// At this point we already have filled in the map,
// and map is the type we want, so we return.
return nil
}
for i := 0; i < vVal.NumField(); i++ {
metaField := vType.Field(i)
field := vVal.Field(i)
formKey := metaField.Tag.Get("form")
formValue := m[formKey]
log.Tracef(trace.Inside, "Value of %s is %s\n", metaField.Name, formValue)
if metaField.Type == stringType {
field.SetString(formValue.(string))
} else {
setterMethodName := fmt.Sprintf("Set%s", metaField.Name)
setterMethod := vPtr.MethodByName(setterMethodName)
log.Tracef(trace.Inside, "Looking for method %s on %s: %s\n", setterMethodName, vPtr, setterMethod)
if reflect.Zero(reflect.TypeOf(setterMethod)) != setterMethod {
valueArg := reflect.ValueOf(formValue)
valueArgs := []reflect.Value{valueArg}
result := setterMethod.Call(valueArgs)
errIfc := result[0].Interface()
if errIfc != nil {
return errIfc.(error)
}
} else {
return fmt.Errorf("Unsupported type of field %s: %s", metaField.Name, metaField.Type)
}
}
}
return err
}
// Raw is a type that can be returned from any service's
// route and the middleware will not try to marshal it.
type Raw struct {
Body string
}
// ContentTypeMarshallers maps MIME type to Marshaller instances
var ContentTypeMarshallers map[string]Marshaller = map[string]Marshaller{
// If no content type is sent, we will still assume it's JSON
// and try.
"": jsonMarshaller{},
"application/json": jsonMarshaller{},
"application/vnd.romana.v1+json": jsonMarshaller{},
"application/vnd.romana+json": jsonMarshaller{},
"application/x-www-form-urlencoded": formMarshaller{},
// "*/*": jsonMarshaller{},
}
type UnmarshallerMiddleware struct {
}
func NewUnmarshaller() *UnmarshallerMiddleware {
return &UnmarshallerMiddleware{}
}
type myReader struct{ *bytes.Buffer }
func (r myReader) Close() error { return nil }
// Unmarshals request body if needed. If not acceptable,
// returns an http.StatusNotAcceptable and this ends this
// request's lifecycle.
func (m UnmarshallerMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
ct := r.Header.Get(HeaderContentType)
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
if len(buf) == 0 {
next(w, r)
return
}
log.Tracef(trace.Inside, "Marshaler %s for %s\n", ContentTypeMarshallers[ct], ct)
if marshaller, ok := ContentTypeMarshallers[ct]; ok {
// Solution due to
// http://stackoverflow.com/questions/23070876/reading-body-of-http-request-without-modifying-request-state
// GG: I would not really judge this at all for this purpose until the
// whole thing about how to use the middlewares settles.
rdr2 := myReader{bytes.NewBuffer(buf)}
r.Body = rdr2
myMap := make(map[string]interface{})
marshaller.Unmarshal(buf, &myMap)
context.Set(r, ContextKeyUnmarshalledMap, myMap)
// TODO
context.Set(r, ContextKeyOriginalBody, buf)
context.Set(r, ContextKeyMarshaller, marshaller)
// Call the next middleware handler
next(w, r)
} else {
sct := SupportedContentTypesMessage
marshaller := ContentTypeMarshallers["application/json"]
dataOut, _ := marshaller.Marshal(sct)
w.WriteHeader(http.StatusNotAcceptable)
w.Write(dataOut)
}
}
type NegotiatorMiddleware struct {
}
func NewNegotiator() *NegotiatorMiddleware {
return &NegotiatorMiddleware{}
}
func (negotiator NegotiatorMiddleware) ServeHTTP(writer http.ResponseWriter, request *http.Request, next http.HandlerFunc) {
// TODO answer with a 406 here?
accept := request.Header.Get("accept")
if accept == "*/*" || accept == "" {
// Force json if it can take anything.
accept = "application/json"
}
format, err := negotiation.NegotiateAccept(accept, SupportedContentTypes)
if err == nil {
writer.Header().Set("Content-Type", format.Value)
}
next(writer, request)
}