response: Don't use bufReadCloser
This commit is contained in:
parent
a40b5dcd0b
commit
3dca29eb41
28
io.go
28
io.go
@ -1,7 +1,6 @@
|
||||
package gemini
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"io"
|
||||
)
|
||||
@ -75,30 +74,3 @@ func (nopReadCloser) Read(p []byte) (int, error) {
|
||||
func (nopReadCloser) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type bufReadCloser struct {
|
||||
br *bufio.Reader // used until empty
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
func newBufReadCloser(br *bufio.Reader, rc io.ReadCloser) io.ReadCloser {
|
||||
body := &bufReadCloser{ReadCloser: rc}
|
||||
if br.Buffered() != 0 {
|
||||
body.br = br
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func (b *bufReadCloser) Read(p []byte) (n int, err error) {
|
||||
if b.br != nil {
|
||||
if n := b.br.Buffered(); len(p) > n {
|
||||
p = p[:n]
|
||||
}
|
||||
n, err = b.br.Read(p)
|
||||
if b.br.Buffered() == 0 {
|
||||
b.br = nil
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
return b.ReadCloser.Read(p)
|
||||
}
|
||||
|
@ -81,7 +81,11 @@ func ReadResponse(r io.ReadCloser) (*Response, error) {
|
||||
resp.Meta = string(meta)
|
||||
|
||||
if resp.Status.Class() == StatusSuccess {
|
||||
resp.Body = newBufReadCloser(br, r)
|
||||
type readCloser struct {
|
||||
io.Reader
|
||||
io.Closer
|
||||
}
|
||||
resp.Body = readCloser{br, r}
|
||||
} else {
|
||||
resp.Body = nopReadCloser{}
|
||||
r.Close()
|
||||
|
Loading…
Reference in New Issue
Block a user