response: Don't use bufReadCloser

This commit is contained in:
Adnan Maolood 2021-03-20 13:41:53 -04:00
parent a40b5dcd0b
commit 3dca29eb41
2 changed files with 5 additions and 29 deletions

28
io.go
View File

@ -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)
}

View File

@ -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()