From 2889239999c7ef561d20228311ea947fea154996 Mon Sep 17 00:00:00 2001 From: Ringo Hoffmann Date: Sat, 22 Jan 2022 12:29:02 +0100 Subject: [PATCH] inhibit stream close on request --- requests.go | 4 ++++ utils.go | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/requests.go b/requests.go index a8ee664..28886a4 100644 --- a/requests.go +++ b/requests.go @@ -14,6 +14,10 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R var retryBuf io.Reader if body != nil { + // Because Request#Do closes closable streams, Seeker#Seek + // will fail on retry because stream is already closed. + // This inhibits the closing of the passed stream. + body = closeInhibitor{body} // If the authorization fails, we will need to restart reading // from the passed body stream. // When body is seekable, use seek to reset the streams diff --git a/utils.go b/utils.go index f82592a..6069f6e 100644 --- a/utils.go +++ b/utils.go @@ -133,3 +133,14 @@ func (l *limitedReadCloser) Read(buf []byte) (int, error) { func (l *limitedReadCloser) Close() error { return l.rc.Close() } + +// closeInhibitor implements io.Closer and +// wraps a Reader. When Close() is performed +// on it, it will simply be silently rejected. +type closeInhibitor struct { + io.Reader +} + +func (ci closeInhibitor) Close() error { + return nil +}