2020-10-24 19:15:32 +00:00
|
|
|
package gemini
|
2020-09-25 23:09:49 +00:00
|
|
|
|
2020-09-26 20:52:14 +00:00
|
|
|
import (
|
2020-09-28 04:38:11 +00:00
|
|
|
"crypto/tls"
|
2020-09-26 20:52:14 +00:00
|
|
|
"crypto/x509"
|
2020-10-14 00:10:04 +00:00
|
|
|
"errors"
|
2020-09-26 20:52:14 +00:00
|
|
|
"sync"
|
2020-09-28 04:38:11 +00:00
|
|
|
"time"
|
2020-09-26 20:52:14 +00:00
|
|
|
)
|
|
|
|
|
2020-10-27 23:21:33 +00:00
|
|
|
var crlf = []byte("\r\n")
|
2020-09-25 23:09:49 +00:00
|
|
|
|
2020-10-14 00:10:04 +00:00
|
|
|
// Errors.
|
|
|
|
var (
|
2020-10-24 19:15:32 +00:00
|
|
|
ErrInvalidURL = errors.New("gemini: invalid URL")
|
|
|
|
ErrInvalidResponse = errors.New("gemini: invalid response")
|
|
|
|
ErrCertificateUnknown = errors.New("gemini: unknown certificate")
|
|
|
|
ErrCertificateExpired = errors.New("gemini: certificate expired")
|
|
|
|
ErrCertificateNotTrusted = errors.New("gemini: certificate is not trusted")
|
|
|
|
ErrNotAFile = errors.New("gemini: not a file")
|
|
|
|
ErrBodyNotAllowed = errors.New("gemini: response status code does not allow for body")
|
2020-10-14 00:10:04 +00:00
|
|
|
)
|
|
|
|
|
2020-10-27 23:21:33 +00:00
|
|
|
// DefaultClient is the default client. It is used by Get and Do.
|
2020-09-26 20:52:14 +00:00
|
|
|
//
|
2020-10-27 23:21:33 +00:00
|
|
|
// On the first request, DefaultClient loads the default list of known hosts.
|
2020-10-14 00:10:04 +00:00
|
|
|
var DefaultClient Client
|
2020-09-26 20:52:14 +00:00
|
|
|
|
2020-10-27 23:21:33 +00:00
|
|
|
// Get performs a Gemini request for the given url.
|
|
|
|
//
|
|
|
|
// Get is a wrapper around DefaultClient.Get.
|
|
|
|
func Get(url string) (*Response, error) {
|
|
|
|
return DefaultClient.Get(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do performs a Gemini request and returns a Gemini response.
|
|
|
|
//
|
|
|
|
// Do is a wrapper around DefaultClient.Do.
|
|
|
|
func Do(req *Request) (*Response, error) {
|
|
|
|
return DefaultClient.Do(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultClientOnce sync.Once
|
2020-09-28 02:15:36 +00:00
|
|
|
|
2020-09-26 20:52:14 +00:00
|
|
|
func init() {
|
2020-09-28 02:18:21 +00:00
|
|
|
DefaultClient.TrustCertificate = func(hostname string, cert *x509.Certificate, knownHosts *KnownHosts) error {
|
2020-10-27 23:21:33 +00:00
|
|
|
defaultClientOnce.Do(func() { knownHosts.LoadDefault() })
|
2020-09-28 02:18:21 +00:00
|
|
|
return knownHosts.Lookup(hostname, cert)
|
2020-09-26 20:52:14 +00:00
|
|
|
}
|
2020-10-12 20:34:52 +00:00
|
|
|
DefaultClient.GetCertificate = func(hostname string, store *CertificateStore) *tls.Certificate {
|
2020-10-12 03:48:18 +00:00
|
|
|
if cert, err := store.Lookup(hostname); err == nil {
|
2020-09-28 04:38:11 +00:00
|
|
|
return cert
|
|
|
|
}
|
|
|
|
duration := time.Hour
|
|
|
|
cert, err := NewCertificate(hostname, duration)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-12 03:48:18 +00:00
|
|
|
store.Add(hostname, cert)
|
2020-09-28 04:38:11 +00:00
|
|
|
return &cert
|
|
|
|
}
|
2020-09-26 20:52:14 +00:00
|
|
|
}
|