Documentation
¶
Overview ¶
Package fetch is the only code that talks to the network. Every request is HTTPS to a host the server's profile allows, including any redirect, and a response is only used after its status and size have been checked.
Index ¶
- Constants
- Variables
- func HashFile(path string) (int64, string, error)
- func Retryable(err error) bool
- type Client
- func (c *Client) Allowed(rawURL string) error
- func (c *Client) Document(ctx context.Context, rawURL string) ([]byte, error)
- func (c *Client) File(ctx context.Context, rawURL, dest string, size int64, sha string, ...) (err error)
- func (c *Client) Post(ctx context.Context, rawURL, contentType string, body []byte) ([]byte, error)
- func (c *Client) Resume(ctx context.Context, rawURL, dest string, size int64, sha string, ...) (err error)
- type Option
- type Progress
- type StatusError
Constants ¶
const MaxDocument = 4 << 20
MaxDocument is the largest profile, manifest or listing entry accepted.
Variables ¶
var ErrNotAllowed = errors.New("not allowed for this server")
ErrNotAllowed matches every error from Allowed, including a refused redirect: the URL breaks the profile's rules, so trying again won't help.
var GitHubAssetHosts = []string{"release-assets.githubusercontent.com", "objects.githubusercontent.com"}
GitHubAssetHosts are where github.com redirects a release asset download. Redirects are checked against the allowed hosts, so files published as release assets need these allowed too.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client downloads from a fixed set of hosts.
func (*Client) File ¶
func (c *Client) File(ctx context.Context, rawURL, dest string, size int64, sha string, progress Progress) (err error)
File downloads rawURL to dest, which must not exist yet, and checks it is exactly size bytes with the given SHA-256. On any failure dest is removed, so a partial or wrong file is never left behind.
func (*Client) Post ¶
Post sends body to rawURL and returns the server's answer. A redirect is refused rather than followed, so the upload only ever goes to the URL the player was shown. It isn't retried: the server may have taken it.
func (*Client) Resume ¶
func (c *Client) Resume(ctx context.Context, rawURL, dest string, size int64, sha string, progress Progress) (err error)
Resume downloads rawURL to dest like File, but carries on from a partial download already at dest, such as one cut short or cancelled earlier, by asking the server for only the rest. A transfer that fails part way leaves what arrived at dest for the next try. The whole file must still be size bytes with the given SHA-256: when it isn't, dest is removed, and if the download had been resumed it starts once more from nothing, since the partial may be of an older version of the file.
type Option ¶
type Option func(*Client)
Option changes how a Client behaves.
func TrustCertificate ¶
TrustCertificate adds a PEM certificate to the trusted roots, alongside the system's. Only for the local development server (cmd/devserve).
func WithHTTPClient ¶
WithHTTPClient replaces the underlying HTTP client, for tests.
type Progress ¶
type Progress func(written int64)
Progress is called as a file downloads with the bytes written so far.
type StatusError ¶
StatusError is a response other than 200 OK.
func (*StatusError) Error ¶
func (e *StatusError) Error() string