-
Notifications
You must be signed in to change notification settings - Fork 65
/
http.go
296 lines (254 loc) · 8.9 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
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
// Copyright © 2020, 2023 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package http
import (
"bytes"
"context"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"strings"
"github.com/attestantio/go-eth2-client/spec"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
)
// Error represents an http error.
type Error struct {
Method string
Endpoint string
StatusCode int
Data []byte
}
func (e Error) Error() string {
return fmt.Sprintf("%s failed with status %d: %s", e.Method, e.StatusCode, e.Data)
}
// get sends an HTTP get request and returns the body.
// If the response from the server is a 404 this will return nil for both the reader and the error.
func (s *Service) get(ctx context.Context, endpoint string) (io.Reader, error) {
// #nosec G404
log := s.log.With().Str("id", fmt.Sprintf("%02x", rand.Int31())).Str("address", s.address).Str("endpoint", endpoint).Logger()
log.Trace().Msg("GET request")
url, err := url.Parse(fmt.Sprintf("%s%s", strings.TrimSuffix(s.base.String(), "/"), endpoint))
if err != nil {
return nil, errors.Wrap(err, "invalid endpoint")
}
opCtx, cancel := context.WithTimeout(ctx, s.timeout)
req, err := http.NewRequestWithContext(opCtx, http.MethodGet, url.String(), nil)
if err != nil {
cancel()
return nil, errors.Wrap(err, "failed to create GET request")
}
s.addExtraHeaders(req)
req.Header.Set("Accept", "application/json")
resp, err := s.client.Do(req)
if err != nil {
cancel()
return nil, errors.Wrap(err, "failed to call GET endpoint")
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
// Nothing found. This is not an error, so we return nil on both counts.
cancel()
return nil, nil
}
data, err := io.ReadAll(resp.Body)
if err != nil {
cancel()
return nil, errors.Wrap(err, "failed to read GET response")
}
statusFamily := resp.StatusCode / 100
if statusFamily != 2 {
cancel()
log.Trace().Int("status_code", resp.StatusCode).Str("data", string(data)).Msg("GET failed")
return nil, Error{
Method: http.MethodGet,
StatusCode: resp.StatusCode,
Endpoint: endpoint,
Data: data,
}
}
cancel()
log.Trace().Str("response", string(data)).Msg("GET response")
return bytes.NewReader(data), nil
}
// post sends an HTTP post request and returns the body.
func (s *Service) post(ctx context.Context, endpoint string, body io.Reader) (io.Reader, error) {
// #nosec G404
log := s.log.With().Str("id", fmt.Sprintf("%02x", rand.Int31())).Str("address", s.address).Str("endpoint", endpoint).Logger()
if e := log.Trace(); e.Enabled() {
bodyBytes, err := io.ReadAll(body)
if err != nil {
return nil, errors.New("failed to read request body")
}
body = bytes.NewReader(bodyBytes)
e.Str("body", string(bodyBytes)).Msg("POST request")
}
url, err := url.Parse(fmt.Sprintf("%s%s", strings.TrimSuffix(s.base.String(), "/"), endpoint))
if err != nil {
return nil, errors.Wrap(err, "invalid endpoint")
}
opCtx, cancel := context.WithTimeout(ctx, s.timeout)
req, err := http.NewRequestWithContext(opCtx, http.MethodPost, url.String(), body)
if err != nil {
cancel()
return nil, errors.Wrap(err, "failed to create POST request")
}
s.addExtraHeaders(req)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", "go-eth2-client/0.18.0")
}
resp, err := s.client.Do(req)
if err != nil {
cancel()
return nil, errors.Wrap(err, "failed to call POST endpoint")
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
cancel()
return nil, errors.Wrap(err, "failed to read POST response")
}
statusFamily := resp.StatusCode / 100
if statusFamily != 2 {
log.Trace().Int("status_code", resp.StatusCode).Str("data", string(data)).Msg("POST failed")
cancel()
return nil, Error{
Method: http.MethodPost,
StatusCode: resp.StatusCode,
Endpoint: endpoint,
Data: data,
}
}
cancel()
log.Trace().Str("response", string(data)).Msg("POST response")
return bytes.NewReader(data), nil
}
func (s *Service) addExtraHeaders(req *http.Request) {
for k, v := range s.extraHeaders {
req.Header.Add(k, v)
}
}
// responseMetadata returns metadata related to responses.
type responseMetadata struct {
Version spec.DataVersion `json:"version"`
}
type httpResponse struct {
statusCode int
contentType ContentType
consensusVersion spec.DataVersion
body []byte
}
// get2 sends an HTTP get request and returns the body.
// If the response from the server is a 404 this will return nil for both the reader and the error.
func (s *Service) get2(ctx context.Context, endpoint string) (*httpResponse, error) {
ctx, span := otel.Tracer("attestantio.go-eth2-client.http").Start(ctx, "get2")
defer span.End()
// #nosec G404
log := s.log.With().Str("id", fmt.Sprintf("%02x", rand.Int31())).Str("address", s.address).Str("endpoint", endpoint).Logger()
log.Trace().Msg("GET request")
url, err := url.Parse(fmt.Sprintf("%s%s", strings.TrimSuffix(s.base.String(), "/"), endpoint))
if err != nil {
return nil, errors.Wrap(err, "invalid endpoint")
}
opCtx, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
req, err := http.NewRequestWithContext(opCtx, http.MethodGet, url.String(), nil)
if err != nil {
cancel()
return nil, errors.Wrap(err, "failed to create GET request")
}
s.addExtraHeaders(req)
// Prefer SSZ, JSON if not.
req.Header.Set("Accept", "application/octet-stream;q=1,application/json;q=0.9")
span.AddEvent("Sending request")
resp, err := s.client.Do(req)
if err != nil {
span.RecordError(errors.New("Request failed"))
return nil, errors.Wrap(err, "failed to call GET endpoint")
}
defer resp.Body.Close()
log = log.With().Int("status_code", resp.StatusCode).Logger()
res := &httpResponse{
statusCode: resp.StatusCode,
}
if resp.StatusCode == http.StatusNotFound {
// Nothing found. Note that this is not considered an error.
span.RecordError(errors.New("endpoint not found"))
log.Debug().Msg("Endpoint not found")
return res, nil
}
if resp.StatusCode == http.StatusNoContent {
// Nothing returned. Note that this is not considered an error.
span.AddEvent("Received empty response")
log.Trace().Msg("Endpoint returned no content")
return res, nil
}
res.body, err = io.ReadAll(resp.Body)
if err != nil {
span.RecordError(err)
log.Warn().Err(err).Msg("Failed to read body")
return nil, errors.Wrap(err, "failed to read body")
}
statusFamily := resp.StatusCode / 100
if statusFamily != 2 {
span.SetStatus(codes.Error, fmt.Sprintf("Status code %d", resp.StatusCode))
trimmedResponse := bytes.ReplaceAll(bytes.ReplaceAll(res.body, []byte{0x0a}, []byte{}), []byte{0x0d}, []byte{})
log.Debug().Int("status_code", resp.StatusCode).RawJSON("response", trimmedResponse).Msg("GET failed")
return nil, Error{
Method: http.MethodGet,
StatusCode: resp.StatusCode,
Endpoint: endpoint,
Data: res.body,
}
}
res.consensusVersion, err = consensusVersionFromResp(resp)
if err != nil {
return nil, errors.Wrap(err, "failed to parse consensus version")
}
res.contentType, err = contentTypeFromResp(resp)
if err != nil {
// For now, assume that unknown type is JSON.
log.Debug().Err(err).Msg("Failed to obtain content type; assuming JSON")
res.contentType = ContentTypeJSON
}
return res, nil
}
func consensusVersionFromResp(resp *http.Response) (spec.DataVersion, error) {
respConsensusVersions, exists := resp.Header["Eth-Consensus-Version"]
if !exists {
return spec.DataVersionUnknown, errors.New("no consensus version supplied in response")
}
if len(respConsensusVersions) != 1 {
return spec.DataVersionUnknown, fmt.Errorf("malformed consensus version (%d entries)", len(respConsensusVersions))
}
res := spec.DataVersionUnknown
if err := res.UnmarshalJSON([]byte(fmt.Sprintf("%q", respConsensusVersions[0]))); err != nil {
return spec.DataVersionUnknown, errors.Wrap(err, "failed to parse consensus version")
}
return res, nil
}
func contentTypeFromResp(resp *http.Response) (ContentType, error) {
respContentTypes, exists := resp.Header["Content-Type"]
if !exists {
return ContentTypeUnknown, errors.New("no content type supplied in response")
}
if len(respContentTypes) != 1 {
return ContentTypeUnknown, fmt.Errorf("malformed content type (%d entries)", len(respContentTypes))
}
return ParseFromMediaType(respContentTypes[0])
}