-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
executable file
·146 lines (125 loc) · 3.22 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
package goweb
import (
"context"
"net/http"
"net/url"
"reflect"
"strings"
)
var (
methodsWithBody = map[string]bool{"POST": true, "PUT": true, "PATCH": true}
)
// Request wraps net/http.Request to provide form parsing ability
type Request struct {
Req *http.Request
URL *url.URL
pathParam map[string]string
}
// NewRequest create a request obect from net/http.Request
func NewRequest(req *http.Request) *Request {
pathParam := make(map[string]string, 0)
// Get params
ctx := req.Context()
params := ctx.Value("params")
if params != nil {
if v, ok := params.(map[string]string); ok {
pathParam = v
}
}
return &Request{
Req: req,
URL: req.URL,
pathParam: pathParam,
}
}
// ParseParam parse http form
func (r *Request) ParseParam() error {
hasBody, _ := methodsWithBody[r.Req.Method]
if !hasBody {
return nil
}
contentType := r.Header("Content-Type")
if contentType == "" {
return ErrUnknowContentType
}
parts := strings.Split(contentType, ";")
parts[0] = strings.Trim(parts[0], " ")
if parts[0] == "application/x-www-form-urlencoded" {
return r.Req.ParseForm()
} else if parts[0] == "multipart/form-data" {
return r.Req.ParseMultipartForm(5 * (1 << 20))
}
return r.Req.ParseForm()
}
// PathParam get Path Param
func (r *Request) PathParam(name string) string {
return r.pathParam[name]
}
// Header get request header
func (r *Request) Header(name string) string {
return r.Req.Header.Get(name)
}
// Cookie get request cookie
func (r *Request) Cookie(name string) string {
cookie, err := r.Req.Cookie(name)
if err == http.ErrNoCookie {
return ""
}
return cookie.Value
}
// Param get a param from QueryString or Body
func (r *Request) Param(name string) string {
return r.Req.FormValue(name)
}
// FillForm populate a form for data validating
func (r *Request) FillForm(m interface{}) error {
formMeta, err := InspectForm(m)
if err != nil {
return err
}
form := reflect.Indirect(reflect.New(*formMeta.Type))
for fieldName, fieldConf := range formMeta.FieldMap {
var v string
if fieldConf.FromPath {
v = r.PathParam(fieldConf.ParamName)
} else if fieldConf.FromHeader {
v = r.Header(fieldConf.ParamName)
} else if fieldConf.FromCookie {
v = r.Cookie(fieldConf.ParamName)
} else {
v = r.Param(fieldConf.ParamName)
}
if fieldConf.IsRequired && v == "" {
return newFormError(FormErrMissingRequired, fieldConf.ParamName, nil)
}
value, err := StringCast(v, fieldConf.Type)
if err != nil {
if !fieldConf.IsRequired {
continue
}
return newFormError(FormErrTypeCannotCast, fieldConf.ParamName, err)
}
fieldV := form.FieldByName(fieldName)
vv := reflect.ValueOf(value)
if fieldV.CanSet() && vv.IsValid() {
fieldV.Set(vv)
}
}
mV := reflect.Indirect(reflect.ValueOf(m))
mV.Set(form)
// Validate required
if vd, ok := m.(FormValidator); ok {
err = vd.ValidateForm()
if err != nil {
return newFormError(FormErrNotAValidForm, "", err)
}
}
return nil
}
// SetContextValue set context value of the current request so that it can be passed down
func (r *Request) SetContextValue(key interface{}, value interface{}) {
req := r.Req
ctx := context.WithValue(req.Context(), key, value)
reqNew := req.WithContext(ctx)
r.Req = reqNew
}