-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.v
127 lines (110 loc) · 2.82 KB
/
request.v
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
module very
import net.http
import net.urllib
import json
pub struct Request {
http.Request
mut:
form map[string]string
files map[string][]http.FileData
url_ urllib.URL
query map[string]string
}
pub fn new_request(req &http.Request, url urllib.URL) &Request {
return &Request{
Request: req
url_: url
query: http.parse_form(url.raw_query)
}
}
@[inline]
pub fn (mut req Request) get_custom_header(key string) !string {
return req.header.get_custom(key)!
}
@[inline]
pub fn (mut req Request) get_header(key http.CommonHeader) string {
return req.header.get(key) or { '' }
}
@[inline]
pub fn (mut req Request) referer() string {
return req.header.get(.referer) or { '' }
}
@[inline]
pub fn (mut req Request) host() string {
return req.url_.host
}
@[inline]
pub fn (mut req Request) path() string {
return req.url_.path
}
@[inline]
pub fn (mut req Request) query(key string) string {
return req.query[key] or { '' }
}
@[inline]
pub fn (mut req Request) add_query(key string, value string) {
req.query[key] = value
}
@[inline]
pub fn (mut req Request) file(name string) ![]http.FileData {
return req.files[name] or { return error('have no upload file ${name}.') }
}
@[inline]
pub fn (mut req Request) form(name string) string {
return req.form[name] or { '' }
}
@[inline]
pub fn (mut req Request) parse_form() ! {
if req.form.len == 0 {
req.form, req.files = req.parse_form_from_request() or { return err }
}
}
pub fn (mut req Request) cookie(key string) !string {
mut cookie_header := req.get_header(.cookie)
cookie_header = ' ' + cookie_header
cookie := if cookie_header.contains(';') {
cookie_header.find_between(' ${key}=', ';')
} else {
cookie_header.find_between(' ${key}=', '\r')
}
if cookie != '' {
return cookie.trim_space()
}
return error('cookie not found')
}
fn (mut req Request) parse_form_from_request() !(map[string]string, map[string][]http.FileData) {
mut form := map[string]string{}
mut files := map[string][]http.FileData{}
if req.method in [http.Method.post, .put, .patch] {
ct := req.header.get(.content_type) or { '' }.split(';').map(it.trim_left(' \t'))
if 'multipart/form-data' in ct {
boundary := ct.filter(it.starts_with('boundary='))
if boundary.len != 1 {
return error('detected more that one form-data boundary')
}
form, files = http.parse_multipart_form(req.data, boundary[0][9..])
} else {
form = http.parse_form(req.data)
}
}
return form, files
}
pub fn (mut req Request) client_ip() string {
mut ip := req.header.get(.x_forwarded_for) or { '' }
if ip == '' {
ip = req.header.get_custom('X-Real-Ip') or { '' }
}
if ip.contains(',') {
ip = ip.all_before(',')
}
if ip == '' {
ip = req.header.get_custom('Remote-Addr') or { '' }
}
return ip
}
pub fn (mut req Request) body_parse[T]() !T {
if req.data.len > 0 {
return json.decode(T, req.data)!
}
return T{}
}