summaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-10-30 10:24:01 -0600
committertjpcc <tjp@ctrl-c.club>2023-10-30 10:24:01 -0600
commit96577f2367b7b02941f991f57125281a9a447c51 (patch)
treeccc74451ad378ce4be816cb4b55f7fef3bca3fbd /client.go
parent0a7e966d5a093e8c2d3b3834d25feb93f5fca156 (diff)
support uploads in sliderule.Client and sw-fetch tool
Diffstat (limited to 'client.go')
-rw-r--r--client.go19
1 files changed, 17 insertions, 2 deletions
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
}