Skip to content

Commit 364288b

Browse files
committed
refactor: Avoid reading unwanted bytes
Limit the reader to the max bytes, plus one to indicate that there's more to truncate.
1 parent 664558a commit 364288b

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

transport/http/channel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ func (c *Channel) Request(ctx context.Context, req transport.HTTPRequest) (trans
7878
return nil, fmt.Errorf("doing HTTP request: %w", err)
7979
}
8080
if !slices.Contains(c.statuses, res.StatusCode) {
81-
b, readErr := io.ReadAll(res.Body)
81+
const maxBodyLen = 4096
82+
b, readErr := io.ReadAll(io.LimitReader(res.Body, maxBodyLen+1))
8283
res.Body.Close()
8384

8485
msg := fmt.Sprintf("HTTP Request failed. %s %s → %d", hr.Method, c.url.String(), res.StatusCode)
8586
if readErr != nil {
8687
msg += fmt.Sprintf(" (failed to read response body: %s)", readErr)
8788
} else if len(b) > 0 {
8889
body := string(b)
89-
const maxBodyLen = 4096
9090
if len(body) > maxBodyLen {
9191
body = body[:maxBodyLen] + "... (truncated)"
9292
}

0 commit comments

Comments
 (0)