1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-16 00:04:40 +00:00

lib/readers: add NoCloser to stop upgrades from io.Reader to io.ReadCloser

This commit is contained in:
Nick Craig-Wood
2019-08-26 12:17:53 +01:00
parent 73e010aff9
commit ce3340621f
2 changed files with 73 additions and 0 deletions

29
lib/readers/noclose.go Normal file
View File

@@ -0,0 +1,29 @@
package readers
import "io"
// noClose is used to wrap an io.Reader to stop it being upgraded
type noClose struct {
in io.Reader
}
// Read implements io.Closer by passing it straight on
func (nc noClose) Read(p []byte) (n int, err error) {
return nc.in.Read(p)
}
// NoCloser makes sure that the io.Reader passed in can't upgraded to
// an io.Closer.
//
// This is for use with http.NewRequest to make sure the body doesn't
// get upgraded to an io.Closer and the body closed unexpectedly.
func NoCloser(in io.Reader) io.Reader {
if in == nil {
return in
}
// if in doesn't implement io.Closer, just return it
if _, canClose := in.(io.Closer); !canClose {
return in
}
return noClose{in: in}
}