Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions internal/fetch/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/ryanfowler/fetch/internal/core"
)

const clipboardLimit = 1 << 24 // 16MB

// clipboardCopier handles capturing the raw response body and copying it
// to the system clipboard. Use newClipboardCopier to set up body wrapping,
// then call finish after the response has been consumed.
Expand Down Expand Up @@ -55,7 +53,7 @@ func newClipboardCopier(r *Request, resp *http.Response) *clipboardCopier {

buf := &bytes.Buffer{}
resp.Body = readCloserTee{
Reader: io.TeeReader(io.LimitReader(resp.Body, clipboardLimit), buf),
Reader: io.TeeReader(io.LimitReader(resp.Body, maxBodyBytes), buf),
Closer: resp.Body,
}
return &clipboardCopier{cmd: cmd, buf: buf}
Expand Down
9 changes: 6 additions & 3 deletions internal/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// maxBodyBytes is the maximum number of bytes read into memory for
// formatting a response body or copying it to the clipboard.
const maxBodyBytes = 1 << 20 // 1MiB

type ContentType int

const (
Expand Down Expand Up @@ -376,12 +380,11 @@ func formatResponse(ctx context.Context, r *Request, resp *http.Response) (io.Re
return resp.Body, nil
}

const bytesLimit = 1 << 24 // 16MB
buf, err := io.ReadAll(io.LimitReader(resp.Body, bytesLimit))
buf, err := io.ReadAll(io.LimitReader(resp.Body, maxBodyBytes))
if err != nil {
return nil, err
}
if len(buf) >= bytesLimit {
if len(buf) >= maxBodyBytes {
// We've reached the limit of bytes read into memory, skip formatting.
return io.MultiReader(bytes.NewReader(buf), resp.Body), nil
}
Expand Down