github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/readers/noclose.go (about)

     1  package readers
     2  
     3  import "io"
     4  
     5  // noClose is used to wrap an io.Reader to stop it being upgraded
     6  type noClose struct {
     7  	in io.Reader
     8  }
     9  
    10  // Read implements io.Closer by passing it straight on
    11  func (nc noClose) Read(p []byte) (n int, err error) {
    12  	return nc.in.Read(p)
    13  }
    14  
    15  // NoCloser makes sure that the io.Reader passed in can't upgraded to
    16  // an io.Closer.
    17  //
    18  // This is for use with http.NewRequest to make sure the body doesn't
    19  // get upgraded to an io.Closer and the body closed unexpectedly.
    20  func NoCloser(in io.Reader) io.Reader {
    21  	if in == nil {
    22  		return in
    23  	}
    24  	// if in doesn't implement io.Closer, just return it
    25  	if _, canClose := in.(io.Closer); !canClose {
    26  		return in
    27  	}
    28  	return noClose{in: in}
    29  }