fetch

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const MaxDocument = 4 << 20

MaxDocument is the largest profile, manifest or listing entry accepted.

Variables

View Source
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.

View Source
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

func HashFile

func HashFile(path string) (int64, string, error)

HashFile returns the size and SHA-256 of a file on disk.

func Retryable

func Retryable(err error) bool

Retryable reports whether a failed request is worth trying again on the same URL: a network failure, a transfer cut short, or a server that is busy or failing right now. A refused URL, a missing file, a certificate problem or a file that doesn't match the manifest won't fix itself.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client downloads from a fixed set of hosts.

func New

func New(hosts []string, opts ...Option) *Client

New returns a client that only fetches from hosts.

func (*Client) Allowed

func (c *Client) Allowed(rawURL string) error

Allowed returns an error unless rawURL may be fetched by this client.

func (*Client) Document

func (c *Client) Document(ctx context.Context, rawURL string) ([]byte, error)

Document downloads a small document such as a profile or manifest.

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

func (c *Client) Post(ctx context.Context, rawURL, contentType string, body []byte) ([]byte, error)

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

func TrustCertificate(pemFile string) (Option, error)

TrustCertificate adds a PEM certificate to the trusted roots, alongside the system's. Only for the local development server (cmd/devserve).

func WithHTTPClient

func WithHTTPClient(h *http.Client) Option

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

type StatusError struct {
	URL    string
	Code   int
	Status string
}

StatusError is a response other than 200 OK.

func (*StatusError) Error

func (e *StatusError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL