(res *http.Response, logger log.Logger)
| 446 | } |
| 447 | |
| 448 | func BodyBytes(res *http.Response, logger log.Logger) ([]byte, error) { |
| 449 | var buf *bytes.Buffer |
| 450 | |
| 451 | // Attempt to cast the response body to a Buffer and use it if possible. |
| 452 | // This is because the frontend may have already read the body and buffered it. |
| 453 | if buffer, ok := res.Body.(Buffer); ok { |
| 454 | buf = bytes.NewBuffer(buffer.Bytes()) |
| 455 | } else { |
| 456 | // Preallocate the buffer with the exact size so we don't waste allocations |
| 457 | // while progressively growing an initial small buffer. The buffer capacity |
| 458 | // is increased by MinRead to avoid extra allocations due to how ReadFrom() |
| 459 | // internally works. |
| 460 | buf = bytes.NewBuffer(make([]byte, 0, res.ContentLength+bytes.MinRead)) |
| 461 | if _, err := buf.ReadFrom(res.Body); err != nil { |
| 462 | return nil, httpgrpc.Errorf(http.StatusInternalServerError, "error decoding response: %v", err) |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // Handle decoding response if it was compressed |
| 467 | encoding := res.Header.Get("Content-Encoding") |
| 468 | return decode(buf, encoding, logger) |
| 469 | } |
| 470 | |
| 471 | func BodyBytesFromHTTPGRPCResponse(res *httpgrpc.HTTPResponse, logger log.Logger) ([]byte, error) { |
| 472 | headers := http.Header{} |
no test coverage detected