1package readers 2 3import "io" 4 5// noClose is used to wrap an io.Reader to stop it being upgraded 6type noClose struct { 7 in io.Reader 8} 9 10// Read implements io.Closer by passing it straight on 11func (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. 20func 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} 30