Documentation ¶
Overview ¶
Package curl implements routines to fetch files given a URL.
curl currently supports HTTP, TFTP, and local files.
Index ¶
- Variables
- func Fetch(u *url.URL) (io.ReaderAt, error)
- func IsURLError(err error) bool
- func LazyFetch(u *url.URL) (io.ReaderAt, error)
- func RegisterScheme(scheme string, fs FileScheme)
- type FileScheme
- type FileSchemeRetryFilter
- type HTTPClient
- type HTTPClientCodeError
- type LocalFileClient
- type MockScheme
- type MockSchemeRetryFilter
- type SchemeWithRetries
- type Schemes
- type TFTPClient
- type URLError
Constants ¶
This section is empty.
Variables ¶
var ( // ErrWrongScheme means the wrong mocked scheme was used. ErrWrongScheme = errors.New("wrong scheme") // ErrNoSuchHost means there is no host record in the mock. ErrNoSuchHost = errors.New("no such host exists") // ErrNoSuchFile means there is no file record in the mock. ErrNoSuchFile = errors.New("no such file exists on this host") )
var ( // DefaultHTTPClient is the default HTTP FileScheme. // // It is not recommended to use this for HTTPS. We recommend creating an // http.Client that accepts only a private pool of certificates. DefaultHTTPClient = NewHTTPClient(http.DefaultClient) // DefaultTFTPClient is the default TFTP FileScheme. DefaultTFTPClient = NewTFTPClient(tftp.ClientMode(tftp.ModeOctet), tftp.ClientBlocksize(1450), tftp.ClientWindowsize(65535)) // DefaultSchemes are the schemes supported by default. DefaultSchemes = Schemes{ "tftp": DefaultTFTPClient, "http": DefaultHTTPClient, "file": &LocalFileClient{}, } )
var ( // ErrNoSuchScheme is returned by Schemes.Fetch and // Schemes.LazyFetch if there is no registered FileScheme // implementation for the given URL scheme. ErrNoSuchScheme = errors.New("no such scheme") )
Functions ¶
func RegisterScheme ¶
func RegisterScheme(scheme string, fs FileScheme)
RegisterScheme calls DefaultSchemes.Register.
Types ¶
type FileScheme ¶
type FileScheme interface { // Fetch returns a reader that gives the contents of `u`. // // It may do so by fetching `u` and placing it in a buffer, or by // returning an io.ReaderAt that fetchs the file. Fetch(u *url.URL) (io.ReaderAt, error) }
FileScheme represents the implementation of a URL scheme and gives access to fetching files of that scheme.
For example, an http FileScheme implementation would fetch files using the HTTP protocol.
func NewTFTPClient ¶
func NewTFTPClient(opts ...tftp.ClientOpt) FileScheme
NewTFTPClient returns a new TFTP client based on the given tftp.ClientOpt.
type FileSchemeRetryFilter ¶
type FileSchemeRetryFilter interface { // RetryFilter lets a FileScheme filter for errors returned by Fetch // which are worth retrying. If this interface is not implemented, the // default for SchemeWithRetries is to always retry. RetryFilter // returns true to indicate a request should be retried. RetryFilter(u *url.URL, err error) bool }
FileSchemeRetryFilter contains extra RetryFilter method for a FileScheme wrapped by SchemeWithRetries.
type HTTPClient ¶
type HTTPClient struct {
// contains filtered or unexported fields
}
HTTPClient implements FileScheme for HTTP files.
func NewHTTPClient ¶
func NewHTTPClient(c *http.Client) *HTTPClient
NewHTTPClient returns a new HTTP FileScheme based on the given http.Client.
func (HTTPClient) RetryFilter ¶
func (h HTTPClient) RetryFilter(u *url.URL, err error) bool
RetryFilter implements FileSchemeRetryFilter.
type HTTPClientCodeError ¶
HTTPClientCodeError is returned by HTTPClient.Fetch when the server replies with a non-200 code.
func (*HTTPClientCodeError) Error ¶
func (h *HTTPClientCodeError) Error() string
Error implements error for HTTPClientCodeError.
type LocalFileClient ¶
type LocalFileClient struct{}
LocalFileClient implements FileScheme for files on disk.
type MockScheme ¶
type MockScheme struct { // scheme is the scheme name. Scheme string // contains filtered or unexported fields }
MockScheme is a Scheme mock for testing.
func NewMockScheme ¶
func NewMockScheme(scheme string) *MockScheme
NewMockScheme creates a new MockScheme with the given scheme name.
func (*MockScheme) Add ¶
func (m *MockScheme) Add(host string, p string, content string)
Add adds a file to the MockScheme
func (*MockScheme) NumCalled ¶
func (m *MockScheme) NumCalled(u *url.URL) uint
NumCalled returns how many times a url has been looked up.
func (*MockScheme) SetErr ¶
func (m *MockScheme) SetErr(err error, count int)
SetErr sets the error which is returned on the next count calls to Fetch.
type MockSchemeRetryFilter ¶
type MockSchemeRetryFilter struct { *MockScheme // contains filtered or unexported fields }
MockSchemeRetryFilter is a Scheme mock for testing and has a method to implement FileSchemeRetryFilter.
func NewMockSchemeRetryFilter ¶
func NewMockSchemeRetryFilter(scheme string) *MockSchemeRetryFilter
NewMockSchemeRetryFilter creates a new MockSchemeRetryFilter with the given scheme name.
func (*MockSchemeRetryFilter) RetryFilter ¶
func (m *MockSchemeRetryFilter) RetryFilter(u *url.URL, err error) bool
RetryFilter implements FileSchemeRetryFilter.
func (*MockSchemeRetryFilter) SetRetryFilter ¶
func (m *MockSchemeRetryFilter) SetRetryFilter(f func(*url.URL, error) bool)
SetRetryFilter sets the function to be used by the RetryFilter method.
type SchemeWithRetries ¶
type SchemeWithRetries struct { Scheme FileScheme BackOff backoff.BackOff }
SchemeWithRetries wraps a FileScheme and automatically retries (with backoff) when Fetch returns a non-nil err.
type Schemes ¶
type Schemes map[string]FileScheme
Schemes is a map of URL scheme identifier -> implementation that can fetch a file for that scheme.
func (Schemes) Fetch ¶
Fetch fetchs the file with the given `u`. `u.Scheme` is used to select the FileScheme via `s`.
If `s` does not contain a FileScheme for `u.Scheme`, ErrNoSuchScheme is returned.
func (Schemes) LazyFetch ¶
LazyFetch returns a reader that will Fetch the file given by `u` when Read is called, based on `u`s scheme. See Schemes.Fetch for more details.
func (Schemes) Register ¶
func (s Schemes) Register(scheme string, fs FileScheme)
Register registers a scheme identified by `scheme` to be `fs`.
type TFTPClient ¶
type TFTPClient struct {
// contains filtered or unexported fields
}
TFTPClient implements FileScheme for TFTP files.
func (*TFTPClient) RetryFilter ¶
func (t *TFTPClient) RetryFilter(u *url.URL, err error) bool
RetryFilter implements FileSchemeRetryFilter.