This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
100 lines (78 loc) · 2.25 KB
/
value.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
package form
import (
"mime/multipart"
"net/http"
"net/url"
)
type value struct {
Form url.Values
PostForm url.Values
MultipartForm *multipart.Form
formCount map[string]int
postFormCount map[string]int
multipartValueCount map[string]int
multipartFileCount map[string]int
}
func newValue(r *http.Request) *value {
v := &value{}
count := 0
if len(r.Form) > 0 {
v.Form = r.Form
count++
}
if len(r.PostForm) > 0 {
v.PostForm = r.PostForm
count++
}
if r.MultipartForm != nil {
v.MultipartForm = r.MultipartForm
count++
}
if count == 0 {
return nil
}
v.formCount = map[string]int{}
v.postFormCount = map[string]int{}
v.multipartValueCount = map[string]int{}
v.multipartFileCount = map[string]int{}
return v
}
func (r *value) Shift(name string) (str string) {
switch {
case r.MultipartForm != nil &&
len(r.MultipartForm.Value[name]) > r.multipartValueCount[name]:
str = r.MultipartForm.Value[name][r.multipartValueCount[name]]
r.multipartValueCount[name]++
case len(r.Form[name]) > r.formCount[name]:
str = r.Form[name][r.formCount[name]]
r.formCount[name]++
case len(r.PostForm[name]) > r.postFormCount[name]:
str = r.PostForm[name][r.postFormCount[name]]
r.postFormCount[name]++
}
return
}
func (r *value) All(name string) []string {
strs := []string{}
switch {
case r.MultipartForm != nil &&
len(r.MultipartForm.Value[name]) > r.multipartValueCount[name]:
strs = r.MultipartForm.Value[name][r.multipartValueCount[name]:]
r.multipartValueCount[name] = len(r.MultipartForm.Value[name])
case len(r.Form[name]) > r.formCount[name]:
strs = r.Form[name][r.formCount[name]:]
r.formCount[name] = len(r.Form[name])
case len(r.PostForm[name]) > r.postFormCount[name]:
strs = r.PostForm[name][r.postFormCount[name]:]
r.postFormCount[name] = len(r.PostForm[name])
}
return strs
}
func (r *value) FileShift(name string) (fileHeader *multipart.FileHeader) {
if r.MultipartForm != nil &&
len(r.MultipartForm.File[name]) > r.multipartFileCount[name] {
fileHeader = r.MultipartForm.File[name][r.multipartFileCount[name]]
r.multipartFileCount[name]++
}
return
}