-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_http.go
245 lines (206 loc) · 5.3 KB
/
client_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
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
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
"github.com/akutz/goof"
"golang.org/x/net/context/ctxhttp"
"github.com/codedellemc/rexray/libstorage/api/context"
"github.com/codedellemc/rexray/libstorage/api/types"
)
type headerKey int
const (
transactionHeaderKey headerKey = iota
instanceIDHeaderKey
localDevicesHeaderKey
authTokenHeaderKey
)
var (
registerCustomKeyOnce sync.Once
)
func (k headerKey) String() string {
switch k {
case transactionHeaderKey:
return types.TransactionHeader
case instanceIDHeaderKey:
return types.InstanceIDHeader
case localDevicesHeaderKey:
return types.LocalDevicesHeader
case authTokenHeaderKey:
return types.AuthorizationHeader
}
panic("invalid header key")
}
func (c *client) httpDo(
ctx types.Context,
method, path string,
payload, reply interface{}) (*http.Response, error) {
registerCustomKeyOnce.Do(func() {
context.RegisterCustomKeyWithContext(
ctx, transactionHeaderKey, context.CustomHeaderKey)
context.RegisterCustomKeyWithContext(
ctx, instanceIDHeaderKey, context.CustomHeaderKey)
context.RegisterCustomKeyWithContext(
ctx, localDevicesHeaderKey, context.CustomHeaderKey)
context.RegisterCustomKeyWithContext(
ctx, authTokenHeaderKey, context.CustomHeaderKey)
})
reqBody, err := encPayload(payload)
if err != nil {
return nil, err
}
url := fmt.Sprintf("http://%s%s", c.host, path)
req, err := http.NewRequest(method, url, reqBody)
if err != nil {
return nil, err
}
ctx = context.RequireTX(ctx)
tx := context.MustTransaction(ctx)
ctx = ctx.WithValue(transactionHeaderKey, tx)
if iid, ok := context.InstanceID(ctx); ok {
ctx = ctx.WithValue(instanceIDHeaderKey, iid)
} else if iidMap, ok := ctx.Value(
context.AllInstanceIDsKey).(types.InstanceIDMap); ok {
if len(iidMap) > 0 {
// iterate over the instance IDs and marshal them to strings,
// storing only the unique, marshaled output. any instance ID
// that specifies a service should be unique, but those that
// do not should collapse into a single header that only
// includes the driver
iidHdrs := map[string]bool{}
for _, iid := range iidMap {
szIID := iid.String()
if _, ok := iidHdrs[szIID]; !ok {
iidHdrs[szIID] = true
}
}
var iids []string
for iid := range iidHdrs {
iids = append(iids, iid)
}
ctx = ctx.WithValue(instanceIDHeaderKey, iids)
}
}
if lds, ok := context.LocalDevices(ctx); ok {
ctx = ctx.WithValue(localDevicesHeaderKey, lds)
} else if ldsMap, ok := ctx.Value(
context.AllLocalDevicesKey).(types.LocalDevicesMap); ok {
if len(ldsMap) > 0 {
var ldsess []fmt.Stringer
for _, lds := range ldsMap {
ldsess = append(ldsess, lds)
}
ctx = ctx.WithValue(localDevicesHeaderKey, ldsess)
}
}
if tok, ok := ctx.Value(context.EncodedAuthTokenKey).(string); ok {
ctx.WithField("secTok", tok).Debug("got auth token in httpDo")
ctx = ctx.WithValue(
authTokenHeaderKey,
fmt.Sprintf("Bearer %s", tok))
}
for key := range context.CustomHeaderKeys() {
var headerName string
switch tk := key.(type) {
case string:
headerName = tk
case fmt.Stringer:
headerName = tk.String()
default:
headerName = fmt.Sprintf("%v", key)
}
if headerName == "" {
continue
}
val := ctx.Value(key)
switch tv := val.(type) {
case string:
req.Header.Add(headerName, tv)
case fmt.Stringer:
req.Header.Add(headerName, tv.String())
case []string:
for _, sv := range tv {
req.Header.Add(headerName, sv)
}
case []fmt.Stringer:
for _, sv := range tv {
req.Header.Add(headerName, sv.String())
}
default:
if val != nil {
req.Header.Add(headerName, fmt.Sprintf("%v", val))
}
}
}
c.logRequest(req)
res, err := ctxhttp.Do(ctx, &c.Client, req)
if err != nil {
return nil, err
}
defer c.setServerName(res)
c.logResponse(res)
if res.StatusCode > 299 {
httpErr, err := goof.DecodeHTTPError(res.Body)
if err != nil {
return res, goof.WithField("status", res.StatusCode, "http error")
}
return res, httpErr
}
if req.Method != http.MethodHead && reply != nil {
if err := decRes(res.Body, reply); err != nil {
return nil, err
}
}
return res, nil
}
func (c *client) setServerName(res *http.Response) {
c.serverName = res.Header.Get(types.ServerNameHeader)
}
func (c *client) httpGet(
ctx types.Context,
path string,
reply interface{}) (*http.Response, error) {
return c.httpDo(ctx, "GET", path, nil, reply)
}
func (c *client) httpHead(
ctx types.Context,
path string) (*http.Response, error) {
return c.httpDo(ctx, "HEAD", path, nil, nil)
}
func (c *client) httpPost(
ctx types.Context,
path string,
payload interface{},
reply interface{}) (*http.Response, error) {
return c.httpDo(ctx, "POST", path, payload, reply)
}
func (c *client) httpDelete(
ctx types.Context,
path string,
reply interface{}) (*http.Response, error) {
return c.httpDo(ctx, "DELETE", path, nil, reply)
}
func encPayload(payload interface{}) (io.Reader, error) {
if payload == nil {
return nil, nil
}
buf, err := json.Marshal(payload)
if err != nil {
return nil, err
}
return bytes.NewReader(buf), nil
}
func decRes(body io.Reader, reply interface{}) error {
buf, err := ioutil.ReadAll(body)
if err != nil {
return err
}
if err := json.Unmarshal(buf, reply); err != nil {
return err
}
return nil
}