Skip to content

Prevent body from being closed by the Request RoundTripper #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ 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 on passing
// it to the RoundTripper and closes the stream after we
// are done with the body content.
if cl, ok := body.(io.Closer); ok {
body = closeInhibitor{body}
defer cl.Close()
}
// 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
Expand Down
11 changes: 11 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}