github.com/sagernet/quic-go@v0.43.1-beta.1/http3/gzip_reader.go (about) 1 package http3 2 3 // copied from net/transport.go 4 5 // gzipReader wraps a response body so it can lazily 6 // call gzip.NewReader on the first call to Read 7 import ( 8 "compress/gzip" 9 "io" 10 ) 11 12 // call gzip.NewReader on the first call to Read 13 type gzipReader struct { 14 body io.ReadCloser // underlying Response.Body 15 zr *gzip.Reader // lazily-initialized gzip reader 16 zerr error // sticky error 17 } 18 19 func newGzipReader(body io.ReadCloser) io.ReadCloser { 20 return &gzipReader{body: body} 21 } 22 23 func (gz *gzipReader) Read(p []byte) (n int, err error) { 24 if gz.zerr != nil { 25 return 0, gz.zerr 26 } 27 if gz.zr == nil { 28 gz.zr, err = gzip.NewReader(gz.body) 29 if err != nil { 30 gz.zerr = err 31 return 0, err 32 } 33 } 34 return gz.zr.Read(p) 35 } 36 37 func (gz *gzipReader) Close() error { 38 return gz.body.Close() 39 }