-
Notifications
You must be signed in to change notification settings - Fork 0
/
httputil.go
308 lines (270 loc) · 9.01 KB
/
httputil.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package httputil
import (
"bytes"
"crypto"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"hash"
"io"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
"github.com/gopherd/doge/typeconv"
)
var (
ErrUnableToMarshalForm = errors.New("unable to marshal form")
ErrSign = errors.New("failed to verify sign")
)
const (
Unused = 0
charsetUTF8 = "charset=utf-8"
MIMEApplicationJSON = "application/json"
MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8
MIMEApplicationJavaScript = "application/javascript"
MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
MIMEApplicationXML = "application/xml"
MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8
MIMEApplicationForm = "application/x-www-form-urlencoded"
MIMEApplicationFormCharsetUTF8 = MIMEApplicationForm + "; " + charsetUTF8
MIMEApplicationProtobuf = "application/protobuf"
MIMEApplicationMsgpack = "application/msgpack"
MIMETextHTML = "text/html"
MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8
MIMETextPlain = "text/plain"
MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8
MIMEMultipartForm = "multipart/form-data"
MIMEOctetStream = "application/octet-stream"
)
const (
HeaderAccept = "Accept"
HeaderAcceptEncoding = "Accept-Encoding"
HeaderAllow = "Allow"
HeaderAuthorization = "Authorization"
HeaderContentDisposition = "Content-Disposition"
HeaderContentEncoding = "Content-Encoding"
HeaderContentLength = "Content-Length"
HeaderContentType = "Content-Type"
HeaderCookie = "Cookie"
HeaderSetCookie = "Set-Cookie"
HeaderIfModifiedSince = "If-Modified-Since"
HeaderLastModified = "Last-Modified"
HeaderLocation = "Location"
HeaderUpgrade = "Upgrade"
HeaderVary = "Vary"
HeaderWWWAuthenticate = "WWW-Authenticate"
HeaderXForwardedProto = "X-Forwarded-Proto"
HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
HeaderXForwardedFor = "X-Forwarded-For"
HeaderXRealIP = "X-Real-IP"
HeaderServer = "Server"
HeaderOrigin = "Origin"
HeaderAccessControlRequestMethod = "Access-Control-Request-Method"
HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers"
HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin"
HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods"
HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers"
HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers"
HeaderAccessControlMaxAge = "Access-Control-Max-Age"
HeaderStrictTransportSecurity = "Strict-Transport-Security"
HeaderXContentTypeOptions = "X-Content-Type-Options"
HeaderXXSSProtection = "X-XSS-Protection"
HeaderXFrameOptions = "X-Frame-Options"
HeaderContentSecurityPolicy = "Content-Security-Policy"
HeaderXCSRFToken = "X-CSRF-Token"
)
type Middleware interface {
Apply(http.Handler) http.Handler
}
// MiddlewareFunc implements Middleware interface
type MiddlewareFunc func(http.Handler) http.Handler
func (m MiddlewareFunc) Apply(h http.Handler) http.Handler {
return m(h)
}
type Result struct {
Response *http.Response
StatusCode int
Data []byte
Error error
}
func (result Result) Ok() bool { return result.StatusCode == http.StatusOK }
func (result Result) Status() string {
if result.Response == nil {
if result.Error != nil {
return result.Error.Error()
}
return ""
}
return result.Response.Status
}
func Get(url string) Result {
resp, err := http.Get(url)
return readResultFromResponse(resp, err)
}
func PostForm(url string, values url.Values) Result {
resp, err := http.PostForm(url, values)
return readResultFromResponse(resp, err)
}
func readResultFromResponse(resp *http.Response, err error) Result {
result := Result{
Response: resp,
Error: err,
}
if err != nil {
return result
}
result.StatusCode = resp.StatusCode
defer resp.Body.Close()
result.Data, result.Error = ioutil.ReadAll(resp.Body)
return result
}
type responseOptions struct {
status int
acceptType string
contentType string
}
func newResponseOptions() *responseOptions {
return &responseOptions{
status: http.StatusOK,
contentType: MIMETextPlain,
}
}
type ResponseOptions func(opts *responseOptions)
func mergeOptions(opts *responseOptions, options ...ResponseOptions) {
for _, o := range options {
o(opts)
}
}
func WithStatus(status int) ResponseOptions {
return func(opts *responseOptions) {
opts.status = status
}
}
func WithAcceptType(acceptType string) ResponseOptions {
return func(opts *responseOptions) {
opts.acceptType = acceptType
}
}
func WithContentType(contentType string) ResponseOptions {
return func(opts *responseOptions) {
opts.contentType = contentType
}
}
func Response(w http.ResponseWriter, body any, options ...ResponseOptions) error {
var opts = newResponseOptions()
mergeOptions(opts, options...)
if body != nil {
var marshaler MarshalFunc
if strings.Contains(opts.contentType, MIMEApplicationJSON) {
marshaler = json.Marshal
} else if strings.Contains(opts.contentType, MIMEApplicationXML) {
marshaler = xml.Marshal
} else if strings.Contains(opts.contentType, MIMEApplicationForm) {
marshaler = marshalForm
} else {
marshaler = typeconv.ToBytes
}
b, err := marshaler(body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return err
}
w.Header().Set("Content-Type", opts.contentType)
w.WriteHeader(opts.status)
_, err = w.Write(b)
return err
}
w.WriteHeader(opts.status)
return nil
}
func JSONResponse(w http.ResponseWriter, value any, options ...ResponseOptions) error {
return Response(w, value, append(options, WithContentType(MIMEApplicationJSONCharsetUTF8))...)
}
func XMLResponse(w http.ResponseWriter, value any, options ...ResponseOptions) error {
return Response(w, value, append(options, WithContentType(MIMEApplicationXMLCharsetUTF8))...)
}
func FormResponse(w http.ResponseWriter, value any, options ...ResponseOptions) error {
return Response(w, value, append(options, WithContentType(MIMEApplicationFormCharsetUTF8))...)
}
func TextResponse(w http.ResponseWriter, value string, options ...ResponseOptions) error {
return Response(w, value, append(options, WithContentType(MIMETextPlain))...)
}
type MarshalFunc func(any) ([]byte, error)
type FormMarshaler interface {
MarshalForm() ([]byte, error)
}
func marshalForm(v any) ([]byte, error) {
if marshaler, ok := v.(FormMarshaler); ok {
return marshaler.MarshalForm()
}
return nil, ErrUnableToMarshalForm
}
type Signer func(key, keyField, signField string, args url.Values) (string, error)
type byKey [][2]string
func (by byKey) Len() int { return len(by) }
func (by byKey) Less(i, j int) bool { return by[i][0] < by[j][0] }
func (by byKey) Swap(i, j int) { by[i], by[j] = by[j], by[i] }
func HashSigner(hasher hash.Hash) Signer {
return func(key, keyField, signField string, args url.Values) (string, error) {
params := make([][2]string, 0, len(args))
for k, vs := range args {
if len(vs) > 0 && k != signField {
params = append(params, [2]string{k, vs[0]})
}
}
sort.Sort(byKey(params))
var buf bytes.Buffer
for _, pair := range params {
buf.WriteString(pair[0])
buf.WriteByte('=')
buf.WriteString(pair[1])
buf.WriteByte('&')
}
buf.WriteString(keyField)
buf.WriteByte('=')
buf.WriteString(key)
hasher.Write(buf.Bytes())
sum := hasher.Sum(nil)
sign := fmt.Sprintf("%x", sum)
return sign, nil
}
}
func MD5Signer() Signer { return HashSigner(crypto.MD5.New()) }
func SHA256Signer() Signer { return HashSigner(crypto.SHA256.New()) }
func VerifySign(signer Signer, key, keyField, signField string, args url.Values) error {
expected, err := signer(key, keyField, signField, args)
if err != nil {
return err
}
if args == nil || args.Get(signField) != expected {
return ErrSign
}
return nil
}
func PostFormJSON(httpc *http.Client, url_ string, data url.Values, res any) error {
response, err := httpc.PostForm(url_, data)
if response != nil && response.Body != nil {
defer response.Body.Close()
}
if err != nil {
return err
}
if response.StatusCode >= 400 {
return errors.New(response.Status)
}
if res == nil {
return nil
}
return json.NewDecoder(response.Body).Decode(res)
}
type Pager struct {
Total int64 `json:"total"`
Curr int `json:"curr"`
Limit int `json:"limit"`
List any `json:"list"`
}