2020-10-24 13:15:32 -06:00
|
|
|
package gemini
|
2020-09-25 17:09:49 -06:00
|
|
|
|
2020-09-26 14:52:14 -06:00
|
|
|
import (
|
2020-09-27 22:38:11 -06:00
|
|
|
"crypto/tls"
|
2020-09-26 14:52:14 -06:00
|
|
|
"crypto/x509"
|
2020-10-13 18:10:04 -06:00
|
|
|
"errors"
|
2020-09-26 14:52:14 -06:00
|
|
|
"sync"
|
2020-09-27 22:38:11 -06:00
|
|
|
"time"
|
2020-09-26 14:52:14 -06:00
|
|
|
)
|
|
|
|
|
2020-10-27 17:21:33 -06:00
|
|
|
var crlf = []byte("\r\n")
|
2020-09-25 17:09:49 -06:00
|
|
|
|
2020-10-13 18:10:04 -06:00
|
|
|
// Errors.
|
|
|
|
var (
|
2020-10-24 13:15:32 -06: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")
|
2020-10-27 19:18:05 -06:00
|
|
|
ErrNotAGeminiURL = errors.New("gemini: not a Gemini URL")
|
2020-10-24 13:15:32 -06:00
|
|
|
ErrBodyNotAllowed = errors.New("gemini: response status code does not allow for body")
|
2020-10-27 20:12:10 -06:00
|
|
|
ErrTooManyRedirects = errors.New("gemini: too many redirects")
|
2020-10-13 18:10:04 -06:00
|
|
|
)
|
|
|
|
|
2020-10-27 17:21:33 -06:00
|
|
|
// DefaultClient is the default client. It is used by Get and Do.
|
2020-09-26 14:52:14 -06:00
|
|
|
//
|
2020-10-27 17:21:33 -06:00
|
|
|
// On the first request, DefaultClient loads the default list of known hosts.
|
2020-10-13 18:10:04 -06:00
|
|
|
var DefaultClient Client
|
2020-09-26 14:52:14 -06:00
|
|
|
|
2020-10-27 17:21:33 -06: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-27 20:15:36 -06:00
|
|
|
|
2020-09-26 14:52:14 -06:00
|
|
|
func init() {
|
2020-09-27 20:18:21 -06:00
|
|
|
DefaultClient.TrustCertificate = func(hostname string, cert *x509.Certificate, knownHosts *KnownHosts) error {
|
2020-10-27 17:21:33 -06:00
|
|
|
defaultClientOnce.Do(func() { knownHosts.LoadDefault() })
|
2020-09-27 20:18:21 -06:00
|
|
|
return knownHosts.Lookup(hostname, cert)
|
2020-09-26 14:52:14 -06:00
|
|
|
}
|
2020-10-27 21:34:06 -06:00
|
|
|
DefaultClient.GetCertificate = func(req *Request, store *ClientCertificateStore) *tls.Certificate {
|
|
|
|
if cert, err := store.Lookup(req.URL.Hostname(), req.URL.Path); err == nil {
|
2020-09-27 22:38:11 -06:00
|
|
|
return cert
|
|
|
|
}
|
|
|
|
duration := time.Hour
|
2020-10-27 21:34:06 -06:00
|
|
|
cert, err := NewCertificate("", duration)
|
2020-09-27 22:38:11 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-27 21:34:06 -06:00
|
|
|
store.Add(req.URL.Hostname()+req.URL.Path, cert)
|
2020-09-27 22:38:11 -06:00
|
|
|
return &cert
|
|
|
|
}
|
2020-09-26 14:52:14 -06:00
|
|
|
}
|