summaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authortjp <tjp@ctrl-c.club>2024-01-13 11:29:17 -0700
committertjp <tjp@ctrl-c.club>2024-01-13 11:29:17 -0700
commit4d861a2c395c0926b066014b92999d4dda454b2b (patch)
tree7bbf7d94cec2a5412a68b830c1e5223918d9bc82 /client.go
parentde1490808fa6e4d6749ff29d20cc1a589ec476d1 (diff)
dial timeouts for clients, and catch up on test fixes
Diffstat (limited to 'client.go')
-rw-r--r--client.go24
1 files changed, 12 insertions, 12 deletions
diff --git a/client.go b/client.go
index 217a777..8119200 100644
--- a/client.go
+++ b/client.go
@@ -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
}