-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
144 lines (115 loc) · 3.26 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
package Sex
import (
str "strings"
re "regexp"
"net/http"
"errors"
"bytes"
"time"
)
type Response struct {
http.ResponseWriter
Body []byte
Status int
}
type Request struct {
*http.Request
PathVars map[string]string
Conf Prop
Writer *Response
}
func (self *Request) JsonBody(v interface{}) error {
encoded := new(bytes.Buffer)
encoded.ReadFrom(self.Body)
return FromJson(encoded.Bytes(), v)
}
func (self *Request) RawBody(b *[]byte) error {
body := new(bytes.Buffer)
_, err := body.ReadFrom(self.Body)
*b = body.Bytes()
return err
}
func (self *Request) MkResponse() *Response {
return self.Writer
}
func (self *Response) SetBody(v []byte) *Response {
self.Body = v
return self
}
func (self *Response) SetStatus(code int) *Response {
self.Status = 200
return self
}
func (self *Response) SetCookie(key string, value string, expires time.Duration) *Response {
cookie := &http.Cookie {
Name: key,
Value: value,
Expires: time.Now().Add(expires),
}
http.SetCookie(self, cookie)
return self
}
func GetPathPattern(t string) string {
var_patt := re.MustCompile(`\{(\w{1,}):{0,1}(.{0,})\}`)
path_tmplt := str.Split(t, "/")
path_pattern := "/"
for i := 0; i < len(path_tmplt); i++ {
if path_tmplt[i] == "" {
continue
}
path_tmplt[i] += "/"
values := var_patt.FindStringSubmatch(path_tmplt[i])
if len(values)>=2 {
if values[2] == "" {
path_pattern += `[a-zA-Z0-9_\ ]{1,}`
} else {
path_pattern += values[2]
}
} else {
path_pattern += path_tmplt[i]
}
}
path_pattern = fixPath(path_pattern)
path_pattern += "$"
if path_pattern == "^$" {
path_pattern = "^/$"
}
return path_pattern
}
func GetPathVars(t string, p string) (map[string]string, error) {
var_patt := re.MustCompile(`\{(\w{1,}):{0,1}(.{0,})\}`)
path_tmplt := str.Split(t, "/")
path := str.Split(p, "/")
if len(path) != len(path_tmplt) {
return map[string]string {}, errors.New("Path don't match with the path template")
}
path_vars_values := []map[string]string{}
for i := 0; i < len(path); i++ {
values := var_patt.FindStringSubmatch(path_tmplt[i])
tmpl_patt := re.MustCompile("")
if len(values)==3 {
tmpl_patt = re.MustCompile(values[2])
if tmpl_patt.MatchString(path[i]){
path_vars_values = append(path_vars_values, map[string]string {
"name": values[1],
"value": path[i],
})
} else {
return map[string]string {}, errors.New(
Fmt("Variable \"%s\" need that \"%s\" match to \"%s\"", values[1], path[i], values[2]),
)
}
continue
}
path_vars_values = append(path_vars_values, map[string]string{
"value": path[i],
})
}
path_vars := map[string]string{}
for _, v := range path_vars_values {
if _, exist := v["name"]; exist {
path_vars[v["name"]] = v["value"]
}
}
return path_vars, nil
}