Make Response implement io.WriterTo

This commit is contained in:
Adnan Maolood 2021-02-28 22:21:54 -05:00
parent f7012b38da
commit 69f0913b3d

View File

@ -112,22 +112,27 @@ func (r *Response) TLS() *tls.ConnectionState {
return nil return nil
} }
// Write writes r to w in the Gemini response format, including the // WriteTo writes r to w in the Gemini response format, including the
// header and body. // header and body.
// //
// This method consults the Status, Meta, and Body fields of the response. // This method consults the Status, Meta, and Body fields of the response.
// The Response Body is closed after it is sent. // The Response Body is closed after it is sent.
func (r *Response) Write(w io.Writer) error { func (r *Response) WriteTo(w io.Writer) (int64, error) {
if _, err := fmt.Fprintf(w, "%02d %s\r\n", r.Status, r.Meta); err != nil { var wrote int64
return err n, err := fmt.Fprintf(w, "%02d %s\r\n", r.Status, r.Meta)
wrote += int64(n)
if err != nil {
return wrote, err
} }
if r.Body != nil { if r.Body != nil {
defer r.Body.Close() defer r.Body.Close()
if _, err := io.Copy(w, r.Body); err != nil { n, err := io.Copy(w, r.Body)
return err wrote += n
if err != nil {
return wrote, err
} }
} }
return nil return wrote, nil
} }
// A ResponseWriter interface is used by a Gemini handler to construct // A ResponseWriter interface is used by a Gemini handler to construct