Documentation
¶
Overview ¶
Package got is an HTTP client that downloads a file in concurrent byte ranges.
A download starts with a one-byte range probe. If the server answers 206 with a Content-Range, the file is split into chunks and fetched in parallel into a single sparse output file, each chunk writing at its own offset through an OffsetWriter. If the server ignores the range — no Content-Range, or a 200 with the whole body — the probe response *is* the download, and it streams straight to disk rather than being thrown away and refetched.
Chunk progress is journaled beside the output file, so an interrupted download resumes from the chunks that finished rather than from zero.
The package also carries the plain request helpers a downloader needs anyway — Got.Request, NewRequest, ParseHeaders, NormalizeURL — and a SOCKS5 constructor, NewWithProxy, that distinguishes socks5:// from socks5h:// the way curl does. That distinction matters for proxies which resolve names no DNS server knows.
It began as a fork of github.com/melbahja/got and has since been substantially rewritten; see the README for what changed.
Index ¶
- Variables
- func DefaultClient() *http.Client
- func GetFilename(URL string) string
- func NewRequest(ctx context.Context, method, URL string, headers []Header, body io.Reader) (*http.Request, error)
- func NormalizeURL(rawURL string) (string, error)
- type Chunk
- type Download
- func (d *Download) AvgSpeed() uint64
- func (d *Download) Context() context.Context
- func (d *Download) DownloadChunk(c *Chunk, dest io.Writer) error
- func (d *Download) GetInfoOrDownload() (*Info, error)
- func (d *Download) Init() error
- func (d *Download) IsRangeable() bool
- func (d *Download) Path() string
- func (d *Download) RunProgress(fn ProgressFunc)
- func (d *Download) Size() uint64
- func (d *Download) Speed() uint64
- func (d *Download) Start() error
- func (d *Download) TotalCost() time.Duration
- func (d *Download) TotalSize() uint64
- func (d *Download) Write(b []byte) (int, error)
- type Got
- type Header
- type Info
- type OffsetWriter
- type ProgressFunc
Constants ¶
This section is empty.
Variables ¶
var DefaultFileName = "got.output"
DefaultFileName is the fallback name when a filename cannot be derived from the URL.
var ErrDownloadAborted = errors.New("download aborted")
ErrDownloadAborted is returned when a download is canceled.
var UserAgent = "Got/2.0"
UserAgent is the default user agent for got HTTP requests.
Functions ¶
func DefaultClient ¶
DefaultClient returns a new default HTTP client.
func GetFilename ¶
GetFilename returns a filename derived from a URL path, or DefaultFileName.
func NewRequest ¶
func NewRequest(ctx context.Context, method, URL string, headers []Header, body io.Reader) (*http.Request, error)
NewRequest creates an http.Request with the given method, URL, headers, and optional body.
func NormalizeURL ¶
NormalizeURL ensures a URL has a scheme, defaulting to https.
Types ¶
type Chunk ¶
Chunk represents a byte range for partial content download. Chunk and OffsetWriter are the byte-range primitives inherited from melbahja/got.
type Download ¶
type Download struct {
Client *http.Client
// Concurrency is the number of concurrent chunk downloads. 0 = auto.
Concurrency uint
// URL is the download URL.
URL string
// Dir is the output directory.
Dir string
// Dest is the output filename. If empty, derived from URL or Content-Disposition.
Dest string
// Interval controls progress callback frequency in milliseconds. 0 = auto.
Interval uint64
// ChunkSize in bytes. 0 = auto.
ChunkSize uint64
// MinChunkSize sets the minimum chunk size in bytes.
MinChunkSize uint64
// MaxChunkSize sets the maximum chunk size in bytes.
MaxChunkSize uint64
// MaxRetries per chunk. 0 = 3 (default).
MaxRetries int
// Resume enables resuming a previously interrupted download using state file.
Resume bool
// Header contains additional HTTP headers for the request.
Header []Header
// StopProgress signals the progress goroutine to stop. It is read by the
// progress goroutine while another goroutine sets it, so access is atomic.
StopProgress atomic.Bool
// contains filtered or unexported fields
}
Download holds the configuration and state for a file download.
func NewDownload ¶
NewDownload creates a new Download with the given context, URL, and destination.
func (*Download) DownloadChunk ¶
DownloadChunk downloads a single byte range.
func (*Download) GetInfoOrDownload ¶
GetInfoOrDownload probes the server with a range request. If the server supports ranges (RFC 7233), it returns size info. Otherwise, it downloads the entire file in a single stream.
func (*Download) IsRangeable ¶
IsRangeable returns whether the server supports partial content.
func (*Download) RunProgress ¶
func (d *Download) RunProgress(fn ProgressFunc)
RunProgress calls fn periodically until StopProgress is set.
func (*Download) Start ¶
Start downloads all chunks concurrently and writes them to the output file.
type Got ¶
type Got struct {
ProgressFunc
// Client is the HTTP client used for requests. If nil, a default client is used.
Client *http.Client
// contains filtered or unexported fields
}
Got holds the configuration for HTTP operations.
func NewWithContext ¶
NewWithContext returns a Got with the given context and default HTTP client.
func NewWithProxy ¶
NewWithProxy returns a Got configured to route through a SOCKS5 proxy.
The proxyAddr is accepted in any of:
socks5h://host:port destination resolved by the proxy (curl --socks5-hostname).
Required for proxies that resolve non-DNS hostnames
themselves — e.g. dmsgweb's <pk>.dmsg URLs.
socks5://host:port destination resolved by this client first, then the
IP is sent to the proxy (curl --socks5).
host:port bare form, treated as socks5h:// for backward compat.
func (*Got) Do ¶
Do initializes and runs a Download, calling ProgressFunc if set.
Callers who construct a *Download by hand (e.g. `skywire-cli got dl`) typically don't set Client or ctx — without this propagation step, Download.Init falls back to DefaultClient(), silently discarding the proxy / context configured on the Got receiver. That manifests as the SOCKS5 proxy being bypassed (DNS lookups resolved locally instead of through the dialer wired up in NewWithProxy) — exactly the failure mode that turned out to outlive #3029.
type Header ¶
Header represents an HTTP header key-value pair.
func ParseHeaders ¶
ParseHeaders parses header strings in "Key: Value" format.
type OffsetWriter ¶
OffsetWriter wraps an io.WriterAt to implement io.Writer at a given offset.
type ProgressFunc ¶
type ProgressFunc func(d *Download)
ProgressFunc is called periodically during downloads to report progress.