-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreq.go
128 lines (114 loc) · 2.64 KB
/
req.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
package req
import (
"fmt"
"io"
"net/http"
"net/url"
)
const (
hdrUserAgentKey = "User-Agent"
hdrUserAgentValue = "req/v3 (https://github.com/imroc/req)"
hdrLocationKey = "Location"
hdrContentTypeKey = "Content-Type"
plainTextContentType = "text/plain; charset=utf-8"
jsonContentType = "application/json; charset=utf-8"
xmlContentType = "text/xml; charset=utf-8"
formContentType = "application/x-www-form-urlencoded"
)
type kv struct {
Key string
Value string
}
// ContentDisposition represents parameters in `Content-Disposition`
// MIME header of multipart request.
type ContentDisposition struct {
kv []kv
}
// Add adds a new key-value pair of Content-Disposition
func (c *ContentDisposition) Add(key, value string) *ContentDisposition {
c.kv = append(c.kv, kv{Key: key, Value: value})
return c
}
func (c *ContentDisposition) string() string {
if c == nil {
return ""
}
s := ""
for _, kv := range c.kv {
s += fmt.Sprintf("; %s=%q", kv.Key, kv.Value)
}
return s
}
// FileUpload represents a "form-data" multipart
type FileUpload struct {
// "name" parameter in `Content-Disposition`
ParamName string
// "filename" parameter in `Content-Disposition`
FileName string
// The file to be uploaded.
File io.Reader
// According to the HTTP specification, this should be nil,
// but some servers may not follow the specification and
// requires `Content-Disposition` parameters more than just
// "name" and "filename".
ExtraContentDisposition *ContentDisposition
}
func cloneCookies(cookies []*http.Cookie) []*http.Cookie {
if len(cookies) == 0 {
return nil
}
c := make([]*http.Cookie, len(cookies))
copy(c, cookies)
return c
}
func cloneHeaders(hdrs http.Header) http.Header {
if hdrs == nil {
return nil
}
h := make(http.Header)
for k, vs := range hdrs {
for _, v := range vs {
h.Add(k, v)
}
}
return h
}
// TODO: change to generics function when generics are commonly used.
func cloneRequestMiddleware(m []RequestMiddleware) []RequestMiddleware {
if len(m) == 0 {
return nil
}
mm := make([]RequestMiddleware, len(m))
copy(mm, m)
return mm
}
func cloneResponseMiddleware(m []ResponseMiddleware) []ResponseMiddleware {
if len(m) == 0 {
return nil
}
mm := make([]ResponseMiddleware, len(m))
copy(mm, m)
return mm
}
func cloneUrlValues(v url.Values) url.Values {
if v == nil {
return nil
}
vv := make(url.Values)
for key, values := range v {
for _, value := range values {
vv.Add(key, value)
}
}
return vv
}
func cloneMap(h map[string]string) map[string]string {
if h == nil {
return nil
}
m := make(map[string]string)
for k, v := range h {
m[k] = v
}
return m
}