1
0
Fork 0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-05-18 20:36:04 +02:00

request.Write: Accept an io.Writer

This commit is contained in:
Adnan Maolood 2021-02-18 21:58:35 -05:00
parent c70ef5c470
commit 229ebb4106
2 changed files with 6 additions and 8 deletions

View File

@ -1,7 +1,6 @@
package gemini
import (
"bufio"
"context"
"crypto/tls"
"crypto/x509"
@ -157,9 +156,7 @@ func (c *Client) Do(req *Request) (*Response, error) {
func (c *Client) do(conn *tls.Conn, req *Request) (*Response, error) {
// Write the request
w := bufio.NewWriter(conn)
err := req.Write(w)
err := req.Write(conn)
if err != nil {
return nil, fmt.Errorf("failed to write request: %w", err)
}

View File

@ -107,16 +107,17 @@ func ReadRequest(r io.Reader) (*Request, error) {
// Write writes a Gemini request in wire format.
// This method consults the request URL only.
func (r *Request) Write(w *bufio.Writer) error {
func (r *Request) Write(w io.Writer) error {
bw := bufio.NewWriterSize(w, 1026)
url := r.URL.String()
if len(url) > 1024 {
return ErrInvalidRequest
}
if _, err := w.WriteString(url); err != nil {
if _, err := bw.WriteString(url); err != nil {
return err
}
if _, err := w.Write(crlf); err != nil {
if _, err := bw.Write(crlf); err != nil {
return err
}
return w.Flush()
return bw.Flush()
}