diff --git a/internal/fetch/clipboard.go b/internal/fetch/clipboard.go index eb3e72f..565cf8c 100644 --- a/internal/fetch/clipboard.go +++ b/internal/fetch/clipboard.go @@ -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. @@ -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} diff --git a/internal/fetch/fetch.go b/internal/fetch/fetch.go index 7c84fee..a6b1b30 100644 --- a/internal/fetch/fetch.go +++ b/internal/fetch/fetch.go @@ -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 ( @@ -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 }