diff options
| author | adnano <[email protected]> | 2020-09-26 16:52:14 -0400 |
|---|---|---|
| committer | adnano <[email protected]> | 2020-09-26 16:52:14 -0400 |
| commit | ceb40a2fab723a73b66d78e99dd924871fb5c2b7 (patch) | |
| tree | 12982edabe5c035680e70ffb4138a77b4bce6322 /gemini.go | |
| parent | Implement file server (diff) | |
| download | go-gemini-ceb40a2fab723a73b66d78e99dd924871fb5c2b7.tar.xz go-gemini-ceb40a2fab723a73b66d78e99dd924871fb5c2b7.zip | |
Implement default client
Diffstat (limited to 'gemini.go')
| -rw-r--r-- | gemini.go | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -1,5 +1,10 @@ package gemini +import ( + "crypto/x509" + "sync" +) + // Status codes. const ( StatusInput = 10 @@ -35,3 +40,36 @@ const ( var ( crlf = []byte("\r\n") ) + +// DefaultClient is the default client. It is used by Send. +// +// On the first request, DefaultClient will load the default list of known hosts. +var DefaultClient *Client + +func init() { + DefaultClient = &Client{ + TrustCertificate: func(cert *x509.Certificate, knownHosts *KnownHosts) error { + // Load the hosts only once. This is so that the hosts don't have to be loaded + // for those using their own clients. + setupDefaultClientOnce.Do(setupDefaultClient) + return knownHosts.Lookup(cert) + }, + } +} + +var setupDefaultClientOnce sync.Once + +func setupDefaultClient() { + knownHosts, err := LoadKnownHosts() + if err != nil { + knownHosts = &KnownHosts{} + } + DefaultClient.KnownHosts = knownHosts +} + +// Send sends a Gemini request and returns a Gemini response. +// +// Send is a wrapper around DefaultClient.Send. +func Send(req *Request) (*Response, error) { + return DefaultClient.Send(req) +} |