summaryrefslogtreecommitdiff
path: root/client.go
blob: 217a777a6ab368c81c00115b2a3b29a91f174ca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package sliderule

import (
	"crypto/tls"
	"errors"
	"fmt"
	"io"
	"net/http"
	"net/url"
	neturl "net/url"

	"tildegit.org/tjp/sliderule/finger"
	"tildegit.org/tjp/sliderule/gemini"
	"tildegit.org/tjp/sliderule/gopher"
	"tildegit.org/tjp/sliderule/internal/types"
	"tildegit.org/tjp/sliderule/nex"
	"tildegit.org/tjp/sliderule/spartan"
)

type protocolClient interface {
	RoundTrip(*Request) (*Response, error)
	IsRedirect(*Response) bool
}

// Client is a multi-protocol client which handles all protocols known to sliderule.
type Client struct {
	MaxRedirects int

	protos map[string]protocolClient
}

const DefaultMaxRedirects int = 5

var ExceededMaxRedirects = errors.New("Client: exceeded MaxRedirects")

// NewClient builds a Client object.
//
// tlsConf may be nil, in which case gemini requests connections will not be made
// with any client certificate.
func NewClient(tlsConf *tls.Config) Client {
	hc := httpClient{tp: http.DefaultTransport.(*http.Transport).Clone()}
	if tlsConf != nil {
		hc.tp.TLSClientConfig = tlsConf
	}
	gemcl := gemini.NewClient(tlsConf)
	return Client{
		protos: map[string]protocolClient{
			"finger":  finger.Client{},
			"gopher":  gopher.Client{},
			"gemini":  gemcl,
			"titan":   gemcl,
			"spartan": spartan.NewClient(),
			"http":    hc,
			"https":   hc,
			"nex":     nex.Client{},
		},
		MaxRedirects: DefaultMaxRedirects,
	}
}

// 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) {
	pc, ok := c.protos[request.Scheme]
	if !ok {
		return nil, fmt.Errorf("unrecognized protocol: %s", request.Scheme)
	}
	return pc.RoundTrip(request)
}

// Fetch collects a resource from a URL including following any redirects.
func (c Client) Fetch(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})
		if err != nil {
			return nil, err
		}

		if !c.protos[u.Scheme].IsRedirect(response) {
			return response, nil
		}

		prev := u
		u, err = neturl.Parse(getRedirectLocation(prev, u.Scheme, response.Meta))
		if err != nil {
			return nil, err
		}
		if u.Scheme == "" {
			u.Scheme = prev.Scheme
		}
	}

	return nil, ExceededMaxRedirects
}

// Upload sends a request with a body and returns any redirect response.
func (c Client) Upload(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})
	default:
		return nil, fmt.Errorf("upload not supported on %s", u.Scheme)
	}
}

func getRedirectLocation(prev *url.URL, proto string, meta any) string {
	switch proto {
	case "gemini", "spartan":
		u, _ := url.Parse(meta.(string))
		return prev.ResolveReference(u).String()
	case "http", "https":
		return meta.(*http.Response).Header.Get("Location")
	}
	return ""
}

type httpClient struct {
	tp *http.Transport
}

func (hc httpClient) RoundTrip(request *Request) (*Response, error) {
	body, _ := request.Meta.(io.Reader)
	hreq, err := http.NewRequest("GET", request.URL.String(), body)
	if err != nil {
		return nil, err
	}

	hresp, err := hc.tp.RoundTrip(hreq)
	if err != nil {
		return nil, err
	}

	return &Response{
		Status: Status(hresp.StatusCode),
		Meta:   hresp,
		Body:   hresp.Body,
	}, nil
}

func (hc httpClient) IsRedirect(response *Response) bool {
	return response.Meta.(*http.Response).Header.Get("Location") != ""
}