From 4e48f0311ec4d5da19510a6414a28b4cd6f76978 Mon Sep 17 00:00:00 2001 From: Ben Tam Date: Fri, 20 Dec 2024 00:28:33 +0800 Subject: [PATCH 1/2] feat: shallow copy a buffer when it is bytes.Buffer Signed-off-by: Ben Tam --- auth.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/auth.go b/auth.go index 71abd66b..48299a64 100644 --- a/auth.go +++ b/auth.go @@ -159,9 +159,14 @@ func (a *authorizer) NewAuthenticator(body io.Reader) (Authenticator, io.Reader) // cursor to the start. // Otherwise, copy the stream into a buffer while uploading // and use the buffers content on retry. - if _, ok := retryBuf.(io.Seeker); ok { + switch body.(type) { + case io.Seeker: body = io.NopCloser(body) - } else { + case *bytes.Buffer: + buffer := &bytes.Buffer{} + *buffer = *body.(*bytes.Buffer) + retryBuf = buffer + default: buff := &bytes.Buffer{} retryBuf = buff body = io.TeeReader(body, buff) From b3ae69af070e985300cd63fccfc253b7df360327 Mon Sep 17 00:00:00 2001 From: Ben Tam Date: Fri, 20 Dec 2024 00:36:23 +0800 Subject: [PATCH 2/2] chore: add comment for the operation Signed-off-by: Ben Tam --- auth.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/auth.go b/auth.go index 48299a64..241addc4 100644 --- a/auth.go +++ b/auth.go @@ -157,6 +157,8 @@ func (a *authorizer) NewAuthenticator(body io.Reader) (Authenticator, io.Reader) // from the passed body stream. // When body is seekable, use seek to reset the streams // cursor to the start. + // If the body is a bytes.Buffer, create a new buffer and perform a shallow copy + // of the original buffer's content to the new one. Use the new buffer for retries. // Otherwise, copy the stream into a buffer while uploading // and use the buffers content on retry. switch body.(type) {