-
Notifications
You must be signed in to change notification settings - Fork 69
/
http.go
108 lines (94 loc) · 2.29 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
// Package http implements crazy ideas for http optimizations that should be
// mostly std compatible.
package http
import (
"bytes"
"context"
"io"
"net/http"
"net/url"
"strings"
"golang.org/x/sync/errgroup"
)
// Client represents http client.
type Client interface {
Do(r *http.Request) (*http.Response, error)
}
// NewRequest creates a new http.Request.
func NewRequest(ctx context.Context, method string, u *url.URL) (*http.Request, error) {
return http.NewRequestWithContext(ctx, method, u.String(), http.NoBody)
}
func initRequest(req *http.Request, contentType string) {
if req.Header == nil {
req.Header = make(http.Header)
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
}
// SetBody sets request body.
func SetBody(req *http.Request, body io.Reader, contentType string) {
initRequest(req, contentType)
req.Body = io.NopCloser(body)
switch v := body.(type) {
case *bytes.Buffer:
req.ContentLength = int64(v.Len())
buf := v.Bytes()
req.GetBody = func() (io.ReadCloser, error) {
r := bytes.NewReader(buf)
return io.NopCloser(r), nil
}
case *bytes.Reader:
req.ContentLength = int64(v.Len())
snapshot := *v
req.GetBody = func() (io.ReadCloser, error) {
r := snapshot
return io.NopCloser(&r), nil
}
case *strings.Reader:
req.ContentLength = int64(v.Len())
snapshot := *v
req.GetBody = func() (io.ReadCloser, error) {
r := snapshot
return io.NopCloser(&r), nil
}
}
}
// SetCloserBody sets request body which should be closed after request.
func SetCloserBody(req *http.Request, body io.ReadCloser, contentType string) {
initRequest(req, contentType)
req.Body = body
}
// CreateBodyWriter is a helper to create a reader from a writer body.
func CreateBodyWriter(cb func(w io.Writer) error) io.ReadCloser {
piper, pipew := io.Pipe()
wg := new(errgroup.Group)
wg.Go(func() (rerr error) {
defer func() {
if rerr != nil {
_ = pipew.CloseWithError(rerr)
} else {
_ = pipew.Close()
}
}()
return cb(pipew)
})
return bodyReader{
r: piper,
w: pipew,
wg: wg,
}
}
type bodyReader struct {
r *io.PipeReader
w *io.PipeWriter
wg *errgroup.Group
}
func (w bodyReader) Read(p []byte) (int, error) {
return w.r.Read(p)
}
func (w bodyReader) Close() (rerr error) {
rerr = w.r.Close()
_ = w.wg.Wait()
return rerr
}