go-gemini/gemini.go

60 lines
1.8 KiB
Go
Raw Normal View History

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 (
"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"
"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")
ErrNotAGeminiURL = errors.New("gemini: not a Gemini URL")
2020-10-24 19:15:32 +00:00
ErrBodyNotAllowed = errors.New("gemini: response status code does not allow for body")
2020-10-28 02:12:10 +00:00
ErrTooManyRedirects = errors.New("gemini: too many redirects")
ErrInputRequired = errors.New("gemini: input required")
ErrCertificateRequired = errors.New("gemini: certificate required")
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-28 17:40:25 +00:00
DefaultClient.CreateCertificate = func(hostname, path string) (tls.Certificate, error) {
return CreateCertificate(CertificateOptions{
Duration: time.Hour,
})
}
2020-09-26 20:52:14 +00:00
}