go-gemini/gemini.go

43 lines
1.1 KiB
Go
Raw Normal View History

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-10-13 18:10:04 -06:00
"errors"
2020-09-26 14:52:14 -06:00
"sync"
)
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")
ErrCertificateExpired = errors.New("gemini: certificate expired")
ErrCertificateNotFound = errors.New("gemini: certificate not found")
2020-10-31 20:45:21 -06:00
ErrCertificateNotTrusted = errors.New("gemini: certificate not trusted")
2020-11-04 21:46:05 -07:00
ErrBodyNotAllowed = errors.New("gemini: response body not allowed")
2020-10-27 20:12:10 -06:00
ErrTooManyRedirects = errors.New("gemini: too many redirects")
2020-10-13 18:10:04 -06:00
)
2020-11-01 12:27:49 -07:00
// defaultClient is the default client. It is used by Get and Do.
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.
func Get(url string) (*Response, error) {
setupDefaultClientOnce()
2020-11-01 12:27:49 -07:00
return defaultClient.Get(url)
2020-10-27 17:21:33 -06:00
}
// Do performs a Gemini request and returns a Gemini response.
func Do(req *Request) (*Response, error) {
setupDefaultClientOnce()
2020-11-01 12:27:49 -07:00
return defaultClient.Do(req)
2020-10-27 17:21:33 -06:00
}
var defaultClientOnce sync.Once
2020-09-27 20:15:36 -06:00
func setupDefaultClientOnce() {
defaultClientOnce.Do(func() {
2020-11-01 12:27:49 -07:00
defaultClient.KnownHosts.LoadDefault()
})
2020-09-26 14:52:14 -06:00
}