-
Notifications
You must be signed in to change notification settings - Fork 279
/
kiterunner.go
97 lines (83 loc) · 2.35 KB
/
kiterunner.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
package proute
import (
"strings"
"github.com/assetnote/kiterunner/pkg/http"
"github.com/hashicorp/go-multierror"
)
func APIsToKiterunnerRoutes(api []API) ([]*http.Route, error) {
var merr *multierror.Error
ret := make([]*http.Route, 0)
for _, v := range api {
tmp, err := ToKiterunnerRoutes(v)
if err != nil {
multierror.Append(merr, err)
}
ret = append(ret, tmp...)
}
return ret, merr.ErrorOrNil()
}
func ToKiterunnerRoutes(api API) ([]*http.Route, error) {
var merr *multierror.Error
ret := make([]*http.Route, 0)
for _, v := range api.Routes {
// Skip these options since we don't actually care about the content here
r, err := v.ToKiterunner(api.Headers(true)...)
if err != nil {
multierror.Append(merr, err)
}
switch string(r.Method) {
case "HEAD", "OPTIONS", "CONNECT", "TRACE":
// we're biased. skip these since they're noisy
continue
case "GET", "POST", "PUT", "DELETE", "PATCH":
// these guys are alright
}
r.Source = api.ID
ret = append(ret, r)
}
return ret, merr.ErrorOrNil()
}
func (r Route) ToKiterunner(extraHeaders ...KV) (*http.Route, error) {
var err error
method := strings.TrimSpace(strings.ToUpper(r.Method))
switch method {
case "HEAD", "OPTIONS", "CONNECT", "TRACE":
// we're biased. skip these since they're noisy
case "GET", "POST", "PUT", "DELETE", "PATCH":
// these guys are alright
default:
method = "GET"
}
ret := &http.Route{
Path: []byte(r.path),
Method: http.Method(method),
}
for _, h := range r.Headers(true) {
ret.Headers = append(ret.Headers, http.Header{h.Key, h.Value})
}
for _, h := range extraHeaders {
ret.Headers = append(ret.Headers, http.Header{h.Key, h.Value})
}
ct := ContentTypeFormEncoded
if len(r.ContentType) > 0 {
ct = r.ContentType[0]
// Overwrite the form-data format with a proper boundary header
if strings.Contains(string(ct), "form-data") {
ct = "multipart/form-data; boundary=" + DefaultFormDataBoundary
}
}
ret.Body = r.Body(true, ct)
// only add content type if there is a body specified
// or if its a non-get type
if len(ret.Body) > 0 || (string(ret.Method) != string(http.GET)) {
ret.Headers = append(ret.Headers, http.Header{"Content-Type", string(ct)})
}
tmp, err := r.Path(false)
if err != nil {
return ret, err
}
ret.Path = []byte( tmp )
tmp, err = r.Query(false)
ret.Query = []byte(tmp)
return ret, err
}