forked from bytedance/go-tagexpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
101 lines (88 loc) · 1.85 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
package binding
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"net/url"
)
type requestWithFileHeader interface {
Request
GetFileHeaders() (map[string][]*multipart.FileHeader, error)
}
type Request interface {
GetMethod() string
GetQuery() url.Values
GetContentType() string
GetHeader() http.Header
GetCookies() []*http.Cookie
GetBody() ([]byte, error)
GetPostForm() (url.Values, error)
GetForm() (url.Values, error)
}
func wrapRequest(req *http.Request) Request {
r := &httpRequest{
Request: req,
}
if getBodyCodec(r) == bodyForm && req.PostForm == nil {
b, _ := r.GetBody()
if b != nil {
req.ParseMultipartForm(defaultMaxMemory)
}
}
return r
}
type httpRequest struct {
*http.Request
}
func (r *httpRequest) GetMethod() string {
return r.Method
}
func (r *httpRequest) GetQuery() url.Values {
return r.URL.Query()
}
func (r *httpRequest) GetContentType() string {
return r.GetHeader().Get("Content-Type")
}
func (r *httpRequest) GetHeader() http.Header {
return r.Header
}
func (r *httpRequest) GetCookies() []*http.Cookie {
return r.Cookies()
}
func (r *httpRequest) GetBody() ([]byte, error) {
body, _ := r.Body.(*Body)
if body != nil {
body.Reset()
return body.bodyBytes, nil
}
switch r.Method {
case "POST", "PUT", "PATCH", "DELETE":
var buf bytes.Buffer
_, err := io.Copy(&buf, r.Body)
r.Body.Close()
if err != nil {
return nil, err
}
body = &Body{
Buffer: &buf,
bodyBytes: buf.Bytes(),
}
r.Body = body
return body.bodyBytes, nil
default:
return nil, nil
}
}
func (r *httpRequest) GetPostForm() (url.Values, error) {
return r.PostForm, nil
}
func (r *httpRequest) GetForm() (url.Values, error) {
return r.Form, nil
}
func (r *httpRequest) GetFileHeaders() (map[string][]*multipart.FileHeader, error) {
if r.MultipartForm == nil {
return nil, nil
}
return r.MultipartForm.File, nil
}