diff --git a/client.go b/client.go index 23d8f92..57a0b4e 100644 --- a/client.go +++ b/client.go @@ -96,10 +96,11 @@ func (c *Client) do(req *Request, via []*Request) (*Response, error) { return c.verifyConnection(req, cs) }, } - conn, err := tls.Dial("tcp", req.Host, config) + netConn, err := (&net.Dialer{}).DialContext(req.Context, "tcp", req.Host) if err != nil { return nil, err } + conn := tls.Client(netConn, config) // Set connection deadline if d := c.Timeout; d != 0 { conn.SetDeadline(time.Now().Add(d)) diff --git a/request.go b/request.go index 0154ab3..b302445 100644 --- a/request.go +++ b/request.go @@ -2,6 +2,7 @@ package gemini import ( "bufio" + "context" "crypto/tls" "net" "net/url" @@ -33,6 +34,10 @@ type Request struct { // connection on which the request was received. // This field is ignored by the client. TLS tls.ConnectionState + + // Context specifies the context to use for client requests. + // Context must not be nil. + Context context.Context } // NewRequest returns a new request. The host is inferred from the URL. @@ -52,8 +57,9 @@ func NewRequestFromURL(url *url.URL) *Request { host += ":1965" } return &Request{ - URL: url, - Host: host, + URL: url, + Host: host, + Context: context.Background(), } }