From 07e319a14fe2bb66e5186d301d5579d02dbabd65 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 11 Nov 2015 19:34:42 +0200 Subject: [PATCH] Move request validation and preparation down the stack to HostClient --- client.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index 3dc3d41..972fc88 100644 --- a/client.go +++ b/client.go @@ -63,16 +63,6 @@ type Client struct { func (c *Client) Do(req *Request, resp *Response) error { req.ParseURI() host := req.URI.Host - if len(req.Header.PeekBytes(strHost)) == 0 { - req.Header.SetCanonical(strHost, host) - } - if len(host) == 0 { - return fmt.Errorf("Host must be non-empty. Set it via RequestHeader.Set() or via RequestHeader.RequestURI") - } - if !bytes.Equal(req.URI.Scheme, strHTTP) { - return fmt.Errorf("unsupported protocol %q. Currently only http is supported", req.URI.Scheme) - } - req.Header.RequestURI = req.URI.AppendRequestURI(req.Header.RequestURI[:0]) startCleaner := false @@ -200,6 +190,19 @@ type clientConn struct { func (c *HostClient) Do(req *Request, resp *Response) error { c.LastUseTime = time.Now() + req.ParseURI() + host := req.URI.Host + if len(req.Header.PeekBytes(strHost)) == 0 { + req.Header.SetCanonical(strHost, host) + } + if len(host) == 0 { + return fmt.Errorf("Host must be non-empty. Set it via RequestHeader.Set() or via RequestHeader.RequestURI") + } + if !bytes.Equal(req.URI.Scheme, strHTTP) { + return fmt.Errorf("unsupported protocol %q. Currently only http is supported", req.URI.Scheme) + } + req.Header.RequestURI = req.URI.AppendRequestURI(req.Header.RequestURI[:0]) + cc, err := c.acquireConn() if err != nil { return err @@ -226,7 +229,12 @@ func (c *HostClient) Do(req *Request, resp *Response) error { return err } c.releaseReader(br) - c.releaseConn(cc) + + if resp.Header.ConnectionClose { + c.closeConn(cc) + } else { + c.releaseConn(cc) + } return err }