aboutsummaryrefslogtreecommitdiff
path: root/gemini.go
diff options
context:
space:
mode:
authoradnano <[email protected]>2020-09-26 16:52:14 -0400
committeradnano <[email protected]>2020-09-26 16:52:14 -0400
commitceb40a2fab723a73b66d78e99dd924871fb5c2b7 (patch)
tree12982edabe5c035680e70ffb4138a77b4bce6322 /gemini.go
parentImplement file server (diff)
downloadgo-gemini-ceb40a2fab723a73b66d78e99dd924871fb5c2b7.tar.xz
go-gemini-ceb40a2fab723a73b66d78e99dd924871fb5c2b7.zip
Implement default client
Diffstat (limited to 'gemini.go')
-rw-r--r--gemini.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/gemini.go b/gemini.go
index ccdb5df..409069c 100644
--- a/gemini.go
+++ b/gemini.go
@@ -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)
+}