diff options
Diffstat (limited to 'gemini/response.go')
-rw-r--r-- | gemini/response.go | 33 |
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 { |