From a5d8aeb0bb53420a6dc87dcdc72494fa3f44224f Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 1 May 2023 08:38:54 -0600 Subject: make spartan.Client.RoundTrip match the API of other clients. - Request.Meta is already used as an *io.LimitedReader in spartan servers, so by following this convention the RoundTrip method doesn't need anything more than the *Request. - Make a new public method for setting the body on a spartan request. --- spartan/client.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'spartan/client.go') diff --git a/spartan/client.go b/spartan/client.go index e571c14..40c9dd6 100644 --- a/spartan/client.go +++ b/spartan/client.go @@ -18,7 +18,7 @@ import ( type Client struct{} // RoundTrip sends a single spartan request and returns its response. -func (c Client) RoundTrip(request *sr.Request, body io.Reader) (*sr.Response, error) { +func (c Client) RoundTrip(request *sr.Request) (*sr.Response, error) { if request.Scheme != "spartan" && request.Scheme != "" { return nil, errors.New("non-spartan protocols not supported") } @@ -38,20 +38,17 @@ func (c Client) RoundTrip(request *sr.Request, body io.Reader) (*sr.Response, er request.RemoteAddr = conn.RemoteAddr() - var bodyBytes []byte = nil - if body != nil { - bodyBytes, err = io.ReadAll(body) - if err != nil { - return nil, err - } + rdr, ok := request.Meta.(*io.LimitedReader) + if !ok { + return nil, errors.New("request body must be an *io.LimitedReader") } - requestLine := host + " " + request.EscapedPath() + " " + strconv.Itoa(len(bodyBytes)) + "\r\n" + requestLine := host + " " + request.EscapedPath() + " " + strconv.Itoa(int(rdr.N)) + "\r\n" if _, err := conn.Write([]byte(requestLine)); err != nil { return nil, err } - if _, err := conn.Write(bodyBytes); err != nil { + if _, err := io.Copy(conn, rdr); err != nil { return nil, err } -- cgit v1.2.3