Skip to content

Commit

Permalink
fix: Read response body more efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgeiser authored and rtpt-alexanderneumann committed Sep 16, 2020
1 parent 2abbd54 commit e051791
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions response/response.go
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -115,19 +116,13 @@ func extractCommand(buf []byte, cmds [][]string) (data []string, err error) {

// ReadBody reads at most maxBodySize bytes from the body and saves it to a buffer in the
// Respons struct for later processing.
func (r *Response) ReadBody(body io.Reader, maxBodySize int) error {
r.RawBody = make([]byte, maxBodySize)

n, err := io.ReadFull(body, r.RawBody)
if n == 0 && err == io.EOF {
err = nil
}

r.RawBody = r.RawBody[:n]
if err == io.ErrUnexpectedEOF {
err = nil
}

func (r *Response) ReadBody(body io.Reader, maxBodySize int) (err error) {
// Read a limited amount of data from the response such that extraordinarily large
// responses don't slow down the scan. If the actual body is larger, it will be
// closes preemptively, closing the TCP connection. The reason is that opening a
// new connection likely has a much lower performance impact than tranferring large
// amounts of unwanted data over the network.
r.RawBody, err = ioutil.ReadAll(&io.LimitedReader{R: body, N: int64(maxBodySize)})
if err != nil {
return err
}
Expand Down

0 comments on commit e051791

Please sign in to comment.