summaryrefslogtreecommitdiff
path: root/gemini/response.go
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-01-28 14:52:35 -0700
committertjpcc <tjp@ctrl-c.club>2023-01-28 15:01:41 -0700
commit66a1b1f39a1e1d5499b548b36d18c8daa872d7da (patch)
tree96471dbd5486ede1a908790ac23e0c55b226dfad /gemini/response.go
parenta27b879accb191b6a6c6e76a6251ed751967f73a (diff)
gopher support.
Some of the contrib packages were originally built gemini-specific and had to be refactored into generic core functionality and thin protocol-specific wrappers for each of gemini and gopher.
Diffstat (limited to 'gemini/response.go')
-rw-r--r--gemini/response.go33
1 files changed, 15 insertions, 18 deletions
diff --git a/gemini/response.go b/gemini/response.go
index 0452462..b8797da 100644
--- a/gemini/response.go
+++ b/gemini/response.go
@@ -6,6 +6,7 @@ import (
"errors"
"io"
"strconv"
+ "sync"
"tildegit.org/tjp/gus"
)
@@ -284,19 +285,17 @@ func ParseResponse(rdr io.Reader) (*gus.Response, error) {
}, nil
}
-type ResponseReader interface {
- io.Reader
- io.WriterTo
- io.Closer
-}
-
-func NewResponseReader(response *gus.Response) ResponseReader {
- return &responseReader{ Response: response }
+func NewResponseReader(response *gus.Response) gus.ResponseReader {
+ return &responseReader{
+ Response: response,
+ once: &sync.Once{},
+ }
}
type responseReader struct {
*gus.Response
reader io.Reader
+ once *sync.Once
}
func (rdr *responseReader) Read(b []byte) (int, error) {
@@ -310,16 +309,14 @@ func (rdr *responseReader) WriteTo(dst io.Writer) (int64, error) {
}
func (rdr *responseReader) ensureReader() {
- if rdr.reader != nil {
- return
- }
-
- hdr := bytes.NewBuffer(rdr.headerLine())
- if rdr.Body != nil {
- rdr.reader = io.MultiReader(hdr, rdr.Body)
- } else {
- rdr.reader = hdr
- }
+ rdr.once.Do(func() {
+ hdr := bytes.NewBuffer(rdr.headerLine())
+ if rdr.Body != nil {
+ rdr.reader = io.MultiReader(hdr, rdr.Body)
+ } else {
+ rdr.reader = hdr
+ }
+ })
}
func (rdr responseReader) headerLine() []byte {