/
http.go
407 lines (346 loc) · 12.2 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/util/http.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package util
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"net/http"
"net/url"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/grafana/dskit/flagext"
"github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"gopkg.in/yaml.v3"
)
// IsRequestBodyTooLarge returns true if the error is "http: request body too large".
func IsRequestBodyTooLarge(err error) bool {
return err != nil && strings.Contains(err.Error(), "http: request body too large")
}
// BasicAuth configures basic authentication for HTTP clients.
type BasicAuth struct {
Username string `yaml:"basic_auth_username"`
Password flagext.Secret `yaml:"basic_auth_password"`
}
func (b *BasicAuth) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
f.StringVar(&b.Username, prefix+"basic-auth-username", "", "HTTP Basic authentication username. It overrides the username set in the URL (if any).")
f.Var(&b.Password, prefix+"basic-auth-password", "HTTP Basic authentication password. It overrides the password set in the URL (if any).")
}
// IsEnabled returns false if basic authentication isn't enabled.
func (b BasicAuth) IsEnabled() bool {
return b.Username != "" || b.Password.String() != ""
}
// WriteJSONResponse writes some JSON as a HTTP response.
func WriteJSONResponse(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// We ignore errors here, because we cannot do anything about them.
// Write will trigger sending Status code, so we cannot send a different status code afterwards.
// Also this isn't internal error, but error communicating with client.
_, _ = w.Write(data)
}
// WriteYAMLResponse writes some YAML as a HTTP response.
func WriteYAMLResponse(w http.ResponseWriter, v interface{}) {
// There is not standardised content-type for YAML, text/plain ensures the
// YAML is displayed in the browser instead of offered as a download
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
data, err := yaml.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// We ignore errors here, because we cannot do anything about them.
// Write will trigger sending Status code, so we cannot send a different status code afterwards.
// Also this isn't internal error, but error communicating with client.
_, _ = w.Write(data)
}
// WriteTextResponse sends message as text/plain response with 200 status code.
func WriteTextResponse(w http.ResponseWriter, message string) {
w.Header().Set("Content-Type", "text/plain")
// Ignore inactionable errors.
_, _ = w.Write([]byte(message))
}
// WriteHTMLResponse sends message as text/html response with 200 status code.
func WriteHTMLResponse(w http.ResponseWriter, message string) {
w.Header().Set("Content-Type", "text/html")
// Ignore inactionable errors.
_, _ = w.Write([]byte(message))
}
// RenderHTTPResponse either responds with JSON or a rendered HTML page using the passed in template
// by checking the Accepts header.
func RenderHTTPResponse(w http.ResponseWriter, v interface{}, t *template.Template, r *http.Request) {
accept := r.Header.Get("Accept")
if strings.Contains(accept, "application/json") {
WriteJSONResponse(w, v)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err := t.Execute(w, v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// StreamWriteYAMLResponse stream writes data as http response
func StreamWriteYAMLResponse(w http.ResponseWriter, iter chan interface{}, logger log.Logger) {
w.Header().Set("Content-Type", "application/yaml")
for v := range iter {
data, err := yaml.Marshal(v)
if err != nil {
level.Error(logger).Log("msg", "yaml marshal failed", "err", err)
continue
}
_, err = w.Write(data)
if err != nil {
level.Error(logger).Log("msg", "write http response failed", "err", err)
return
}
}
}
// CompressionType for encoding and decoding requests and responses.
type CompressionType int
// Values for CompressionType
const (
NoCompression CompressionType = iota
RawSnappy
Gzip
)
// ParseProtoReader parses a compressed proto from an io.Reader.
// You can pass in an optional RequestBuffers.
// If no error is returned, the returned actualSize is the size of the uncompressed proto.
func ParseProtoReader(ctx context.Context, reader io.Reader, expectedSize, maxSize int, buffers *RequestBuffers, req proto.Message, compression CompressionType) (actualSize int, err error) {
sp := opentracing.SpanFromContext(ctx)
if sp != nil {
sp.LogFields(otlog.Event("util.ParseProtoReader[start reading]"))
}
body, err := decompressRequest(buffers, reader, expectedSize, maxSize, compression, sp)
if err != nil {
return 0, err
}
if sp != nil {
sp.LogFields(otlog.Event("util.ParseProtoReader[unmarshal]"), otlog.Int("size", len(body)))
}
// We re-implement proto.Unmarshal here as it calls XXX_Unmarshal first,
// which we can't override without upsetting golint.
req.Reset()
if u, ok := req.(proto.Unmarshaler); ok {
err = u.Unmarshal(body)
} else {
err = proto.NewBuffer(body).Unmarshal(req)
}
if err != nil {
if sp != nil {
sp.LogFields(otlog.Event("util.ParseProtoReader[unmarshal done]"), otlog.Error(err))
}
return 0, err
}
if sp != nil {
sp.LogFields(otlog.Event("util.ParseProtoReader[unmarshal done]"))
}
return len(body), nil
}
type MsgSizeTooLargeErr struct {
Actual, Limit int
}
func (e MsgSizeTooLargeErr) Error() string {
return fmt.Sprintf("the request has been rejected because its size of %d bytes exceeds the limit of %d bytes", e.Actual, e.Limit)
}
// Needed for errors.Is to work properly.
func (e MsgSizeTooLargeErr) Is(err error) bool {
_, ok1 := err.(MsgSizeTooLargeErr)
_, ok2 := err.(*MsgSizeTooLargeErr)
return ok1 || ok2
}
func decompressRequest(buffers *RequestBuffers, reader io.Reader, expectedSize, maxSize int, compression CompressionType, sp opentracing.Span) ([]byte, error) {
if expectedSize > maxSize {
return nil, MsgSizeTooLargeErr{Actual: expectedSize, Limit: maxSize}
}
if compression != NoCompression && compression != RawSnappy && compression != Gzip {
return nil, fmt.Errorf("unrecognized compression type %v", compression)
}
if compression == NoCompression || compression == RawSnappy {
buf, ok := tryBufferFromReader(reader)
if ok {
if compression == NoCompression {
if buf.Len() > maxSize {
return nil, MsgSizeTooLargeErr{Actual: buf.Len(), Limit: maxSize}
}
return buf.Bytes(), nil
}
return decompressSnappyFromBuffer(buffers, buf, maxSize, sp)
}
}
if sp != nil {
sp.LogFields(otlog.Event("util.ParseProtoReader[decompress]"), otlog.Int("expectedSize", expectedSize))
}
if compression == Gzip {
var err error
reader, err = gzip.NewReader(reader)
if err != nil {
return nil, errors.Wrap(err, "create gzip reader")
}
}
// Limit at maxSize+1 so we can tell when the size is exceeded
reader = io.LimitReader(reader, int64(maxSize)+1)
sz := expectedSize
if sz > 0 {
// Extra space guarantees no reallocation
sz += bytes.MinRead
}
buf := buffers.Get(sz)
if _, err := buf.ReadFrom(reader); err != nil {
if compression == Gzip {
return nil, errors.Wrap(err, "decompress gzip")
}
return nil, errors.Wrap(err, "read body")
}
if compression == RawSnappy {
return decompressSnappyFromBuffer(buffers, buf, maxSize, sp)
}
if buf.Len() > maxSize {
return nil, MsgSizeTooLargeErr{Actual: -1, Limit: maxSize}
}
return buf.Bytes(), nil
}
func decompressSnappyFromBuffer(buffers *RequestBuffers, buffer *bytes.Buffer, maxSize int, sp opentracing.Span) ([]byte, error) {
if sp != nil {
sp.LogFields(otlog.Event("util.ParseProtoReader[decompressSnappy]"), otlog.Int("size", buffer.Len()))
}
size, err := snappy.DecodedLen(buffer.Bytes())
if err != nil {
return nil, errors.Wrap(err, "getting snappy decoded length")
}
if size > maxSize {
return nil, MsgSizeTooLargeErr{Actual: size, Limit: maxSize}
}
decBuf := buffers.Get(size)
// Snappy bases itself on the target buffer's length, not capacity
decBufBytes := decBuf.Bytes()[0:size]
decoded, err := snappy.Decode(decBufBytes, buffer.Bytes())
if err != nil {
return nil, errors.Wrap(err, "decompress snappy")
}
return decoded, nil
}
// tryBufferFromReader attempts to cast the reader to a `*bytes.Buffer` this is possible when using httpgrpc.
// If it fails it will return nil and false.
func tryBufferFromReader(reader io.Reader) (*bytes.Buffer, bool) {
if bufReader, ok := reader.(interface {
BytesBuffer() *bytes.Buffer
}); ok && bufReader != nil {
return bufReader.BytesBuffer(), true
}
return nil, false
}
var snappyEncoding = snappyCheckAndEncode
// SerializeProtoResponse serializes a protobuf response into an HTTP response.
func SerializeProtoResponse(w http.ResponseWriter, resp proto.Message, compression CompressionType) error {
data, err := proto.Marshal(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return fmt.Errorf("error marshaling proto response: %v", err)
}
switch compression {
case NoCompression:
case RawSnappy:
data, err = snappyEncoding(nil, data)
if err != nil {
err = errors.Wrap(err, "snappy encoding")
break
}
case Gzip:
var buf bytes.Buffer
buf.Grow(len(data))
wr := gzip.NewWriter(&buf)
if _, err = wr.Write(data); err != nil {
err = errors.Wrap(err, "write gzip")
break
}
if err = wr.Close(); err != nil {
err = errors.Wrap(err, "close gzip writer")
break
}
data = buf.Bytes()
default:
err = fmt.Errorf("unrecognized compression format %v", compression)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return err
}
if _, err := w.Write(data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return fmt.Errorf("error sending proto response: %v", err)
}
return nil
}
// ParseRequestFormWithoutConsumingBody parsed and returns the request parameters (query string and/or request body)
// from the input http.Request. If the request has a Body, the request's Body is replaces so that it can be consumed again.
func ParseRequestFormWithoutConsumingBody(r *http.Request) (url.Values, error) {
if r.Body == nil {
if err := r.ParseForm(); err != nil {
return nil, err
}
return r.Form, nil
}
// Close the original body reader. It's going to be replaced later in this function.
origBody := r.Body
defer func() { _ = origBody.Close() }()
// Store the body contents, so we can read it multiple times.
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
r.Body = io.NopCloser(bytes.NewReader(bodyBytes))
// Parse the request data.
if err := r.ParseForm(); err != nil {
return nil, err
}
// Store a copy of the params and restore the request state.
// Restore the body, so it can be read again if it's used to forward the request through a roundtripper.
// Restore the Form and PostForm, to avoid subtle bugs in middlewares, as they were set by ParseForm.
params := copyValues(r.Form)
r.Body = io.NopCloser(bytes.NewReader(bodyBytes))
r.Form, r.PostForm = nil, nil
return params, nil
}
func copyValues(src url.Values) url.Values {
dst := make(url.Values, len(src))
for k, vs := range src {
dst[k] = append([]string(nil), vs...)
}
return dst
}
// IsHTTPStatusCode returns true if the given code is a valid HTTP status code, or false otherwise.
func IsHTTPStatusCode(code codes.Code) bool {
return int(code) >= 100 && int(code) < 600
}
func IsValidURL(endpoint string) bool {
u, err := url.Parse(endpoint)
if err != nil {
return false
}
return u.Scheme != "" && u.Host != ""
}
func snappyCheckAndEncode(dst []byte, data []byte) ([]byte, error) {
if encodeLen := snappy.MaxEncodedLen(len(data)); encodeLen == -1 {
return nil, fmt.Errorf("data too large to encode")
}
return snappy.Encode(dst, data), nil
}