-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse.go
276 lines (238 loc) · 5.88 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
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
package handler
import (
"fmt"
"net/http"
)
// MaxLevel defines maximum tree depth for incoming request data and files.
const MaxLevel = 127
type dataTree map[string]any
type fileTree map[string]any
// parsePostForm parses incoming request body into data tree.
func parsePostForm(r *http.Request) (dataTree, error) {
data := make(dataTree, 2)
if r.PostForm != nil {
for k, v := range r.PostForm {
err := data.push(k, v)
if err != nil {
return nil, err
}
}
}
return data, nil
}
// parseMultipartData parses incoming request body into data tree.
func parseMultipartData(r *http.Request) (dataTree, error) {
data := make(dataTree, 2)
if r.MultipartForm != nil {
for k, v := range r.MultipartForm.Value {
err := data.push(k, v)
if err != nil {
return nil, err
}
}
}
return data, nil
}
// pushes value into data tree.
func (dt dataTree) push(k string, v []string) error {
keys := make([]string, 1)
fetchIndexes(k, &keys)
if len(keys) <= MaxLevel {
return dt.mount(keys, v)
}
return nil
}
func invalidMultipleValuesErr(key string) error {
return fmt.Errorf(
"invalid multiple values to key '%+v' in tree",
key,
)
}
// mount mounts data tree recursively.
//
// This is written to handle this very-edge case
// Assume that we have the following POST data
//
// _token: NM8eor1JFGRLxfaTNHanGX4en0ZMFtatdz1Muu5Z
// _method: PUT
// _http_referrer: http://localhost/admin/article
// name: Rerum non omnis dicta occaecati dignissimos culpa commodi.
// options:
// options[0][id]: 97b64557-24ad-4099-88f2-0d275874d2e8
// options[0][order_priority]: 1000
// options[0][name]: Adipisci eaque vero laborum reprehenderit id ipsam deserunt.
// id: 97b64557-19ba-49bd-bdec-783e32fbc6e8
// _save_action: save_and_back
//
// If we don't ignore empty options, we will lose the whole array of data in key options[x]
// So we will ignore it and process the array of data in options[x] if those present in the request.
// The same is done for fileTree data structure underneath
func (dt dataTree) mount(keys, v []string) error {
if len(keys) == 0 {
return nil
}
done, err := prepareTreeNode(dt, keys, v)
if err != nil {
return err
}
if done {
return nil
}
// getting all elements of non-associated array
if len(keys) == 2 && keys[1] == "" {
dt[keys[0]] = v
return nil
}
// only getting the last elements
if len(keys) == 1 && len(v) > 0 {
dt[keys[0]] = v[len(v)-1]
return nil
}
if len(keys) == 1 {
dt[keys[0]] = v
return nil
}
res, ok := dt[keys[0]].(dataTree)
if !ok {
dt[keys[0]] = make(dataTree, 1)
res, ok = dt[keys[0]].(dataTree)
if !ok {
return nil
}
}
return res.mount(keys[1:], v)
}
func prepareTreeNode[T dataTree | fileTree, V []string | []*FileUpload](tree T, i []string, v V) (bool, error) {
if _, ok := tree[i[0]]; !ok {
tree[i[0]] = make(T)
return false, nil
}
_, isBranch := tree[i[0]].(T)
isDataInTreeEmpty := isDataEmpty(tree[i[0]])
isIncomingValueEmpty := isDataEmpty(v)
isLeafNodeIncoming := len(i) == 1 || (len(i) == 2 && len(i[1]) == 0)
if !isBranch {
if !isDataInTreeEmpty {
// we have a leaf node with value, but there is incoming branch data in the input
if len(i) > 1 && len(i[1]) > 0 {
return true, invalidMultipleValuesErr(i[0])
}
// we have a non-empty leaf node and there is incoming empty value
if isIncomingValueEmpty {
return true, nil
}
}
// we have an empty leaf node and there is incoming value
if isDataInTreeEmpty && !isIncomingValueEmpty {
tree[i[0]] = make(T)
return false, nil
}
}
if isBranch && isLeafNodeIncoming {
// we have a branch with tree data, but there is incoming value in the input
if !isIncomingValueEmpty {
return true, invalidMultipleValuesErr(i[0])
}
// we have a branch with tree data but there is incoming empty value
if isIncomingValueEmpty {
return true, nil
}
}
return false, nil
}
func isDataEmpty(v any) bool {
switch actualV := v.(type) {
case string:
return len(actualV) == 0
case []string:
return len(actualV) == 0 || (len(actualV) == 1 && len(actualV[0]) == 0)
case []*FileUpload:
return len(actualV) == 0 || (len(actualV) == 1 && actualV[0] == nil)
default:
return v == nil
}
}
// parse incoming dataTree request into JSON (including contentMultipart form dataTree)
func parseUploads(r *http.Request, uid, gid int) (*Uploads, error) {
u := &Uploads{
tree: make(fileTree),
list: make([]*FileUpload, 0),
}
for k, v := range r.MultipartForm.File {
files := make([]*FileUpload, 0, len(v))
for _, f := range v {
files = append(files, NewUpload(f, uid, gid))
}
u.list = append(u.list, files...)
err := u.tree.push(k, files)
if err != nil {
return nil, err
}
}
return u, nil
}
// pushes new file upload into it's proper place.
func (ft fileTree) push(k string, v []*FileUpload) error {
keys := make([]string, 1)
fetchIndexes(k, &keys)
if len(keys) <= MaxLevel {
return ft.mount(keys, v)
}
return nil
}
// mount mounts data tree recursively.
func (ft fileTree) mount(i []string, v []*FileUpload) error {
if len(i) == 0 {
return nil
}
done, err := prepareTreeNode(ft, i, v)
if err != nil {
return err
}
if done {
return nil
}
switch {
case len(i) == 2 && i[1] == "":
// non-associated array of elements
ft[i[0]] = v
return nil
case len(i) == 1 && len(v) > 0:
ft[i[0]] = v[0]
return nil
case len(i) == 1:
ft[i[0]] = v
return nil
}
return ft[i[0]].(fileTree).mount(i[1:], v)
}
// fetchIndexes parses input name and splits it into separate indexes list.
func fetchIndexes(s string, keys *[]string) {
const empty = ""
var (
pos int
ch string
)
for _, c := range s {
ch = string(c)
switch ch {
case " ":
// ignore all spaces
continue
case "[":
pos = 1
continue
case "]":
if pos == 1 {
*keys = append(*keys, empty)
}
pos = 2
default:
if pos == 1 || pos == 2 {
*keys = append(*keys, empty)
}
(*keys)[len(*keys)-1] += ch
pos = 0
}
}
}