From ceb40a2fab723a73b66d78e99dd924871fb5c2b7 Mon Sep 17 00:00:00 2001 From: adnano Date: Sat, 26 Sep 2020 16:52:14 -0400 Subject: [PATCH] Implement default client --- gemini.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) 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) +}