mirror of
https://git.sr.ht/~adnano/go-gemini
synced 2024-09-18 13:31:36 +02:00
response: Don't use bufReadCloser
This commit is contained in:
parent
21845ca419
commit
4ee8740ee3
28
io.go
28
io.go
@ -1,7 +1,6 @@
|
|||||||
package gemini
|
package gemini
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
@ -75,30 +74,3 @@ func (nopReadCloser) Read(p []byte) (int, error) {
|
|||||||
func (nopReadCloser) Close() error {
|
func (nopReadCloser) Close() error {
|
||||||
return nil
|
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)
|
resp.Meta = string(meta)
|
||||||
|
|
||||||
if resp.Status.Class() == StatusSuccess {
|
if resp.Status.Class() == StatusSuccess {
|
||||||
resp.Body = newBufReadCloser(br, r)
|
type readCloser struct {
|
||||||
|
io.Reader
|
||||||
|
io.Closer
|
||||||
|
}
|
||||||
|
resp.Body = readCloser{br, r}
|
||||||
} else {
|
} else {
|
||||||
resp.Body = nopReadCloser{}
|
resp.Body = nopReadCloser{}
|
||||||
r.Close()
|
r.Close()
|
||||||
|
Loading…
Reference in New Issue
Block a user