2020-09-25 17:09:49 -06:00
|
|
|
package gemini
|
|
|
|
|
2020-09-26 14:52:14 -06:00
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2020-09-25 17:09:49 -06:00
|
|
|
// Status codes.
|
|
|
|
const (
|
|
|
|
StatusInput = 10
|
|
|
|
StatusSensitiveInput = 11
|
|
|
|
StatusSuccess = 20
|
|
|
|
StatusRedirectTemporary = 30
|
|
|
|
StatusRedirectPermanent = 31
|
|
|
|
StatusTemporaryFailure = 40
|
|
|
|
StatusServerUnavailable = 41
|
|
|
|
StatusCGIError = 42
|
|
|
|
StatusProxyError = 43
|
|
|
|
StatusSlowDown = 44
|
|
|
|
StatusPermanentFailure = 50
|
|
|
|
StatusNotFound = 51
|
|
|
|
StatusGone = 52
|
|
|
|
StatusProxyRequestRefused = 53
|
|
|
|
StatusBadRequest = 59
|
|
|
|
StatusClientCertificateRequired = 60
|
|
|
|
StatusCertificateNotAuthorised = 61
|
|
|
|
StatusCertificateNotValid = 62
|
|
|
|
)
|
|
|
|
|
|
|
|
// Status code categories.
|
|
|
|
const (
|
|
|
|
StatusClassInput = 1
|
|
|
|
StatusClassSuccess = 2
|
|
|
|
StatusClassRedirect = 3
|
|
|
|
StatusClassTemporaryFailure = 4
|
|
|
|
StatusClassPermanentFailure = 5
|
|
|
|
StatusClassClientCertificateRequired = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
crlf = []byte("\r\n")
|
|
|
|
)
|
2020-09-26 14:52:14 -06:00
|
|
|
|
|
|
|
// 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{
|
2020-09-27 14:10:36 -06:00
|
|
|
TrustCertificate: func(hostname string, cert *x509.Certificate, knownHosts *KnownHosts) error {
|
2020-09-26 14:52:14 -06:00
|
|
|
// 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)
|
2020-09-27 14:10:36 -06:00
|
|
|
return knownHosts.Lookup(hostname, cert)
|
2020-09-26 14:52:14 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var setupDefaultClientOnce sync.Once
|
|
|
|
|
|
|
|
func setupDefaultClient() {
|
2020-09-27 12:18:30 -06:00
|
|
|
DefaultClient.KnownHosts.Load()
|
2020-09-26 14:52:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|