-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrequest.go
221 lines (192 loc) · 4.37 KB
/
request.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
package foolgo
import (
"errors"
"io"
"mime/multipart"
"net/http"
"os"
"strings"
)
type Request struct {
request *http.Request
controller_name string
action_name string
form_parsed bool
rewrite_params map[string]string
}
func NewRequest(r *http.Request) *Request {
request_instance := &Request{
request: r,
}
return request_instance
}
func (this *Request) SetController(name string) {
if name == "" {
return
}
this.controller_name = name
}
func (this *Request) SetAction(name string) {
if name == "" {
return
}
this.action_name = name
}
func (this *Request) GetController() string {
return this.controller_name
}
func (this *Request) GetAction() string {
return this.action_name
}
func (this *Request) Method() string {
return this.request.Method
}
func (this *Request) Uri() string {
return this.request.RequestURI
}
func (this *Request) Url() string {
return this.request.URL.Path
}
func (this *Request) IP() string {
ips := this.Proxy()
if len(ips) > 0 && ips[0] != "" {
rip := strings.Split(ips[0], ":")
return rip[0]
}
ip := strings.Split(this.request.RemoteAddr, ":")
if len(ip) > 0 {
if ip[0] != "[" {
return ip[0]
}
}
return "127.0.0.1"
}
func (this *Request) Scheme() string {
if this.request.URL.Scheme != "" {
return this.request.URL.Scheme
}
if this.request.TLS == nil {
return "http"
}
return "https"
}
func (this *Request) Proxy() []string {
if ips := this.Header("X-Forwarded-For"); ips != "" {
return strings.Split(ips, ",")
}
return []string{}
}
func (this *Request) Header(key string) string {
return this.request.Header.Get(key)
}
// Get cookie
func (this *Request) Cookie(key string) string { /*{{{*/
cookie, err := this.request.Cookie(key)
if err != nil {
return ""
}
return cookie.Value
} /*}}}*/
// Get request param by key
func (this *Request) Param(key string) string { /*{{{*/
if this.rewrite_params != nil {
if d, ok := this.rewrite_params[key]; ok == true {
return d
}
}
err := this.ParseMultiForm()
if err != nil {
return ""
}
return this.request.Form.Get(key)
} /*}}}*/
// Get all request params passed by GET method
func (this *Request) ParamGet() (data map[string]string) { /*{{{*/
err := this.ParseMultiForm()
if err != nil {
return nil
}
if this.request.Form == nil {
return this.rewrite_params
}
data = make(map[string]string)
for k, v := range this.request.Form {
data[k] = v[0]
}
if this.rewrite_params != nil {
for k, v := range this.rewrite_params {
data[k] = v
}
}
return data
} /*}}}*/
// Get all request params passed by POST method
func (this *Request) ParamPost() (data map[string]interface{}) { /*{{{*/
err := this.ParseMultiForm()
if err != nil {
return nil
}
if this.request.PostForm == nil {
return nil
}
data = make(map[string]interface{})
for k, v := range this.request.PostForm {
if len(v) > 1 {
data[k] = v
} else {
data[k] = v[0]
}
}
return data
} /*}}}*/
func (this *Request) ParseMultiForm() error { /*{{{*/
if this.form_parsed == true || this.request.Form != nil || this.request.PostForm != nil || this.request.MultipartForm != nil {
return nil
}
this.form_parsed = true
if strings.Contains(this.Header("Content-Type"), "multipart/form-data") {
if err := this.request.ParseMultipartForm(32 << 20); err != nil {
return errors.New("Error parsing request body:" + err.Error())
}
} else if err := this.request.ParseForm(); err != nil {
return errors.New("Error parsing request body:" + err.Error())
}
return nil
} /*}}}*/
// Get all upload files
func (this *Request) GetUploadFiles(key string) ([]*multipart.FileHeader, error) { /*{{{*/
this.ParseMultiForm()
if this.request.MultipartForm == nil {
return nil, nil
}
files, ok := this.request.MultipartForm.File[key]
if ok {
return files, nil
}
return nil, http.ErrMissingFile
} /*}}}*/
// Save upload file
func (this *Request) MoveUploadFile(fromfile, tofile string) error { /*{{{*/
file, _, err := this.request.FormFile(fromfile)
if err != nil {
return err
}
defer file.Close()
f, err := os.OpenFile(tofile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer f.Close()
io.Copy(f, file)
return nil
} /*}}}*/
type Size interface {
Size() int64
}
// Get upload file size
func (this *Request) GetFileSize(file *multipart.File) int64 {
if sizeInterface, ok := (*file).(Size); ok {
return sizeInterface.Size()
}
return -1
}