-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_caller.go
290 lines (269 loc) · 8.71 KB
/
http_caller.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
package core
import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/byteplus-sdk/sdk-go/core/logs"
"github.com/byteplus-sdk/sdk-go/core/option"
"github.com/google/uuid"
"github.com/valyala/fasthttp"
"google.golang.org/protobuf/proto"
)
const netErrMark = "[netErr]"
func NewHTTPCaller(context *Context) *HTTPCaller {
return &HTTPCaller{context: context}
}
type HTTPCaller struct {
context *Context
}
func (c *HTTPCaller) DoJSONRequest(url string, request interface{},
response proto.Message, options *option.Options) error {
reqBytes, err := c.jsonMarshal(request)
if err != nil {
logs.Error("json marshal request fail, err:%s url:%s", err.Error(), url)
return err
}
headers := c.buildHeaders(options, "application/json")
url = c.withOptionQueries(options, url)
rspBytes, err := c.doHttpRequest(url, headers, reqBytes, options.Timeout)
if err != nil {
return err
}
err = proto.Unmarshal(rspBytes, response)
if err != nil {
logs.Error("unmarshal response fail, err:%s url:%s", err.Error(), url)
return err
}
return nil
}
func (c *HTTPCaller) jsonMarshal(request interface{}) ([]byte, error) {
reqBytes, err := json.Marshal(request)
if err != nil {
return nil, err
}
reqBytes = fasthttp.AppendGzipBytes(nil, reqBytes)
return reqBytes, nil
}
func (c *HTTPCaller) DoPBRequest(url string, request proto.Message,
response proto.Message, options *option.Options) error {
reqBytes, err := c.marshal(request)
if err != nil {
logs.Error("marshal request fail, err:%s url:%s", err.Error(), url)
return err
}
headers := c.buildHeaders(options, "application/x-protobuf")
url = c.withOptionQueries(options, url)
rspBytes, err := c.doHttpRequest(url, headers, reqBytes, options.Timeout)
if err != nil {
return err
}
err = proto.Unmarshal(rspBytes, response)
if err != nil {
logs.Error("unmarshal response fail, err:%s url:%s", err.Error(), url)
return err
}
return nil
}
func (c *HTTPCaller) marshal(request proto.Message) ([]byte, error) {
reqBytes, err := proto.Marshal(request)
if err != nil {
return nil, err
}
reqBytes = fasthttp.AppendGzipBytes(nil, reqBytes)
return reqBytes, nil
}
func (c *HTTPCaller) buildHeaders(options *option.Options, contentType string) map[string]string {
headers := make(map[string]string)
headers["Content-Encoding"] = "gzip"
headers["Accept-Encoding"] = "gzip"
headers["Content-Type"] = contentType
headers["Accept"] = "application/x-protobuf"
headers["Tenant-Id"] = c.context.tenantId
c.withOptionHeaders(headers, options)
return headers
}
func (c *HTTPCaller) withOptionHeaders(headers map[string]string, options *option.Options) {
if len(options.RequestId) == 0 {
requestId := uuid.NewString()
logs.Info("use requestId generated by sdk: '%s' ", requestId)
headers["Request-Id"] = requestId
} else {
headers["Request-Id"] = options.RequestId
}
if !options.DataDate.IsZero() {
headers["Content-Date"] = options.DataDate.Format(time.RFC3339)
}
if options.DataIsEnd {
headers["Content-End"] = "true"
}
if options.ServerTimeout > 0 {
headers["Timeout-Millis"] = strconv.Itoa(int(options.ServerTimeout.Milliseconds()))
}
for k, v := range options.Headers {
headers[k] = v
}
}
func (c *HTTPCaller) withAuthHeaders(req *fasthttp.Request, reqBytes []byte) {
if c.context.UseVolcAuth() {
c.withVolcAuthHeaders(req)
return
}
c.withAirAuthHeaders(req, reqBytes)
}
func (c *HTTPCaller) withAirAuthHeaders(req *fasthttp.Request, reqBytes []byte) {
var (
// Gets the second-level timestamp of the current time.
// The server only supports the second-level timestamp.
// The 'ts' must be the current time.
// When current time exceeds a certain time, such as 5 seconds, of 'ts',
// the signature will be invalid and cannot pass authentication
ts = strconv.FormatInt(time.Now().Unix(), 10)
// Use sub string of UUID as "nonce", too long will be wasted.
// You can also use 'ts' as' nonce'
nonce = uuid.NewString()[:8]
// calculate the authentication signature
signature = c.calSignature(reqBytes, ts, nonce)
)
req.Header.Set("Tenant-Ts", ts)
req.Header.Set("Tenant-Nonce", nonce)
req.Header.Set("Tenant-Signature", signature)
}
func (c *HTTPCaller) withVolcAuthHeaders(req *fasthttp.Request) {
VolcSign(req, c.context.volcCredentials)
}
func (c *HTTPCaller) calSignature(reqBytes []byte, ts, nonce string) string {
var (
token = c.context.token
tenantId = c.context.tenantId
)
// Splice in the order of "token", "HttpBody", "tenant_id", "ts", and "nonce".
// The order must not be mistaken.
// String need to be encoded as byte arrays by UTF-8
shaHash := sha256.New()
shaHash.Write([]byte(token))
shaHash.Write(reqBytes)
shaHash.Write([]byte(tenantId))
shaHash.Write([]byte(ts))
shaHash.Write([]byte(nonce))
return fmt.Sprintf("%x", shaHash.Sum(nil))
}
func (c *HTTPCaller) withOptionQueries(options *option.Options, url string) string {
var queriesParts []string
if options.Stage != "" {
queriesParts = append(queriesParts, "stage="+options.Stage)
}
for name, value := range options.Queries {
queriesParts = append(queriesParts, name+"="+value)
}
optionQuery := strings.Join(queriesParts, "&")
if optionQuery == "" {
return url
}
if strings.Contains(url, "?") {
url = url + "&" + optionQuery
} else {
url = url + "?" + optionQuery
}
return url
}
func (c *HTTPCaller) doHttpRequest(url string, headers map[string]string,
reqBytes []byte, timeout time.Duration) ([]byte, error) {
request := c.acquireRequest(url, headers, reqBytes)
response := fasthttp.AcquireResponse()
defer func() {
fasthttp.ReleaseRequest(request)
fasthttp.ReleaseResponse(response)
}()
c.withAuthHeaders(request, reqBytes)
start := time.Now()
defer func() {
logs.Debug("http url:%s, cost:%s", url, time.Now().Sub(start))
}()
logs.Trace("http request header:\n%s", string(request.Header.Header()))
err := c.smartDoRequest(timeout, request, response)
if err != nil {
ReportRequestException(metricsKeyInvokeError, url, start, err)
if strings.Contains(strings.ToLower(err.Error()), "timeout") {
logs.Error("do http request timeout, msg:%s url:%s", err.Error(), url)
return nil, errors.New(netErrMark + " timeout")
}
logs.Error("do http request occur error, msg:%s url:%s", err.Error(), url)
return nil, err
}
logs.Trace("http response headers:\n%s", string(response.Header.Header()))
if response.StatusCode() != fasthttp.StatusOK {
c.logHttpResponse(url, response)
ReportRequestError(metricsKeyInvokeError, url, start, response.StatusCode(), "invoke-fail")
return nil, errors.New(netErrMark + "http status not 200")
}
ReportRequestSuccess(metricsKeyInvokeSuccess, url, start)
return decompressResponse(url, response)
}
func (c *HTTPCaller) acquireRequest(url string,
headers map[string]string, reqBytes []byte) *fasthttp.Request {
request := fasthttp.AcquireRequest()
request.Header.SetMethod(fasthttp.MethodPost)
request.SetRequestURI(url)
for k, v := range headers {
request.Header.Set(k, v)
}
request.SetBodyRaw(reqBytes)
if len(c.context.hostHeader) > 0 {
request.SetHost(c.context.hostHeader)
}
return request
}
func (c *HTTPCaller) smartDoRequest(timeout time.Duration,
request *fasthttp.Request, response *fasthttp.Response) error {
var err error
if c.context.hostHeader != "" {
var httpCli = c.context.hostHTTPCli
if timeout > 0 {
err = httpCli.DoTimeout(request, response, timeout)
} else {
err = httpCli.Do(request, response)
}
} else {
var httpCli = c.context.defaultHTTPCli
if timeout > 0 {
err = httpCli.DoTimeout(request, response, timeout)
} else {
err = httpCli.Do(request, response)
}
}
return err
}
func (c *HTTPCaller) logHttpResponse(url string, response *fasthttp.Response) {
rspBytes, _ := decompressResponse(url, response)
if len(rspBytes) > 0 {
logs.Error("http status not 200, url:%s code:%d headers:\n%s\n body:\n%s",
url, response.StatusCode(), string(response.Header.Header()), string(rspBytes))
return
}
logs.Error("http status not 200, url:%s code:%d headers:\n%s\n",
url, response.StatusCode(), string(response.Header.Header()))
}
func decompressResponse(url string, response *fasthttp.Response) ([]byte, error) {
contentEncoding := strings.ToLower(strings.TrimSpace(string(response.Header.Peek("Content-Encoding"))))
switch contentEncoding {
case "gzip":
respBodyBytes, err := response.BodyGunzip()
if err != nil {
logs.Error("decompress gzip resp occur error, msg:%v url:%s header:\n%s",
err, url, &response.Header)
return nil, err
}
return respBodyBytes, nil
case "":
return response.Body(), nil
default:
logs.Error("receive unsupported response content encoding:%s url:%s header:\n%s",
contentEncoding, url, &response.Header)
err := errors.New("unsupported resp content encoding:" + contentEncoding)
return nil, err
}
}