diff options
Diffstat (limited to 'client.go')
-rw-r--r-- | client.go | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -1,12 +1,12 @@ package sliderule import ( + "context" "crypto/tls" "errors" "fmt" "io" "net/http" - "net/url" neturl "net/url" "tildegit.org/tjp/sliderule/finger" @@ -18,7 +18,7 @@ import ( ) type protocolClient interface { - RoundTrip(*Request) (*Response, error) + RoundTrip(context.Context, *Request) (*Response, error) IsRedirect(*Response) bool } @@ -61,23 +61,23 @@ func NewClient(tlsConf *tls.Config) Client { // RoundTrip sends a single request and returns the repsonse. // // If the response is a redirect it will be returned, rather than fetched. -func (c Client) RoundTrip(request *Request) (*Response, error) { +func (c Client) RoundTrip(ctx context.Context, request *Request) (*Response, error) { pc, ok := c.protos[request.Scheme] if !ok { return nil, fmt.Errorf("unrecognized protocol: %s", request.Scheme) } - return pc.RoundTrip(request) + return pc.RoundTrip(ctx, request) } // Fetch collects a resource from a URL including following any redirects. -func (c Client) Fetch(url string) (*Response, error) { +func (c Client) Fetch(ctx context.Context, url string) (*Response, error) { u, err := neturl.Parse(url) if err != nil { return nil, err } for i := 0; i <= c.MaxRedirects; i += 1 { - response, err := c.RoundTrip(&types.Request{URL: u}) + response, err := c.RoundTrip(ctx, &types.Request{URL: u}) if err != nil { return nil, err } @@ -100,23 +100,23 @@ func (c Client) Fetch(url string) (*Response, error) { } // Upload sends a request with a body and returns any redirect response. -func (c Client) Upload(url string, contents io.Reader) (*Response, error) { +func (c Client) Upload(ctx context.Context, url string, contents io.Reader) (*Response, error) { u, err := neturl.Parse(url) if err != nil { return nil, err } switch u.Scheme { case "titan", "spartan", "http", "https": - return c.RoundTrip(&types.Request{URL: u, Meta: contents}) + return c.RoundTrip(ctx, &types.Request{URL: u, Meta: contents}) default: return nil, fmt.Errorf("upload not supported on %s", u.Scheme) } } -func getRedirectLocation(prev *url.URL, proto string, meta any) string { +func getRedirectLocation(prev *neturl.URL, proto string, meta any) string { switch proto { case "gemini", "spartan": - u, _ := url.Parse(meta.(string)) + u, _ := neturl.Parse(meta.(string)) return prev.ResolveReference(u).String() case "http", "https": return meta.(*http.Response).Header.Get("Location") @@ -128,9 +128,9 @@ type httpClient struct { tp *http.Transport } -func (hc httpClient) RoundTrip(request *Request) (*Response, error) { +func (hc httpClient) RoundTrip(ctx context.Context, request *Request) (*Response, error) { body, _ := request.Meta.(io.Reader) - hreq, err := http.NewRequest("GET", request.URL.String(), body) + hreq, err := http.NewRequestWithContext(ctx, "GET", request.URL.String(), body) if err != nil { return nil, err } |