forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
177 lines (144 loc) · 4.09 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
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
package client
import (
"bufio"
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"net/url"
"time"
)
// RespError defines the response error.
type RespError struct {
code int
msg string
}
// Error implements the error interface.
func (e RespError) Error() string {
return e.msg
}
// Code returns the response code
func (e RespError) Code() int {
return e.code
}
// Response wraps the http.Response and other states.
type Response struct {
StatusCode int
Status string
Body io.ReadCloser
}
func (client *APIClient) get(ctx context.Context, path string, query url.Values, headers map[string][]string) (*Response, error) {
return client.sendRequest(ctx, "GET", path, query, nil, headers)
}
func (client *APIClient) post(ctx context.Context, path string, query url.Values, obj interface{}, headers map[string][]string) (*Response, error) {
body, err := objectToJSONStream(obj)
if err != nil {
return nil, err
}
return client.sendRequest(ctx, "POST", path, query, body, headers)
}
func (client *APIClient) postRawData(ctx context.Context, path string, query url.Values, data io.Reader, headers map[string][]string) (*Response, error) {
return client.sendRequest(ctx, "POST", path, query, data, headers)
}
func (client *APIClient) delete(ctx context.Context, path string, query url.Values, headers map[string][]string) (*Response, error) {
return client.sendRequest(ctx, "DELETE", path, query, nil, headers)
}
func (client *APIClient) hijack(ctx context.Context, path string, query url.Values, obj interface{}, header map[string][]string) (net.Conn, *bufio.Reader, error) {
body, err := objectToJSONStream(obj)
if err != nil {
return nil, nil, err
}
req, err := client.newRequest("POST", path, query, body, header)
if err != nil {
return nil, nil, err
}
req.Header.Set("Connection", "Upgrade")
req.Header.Set("Upgrade", "tcp")
req.Host = client.addr
conn, err := net.DialTimeout(client.proto, client.addr, defaultTimeout)
if err != nil {
return nil, nil, err
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(30 * time.Second)
}
clientconn := httputil.NewClientConn(conn, nil)
defer clientconn.Close()
if _, err := clientconn.Do(req); err != nil {
return nil, nil, err
}
rwc, br := clientconn.Hijack()
return rwc, br, nil
}
func (client *APIClient) newRequest(method, path string, query url.Values, body io.Reader, header map[string][]string) (*http.Request, error) {
fullPath := client.baseURL + client.GetAPIPath(path, query)
req, err := http.NewRequest(method, fullPath, body)
if err != nil {
return nil, err
}
for k, v := range header {
req.Header[k] = v
}
return req, err
}
func (client *APIClient) sendRequest(ctx context.Context, method, path string, query url.Values, body io.Reader, headers map[string][]string) (*Response, error) {
req, err := client.newRequest(method, path, query, body, headers)
if err != nil {
return nil, err
}
resp, err := cancellableDo(ctx, client.HTTPCli, req)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, RespError{code: resp.StatusCode, msg: string(data)}
}
return &Response{
StatusCode: resp.StatusCode,
Status: resp.Status,
Body: resp.Body,
}, nil
}
func cancellableDo(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
type contextResp struct {
response *http.Response
err error
}
ctxResp := make(chan contextResp, 1)
go func() {
resp, err := client.Do(req)
ctxResp <- contextResp{
response: resp,
err: err,
}
}()
select {
case <-ctx.Done():
tr := client.Transport.(*http.Transport)
tr.CancelRequest(req)
<-ctxResp
return nil, ctx.Err()
case resp := <-ctxResp:
return resp.response, resp.err
}
}
func objectToJSONStream(obj interface{}) (io.Reader, error) {
if obj != nil {
b, err := json.Marshal(obj)
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
return nil, nil
}