-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommons.go
More file actions
191 lines (156 loc) · 3.4 KB
/
commons.go
File metadata and controls
191 lines (156 loc) · 3.4 KB
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
package literoute
import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
func cleanURL(url *string) {
_url := *url
urlLength := len(_url)
if urlLength > 1 {
if (*url)[urlLength-1:] == "/" {
*url = (*url)[:urlLength-1]
cleanURL(url)
}
}
}
func valid(path string) bool {
pathLength := len(path)
if pathLength > 1 && path[pathLength-1:] == "/" {
return false
}
return true
}
func DecodeQuery(path string) string {
if path == "" {
return ""
}
encodedPath, err := url.QueryUnescape(path)
if err != nil {
return path
}
return encodedPath
}
func DecodeURL(uri string) string {
u, err := url.Parse(uri)
if err != nil {
return uri
}
return u.String()
}
func GetHost(r *http.Request) string {
if host := r.Host; host != "" {
return host
}
return r.URL.Host
}
func (ctx *context) GetHeader(name string) string {
return ctx.request.Header.Get(name)
}
func GetForm(r *http.Request, postMaxMemory int64, resetBody bool) (form map[string][]string, found bool) {
if form := r.Form; len(form) > 0 {
return form, true
}
if form := r.PostForm; len(form) > 0 {
return form, true
}
if m := r.MultipartForm; m != nil {
if len(m.Value) > 0 {
return m.Value, true
}
}
var bodyCopy []byte
if resetBody {
if m := r.Method; m == "POST" || m == "PUT" || m == "PATCH" {
bodyCopy, _ = GetBody(r, resetBody)
if len(bodyCopy) == 0 {
return nil, false
}
} else {
resetBody = false
}
}
err := r.ParseMultipartForm(postMaxMemory)
if resetBody {
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyCopy))
}
if err != nil && err != http.ErrNotMultipart {
return nil, false
}
if form := r.Form; len(form) > 0 {
return form, true
}
if form := r.PostForm; len(form) > 0 {
return form, true
}
if m := r.MultipartForm; m != nil {
if len(m.Value) > 0 {
return m.Value, true
}
}
return nil, false
}
func (ctx *context) FormValueDefault(name string, def string) string {
if form, has := ctx.form(); has {
if v := form[name]; len(v) > 0 {
return v[0]
}
}
return def
}
func FormValueDefault(r *http.Request, name string, def string, postMaxMemory int64, resetBody bool) string {
if form, has := GetForm(r, postMaxMemory, resetBody); has {
if v := form[name]; len(v) > 0 {
return v[0]
}
}
return def
}
func GetBody(r *http.Request, resetBody bool) ([]byte, error) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
if resetBody {
r.Body = ioutil.NopCloser(bytes.NewBuffer(data))
}
return data, nil
}
var UnixEpochTime = time.Unix(0, 0)
func IsZeroTime(t time.Time) bool {
return t.IsZero() || t.Equal(UnixEpochTime)
}
var ParseTimeRFC3339 = func(ctx Context, text string) (t time.Time, err error) {
t, err = time.Parse(time.RFC3339, text)
if err != nil {
return http.ParseTime(text)
}
return
}
var ParseTimeRFC3339Nano = func(ctx Context, text string) (t time.Time, err error) {
t, err = time.Parse(time.RFC3339Nano, text)
if err != nil {
return http.ParseTime(text)
}
return
}
func FormatTimeRFC3339(t time.Time) string {
return t.Format(time.RFC3339)
}
func FormatTimeRFC3339Nano(t time.Time) string {
return t.Format(time.RFC3339Nano)
}
func parseHeader(headerValue string) []string {
in := strings.Split(headerValue, ",")
out := make([]string, 0, len(in))
for _, value := range in {
v := strings.TrimSpace(strings.Split(value, ";")[0])
if v != "" {
out = append(out, v)
}
}
return out
}