From 66a1b1f39a1e1d5499b548b36d18c8daa872d7da Mon Sep 17 00:00:00 2001 From: tjpcc Date: Sat, 28 Jan 2023 14:52:35 -0700 Subject: 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. --- gemini/response.go | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'gemini/response.go') 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 { -- cgit v1.2.3