From 96577f2367b7b02941f991f57125281a9a447c51 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 30 Oct 2023 10:24:01 -0600 Subject: support uploads in sliderule.Client and sw-fetch tool --- client.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'client.go') diff --git a/client.go b/client.go index 0a25110..145b2f8 100644 --- a/client.go +++ b/client.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "errors" "fmt" + "io" "net/http" neturl "net/url" @@ -93,6 +94,20 @@ func (c Client) Fetch(url string) (*Response, error) { return nil, ExceededMaxRedirects } +// Upload sends a request with a body and returns any redirect response. +func (c Client) Upload(url string, contents *io.LimitedReader) (*Response, error) { + u, err := neturl.Parse(url) + if err != nil { + return nil, err + } + switch u.Scheme { + case "spartan", "http", "https": + return c.RoundTrip(&types.Request{URL: u, Meta: contents}) + default: + return nil, fmt.Errorf("upload not supported on %s", u.Scheme) + } +} + func getRedirectLocation(proto string, meta any) string { switch proto { case "gemini", "spartan": @@ -103,12 +118,12 @@ func getRedirectLocation(proto string, meta any) string { return "" } -type httpClient struct{ +type httpClient struct { tp *http.Transport } func (hc httpClient) RoundTrip(request *Request) (*Response, error) { - hreq, err := http.NewRequest("GET", request.URL.String(), nil) + hreq, err := http.NewRequest("GET", request.URL.String(), request.Meta.(*io.LimitedReader)) if err != nil { return nil, err } -- cgit v1.2.3