fetch
import "github.com/barevoidco/zelk/core/fetch"Package fetch is the seam WebFetch reaches the web through, and its default implementation.
A Fetcher takes a URL and returns the response’s body, bounded. The default Client enforces its address policy when it connects, on the address it actually dials, so a host name that resolves to a private address, or changes what it resolves to between checks, is refused whatever its name; every redirect is checked again the same way. It never reads proxy settings or certificates from the environment: a proxy (WithProxy) and extra roots (WithRootCAs) are explicit options. Compressed responses are decompressed within the same size bound, so a small compressed body can’t expand without limit.
An Extractor optionally answers a question about a fetched page with a model, within a token reservation the session checks against its budget.
Constants
Section titled “Constants”const ( DefaultMaxBytes = 10 << 20 DefaultMaxRedirects = 5 DefaultTimeout = 30 * time.Second DefaultUserAgent = "zelk-webfetch/1")Defaults of the Client.
Variables
Section titled “Variables”var ( // ErrBlocked reports an address the policy doesn't allow: private, // loopback, link-local and similar, unless allowed explicitly. ErrBlocked = errors.New("address not allowed") // ErrURL reports a URL WebFetch can't fetch: not http or https, or // with credentials. ErrURL = errors.New("unsupported URL") // ErrTooManyRedirects reports a chain of redirects past the limit. ErrTooManyRedirects = errors.New("too many redirects"))Blocked
Section titled “Blocked”func Blocked(ip netip.Addr) boolBlocked reports whether the default policy refuses ip: loopback, private, link-local, unspecified, multicast, carrier-grade NAT and the other ranges that don’t belong to the public internet.
Client
Section titled “Client”type Client struct { // contains filtered or unexported fields}Client is the default Fetcher. It is safe for concurrent use.
func New(opts ...Option) (*Client, error)New returns a Client. It reads nothing from the process environment.
Client.Close
Section titled “Client.Close”func (c *Client) Close() errorClose releases idle connections.
Client.Fetch
Section titled “Client.Fetch”func (c *Client) Fetch(ctx context.Context, req Request) (Response, error)Fetch gets req.URL. A refused address, a bad URL or too many redirects are errors wrapping ErrBlocked, ErrURL or ErrTooManyRedirects; any HTTP status is a response.
ExtractRequest
Section titled “ExtractRequest”type ExtractRequest struct { URL, Content, Prompt string}ExtractRequest is a page and a question about it.
Extraction
Section titled “Extraction”type Extraction struct { Text string Usage *event.Usage}Extraction is an extractor’s answer and what it spent; Usage is nil if the provider didn’t report it, and the whole reservation is charged.
Extractor
Section titled “Extractor”type Extractor interface { // Limit is the most tokens one extraction may spend. Limit() event.Tokens Extract(ctx context.Context, req ExtractRequest) (Extraction, error)}Extractor answers a question about a fetched page with a model, usually a small one. Its calls are budgeted: each extraction reserves Limit from the session’s budget before it runs, and reports what it spent.
Fetcher
Section titled “Fetcher”type Fetcher interface { Fetch(ctx context.Context, req Request) (Response, error)}Fetcher fetches a URL. Implementations must be safe for concurrent use and honour ctx.
type Model struct { // contains filtered or unexported fields}Model is an Extractor that asks a model through a provider.
NewModel
Section titled “NewModel”func NewModel(p provider.Provider, model string, limit event.Tokens) (*Model, error)NewModel returns an extractor that asks model through p, spending at most limit per extraction: the page is cut so the request’s bytes stay within limit.Input, which bounds its tokens, and the answer within limit.Output.
Model.Extract
Section titled “Model.Extract”func (m *Model) Extract(ctx context.Context, req ExtractRequest) (Extraction, error)Extract asks the model the request’s question about its page.
Model.Limit
Section titled “Model.Limit”func (m *Model) Limit() event.TokensLimit returns the most one extraction spends.
Option
Section titled “Option”type Option func(*Client)Option configures a Client.
WithAllow
Section titled “WithAllow”func WithAllow(prefixes ...netip.Prefix) OptionWithAllow permits addresses in prefixes that the policy otherwise blocks, such as an internal network the host trusts.
WithHosts
Section titled “WithHosts”func WithHosts(allow func(host, port string) bool) OptionWithHosts limits requests to hosts allow accepts, the first request and every redirect alike. port is the URL’s port, or the scheme’s default. Without it any public host may be fetched.
WithMaxBytes
Section titled “WithMaxBytes”func WithMaxBytes(n int64) OptionWithMaxBytes sets the default bound on a body, after decompression.
WithMaxRedirects
Section titled “WithMaxRedirects”func WithMaxRedirects(n int) OptionWithMaxRedirects sets how many redirects a fetch follows.
WithProxy
Section titled “WithProxy”func WithProxy(proxy func(target *url.URL) (*url.URL, error)) OptionWithProxy sends each request through the HTTP or HTTPS proxy proxy names for its URL (an http, https or socks5 URL), or directly when it names none, as http.Transport.Proxy does (golang.org/x/net/http/httpproxy builds one from proxy settings). A proxy is the host’s to trust: a connection to it as the proxy is made whatever the policy says (a request that goes direct to the same address is still checked), and it reaches the target itself, so only a target given as an IP address is checked against the policy.
WithRootCAs
Section titled “WithRootCAs”func WithRootCAs(roots *x509.CertPool) OptionWithRootCAs verifies servers’ certificates against roots instead of the system’s, as a proxy that inspects TLS needs.
WithTimeout
Section titled “WithTimeout”func WithTimeout(d time.Duration) OptionWithTimeout bounds a whole fetch, redirects and body included.
WithUserAgent
Section titled “WithUserAgent”func WithUserAgent(s string) OptionWithUserAgent sets the User-Agent header.
RedirectError
Section titled “RedirectError”type RedirectError struct { // URL is where the redirect leads. URL string}RedirectError reports a redirect that SameHost didn’t follow.
RedirectError.Error
Section titled “RedirectError.Error”func (e *RedirectError) Error() stringRequest
Section titled “Request”type Request struct { URL string // MaxBytes bounds the body, after decompression; zero is the // fetcher's default. MaxBytes int64 // SameHost stops at a redirect to another host, with a *RedirectError, // for a caller whose permission to fetch was for this host alone. SameHost bool}Request is one fetch.
Response
Section titled “Response”type Response struct { // URL is where the body came from, after redirects. URL string Status int ContentType string Body []byte // Truncated reports a body cut at MaxBytes. Truncated bool}Response is a fetched resource.
Response.MediaType
Section titled “Response.MediaType”func (r Response) MediaType() stringMediaType returns a response’s media type, lowercase, without parameters.