-
Notifications
You must be signed in to change notification settings - Fork 35
/
parse.go
186 lines (152 loc) · 3.74 KB
/
parse.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
package parse
import (
"net/http"
"net/url"
"strings"
"github.com/rancher/apiserver/pkg/types"
"github.com/rancher/apiserver/pkg/urlbuilder"
)
const (
maxFormSize = 2 * 1 << 20
)
var (
allowedFormats = map[string]bool{
"html": true,
"json": true,
"jsonl": true,
"yaml": true,
}
)
type ParsedURL struct {
Type string
Name string
Namespace string
Link string
Method string
Action string
Prefix string
SubContext map[string]string
Query url.Values
}
type URLParser func(rw http.ResponseWriter, req *http.Request, schemas *types.APISchemas) (ParsedURL, error)
type Parser func(apiOp *types.APIRequest, urlParser URLParser) error
func Parse(apiOp *types.APIRequest, urlParser URLParser) error {
var err error
if apiOp.Request == nil {
apiOp.Request, err = http.NewRequest("GET", "/", nil)
if err != nil {
return err
}
}
apiOp = types.StoreAPIContext(apiOp)
if apiOp.Method == "" {
apiOp.Method = parseMethod(apiOp.Request)
}
if apiOp.ResponseFormat == "" {
apiOp.ResponseFormat = parseResponseFormat(apiOp.Request)
}
// The response format is guaranteed to be set even in the event of an error
parsedURL, err := urlParser(apiOp.Response, apiOp.Request, apiOp.Schemas)
// wait to check error, want to set as much as possible
if apiOp.Type == "" {
apiOp.Type = parsedURL.Type
}
if apiOp.Name == "" {
apiOp.Name = parsedURL.Name
}
if apiOp.Link == "" {
apiOp.Link = parsedURL.Link
}
if apiOp.Action == "" {
apiOp.Action = parsedURL.Action
}
if apiOp.Query == nil {
apiOp.Query = parsedURL.Query
}
if apiOp.Method == "" && parsedURL.Method != "" {
apiOp.Method = parsedURL.Method
}
if apiOp.URLPrefix == "" {
apiOp.URLPrefix = parsedURL.Prefix
}
if apiOp.Namespace == "" {
apiOp.Namespace = parsedURL.Namespace
}
if apiOp.URLBuilder == nil {
// make error local to not override the outer error we have yet to check
var err error
apiOp.URLBuilder, err = urlbuilder.New(apiOp.Request, &urlbuilder.DefaultPathResolver{
Prefix: apiOp.URLPrefix,
}, apiOp.Schemas)
if err != nil {
return err
}
}
if err != nil {
return err
}
if apiOp.Schema == nil && apiOp.Schemas != nil {
apiOp.Schema = apiOp.Schemas.LookupSchema(apiOp.Type)
}
if apiOp.Schema != nil {
apiOp.Type = apiOp.Schema.ID
}
if apiOp.Schema != nil && apiOp.ErrorHandler != nil {
apiOp.ErrorHandler = apiOp.Schema.ErrorHandler
}
if err := ValidateMethod(apiOp); err != nil {
return err
}
return nil
}
func parseResponseFormat(req *http.Request) string {
format := req.URL.Query().Get("_format")
if format != "" {
format = strings.TrimSpace(strings.ToLower(format))
}
/* Format specified */
if allowedFormats[format] {
return format
}
// User agent has Mozilla and browser accepts */*
if IsBrowser(req, true) {
return "html"
}
if isYaml(req) {
return "yaml"
}
if isJSONL(req) {
return "jsonl"
}
return "json"
}
func isYaml(req *http.Request) bool {
return strings.Contains(req.Header.Get("Accept"), "application/yaml")
}
func isJSONL(req *http.Request) bool {
return strings.Contains(req.Header.Get("Accept"), "application/jsonl")
}
func parseMethod(req *http.Request) string {
method := req.URL.Query().Get("_method")
if method == "" {
method = req.Method
}
return method
}
func Body(req *http.Request) (types.APIObject, error) {
req.ParseMultipartForm(maxFormSize)
if req.MultipartForm != nil {
return valuesToBody(req.MultipartForm.Value), nil
}
if req.PostForm != nil && len(req.PostForm) > 0 {
return valuesToBody(map[string][]string(req.Form)), nil
}
return ReadBody(req)
}
func valuesToBody(input map[string][]string) types.APIObject {
result := map[string]interface{}{}
for k, v := range input {
result[k] = v
}
return toAPI(result)
}