-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
124 lines (97 loc) · 2.84 KB
/
http.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
package binder
import (
"io"
"mime/multipart"
"net/http"
"net/url"
)
var DefaultHttpBinder *HttpBinder
type HttpBindableRequest struct {
*http.Request
}
func (r HttpBindableRequest) GetBody() io.Reader {
return r.Body
}
func (r HttpBindableRequest) GetPathPattern() string {
return r.Pattern
}
func (r HttpBindableRequest) GetPathValue(key string) string {
return r.PathValue(key)
}
func (r HttpBindableRequest) GetQuery() url.Values {
return r.URL.Query()
}
func (r HttpBindableRequest) headersToValues(headers http.Header) url.Values {
values := url.Values{}
for key, val := range headers {
for _, v := range val {
values.Add(key, v)
}
}
return values
}
func (r HttpBindableRequest) GetHeaders() url.Values {
return r.headersToValues(r.Header)
}
func (r HttpBindableRequest) GetContentLength() int64 {
return r.ContentLength
}
func (r HttpBindableRequest) GetContentType() string {
return r.Header.Get("Content-Type")
}
func (r HttpBindableRequest) GetForm() (url.Values, error) {
if err := r.ParseForm(); err != nil {
return nil, err
}
return r.Form, nil
}
func (r HttpBindableRequest) GetMultipartForm(maxBodySize int64) (*multipart.Form, error) {
return r.MultipartForm, r.ParseMultipartForm(maxBodySize)
}
func NewHttpBindableRequest(r *http.Request) HttpBindableRequest {
return HttpBindableRequest{r}
}
// BindHttp binds an http.Request to a struct or map.
func BindHttp(r *http.Request, i interface{}) error {
return GetHttpBinder().Bind(r, i)
}
// BindHttpBody binds an http.Request body to a struct or map.
func BindHttpBody(r *http.Request, i interface{}) error {
return GetHttpBinder().BindBody(r, i)
}
func BindHttpPathParms(r *http.Request, i interface{}) error {
return GetHttpBinder().BindPathParams(r, i)
}
func BindHttpQueryParams(r *http.Request, i interface{}) error {
return GetHttpBinder().BindQueryParams(r, i)
}
func BindHttpHeaders(r *http.Request, i interface{}) error {
return GetHttpBinder().BindHeaders(r, i)
}
func GetHttpBinder() *HttpBinder {
if DefaultHttpBinder == nil {
DefaultHttpBinder = NewHttpBinder()
}
return DefaultHttpBinder
}
type HttpBinder struct {
Binder
}
func NewHttpBinder() *HttpBinder {
return &HttpBinder{GetBinder()}
}
func (b *HttpBinder) Bind(r *http.Request, i interface{}) error {
return b.Binder.Bind(NewHttpBindableRequest(r), i)
}
func (b *HttpBinder) BindBody(r *http.Request, i interface{}) error {
return b.Binder.BindBody(NewHttpBindableRequest(r), i)
}
func (b *HttpBinder) BindPathParams(r *http.Request, i interface{}) error {
return b.Binder.BindPathParams(NewHttpBindableRequest(r), i)
}
func (b *HttpBinder) BindQueryParams(r *http.Request, i interface{}) error {
return b.Binder.BindQueryParams(NewHttpBindableRequest(r), i)
}
func (b *HttpBinder) BindHeaders(r *http.Request, i interface{}) error {
return b.Binder.BindHeaders(NewHttpBindableRequest(r), i)
}