Documentation
¶
Overview ¶
Package imagefetch fetches and checks images: the reader's image proxy and prefetch download remote ones through a client of its own (public hosts only, no cookies, no Referer, a generic User-Agent), and a message's inline ones count against one Budget. Every image must be a PNG, JPEG, GIF or WebP within the per-image limits.
Index ¶
- Constants
- Variables
- func CheckAvatarImage(data []byte, source string) (out []byte, format string, width, height int, err error)
- func Config(data []byte) (format string, px int64, err error)
- func DataURL(src string) ([]byte, error)
- func Dims(data []byte) (format string, width, height int, err error)
- func Download(ctx context.Context, c *http.Client, src string) ([]byte, error)
- func DownloadAvatar(ctx context.Context, client *http.Client, src string) ([]byte, error)
- func Encode(format string, data []byte) string
- func Fetch(ctx context.Context, c *http.Client, src string) (data []byte, format string, width, height int, err error)
- func IsHTTP(u *url.URL) bool
- func NewAvatarClient(base *http.Client) *http.Client
- func NewClient(allowLoopback bool) *http.Client
- func PublicIP(ip netip.Addr) bool
- type Budget
Constants ¶
const ( MaxImages = 50 // images, remote and inline MaxImageBytes = 5 << 20 // one image MaxTotalBytes = 20 << 20 // all images together MaxPixels = 40_000_000 MaxTotalPixels = 100_000_000 // all images together, decoded Timeout = 15 * time.Second MaxRedirects = 5 )
The budget of one message's images (global constraints).
const MaxAvatarBytes = 256 << 10
const UserAgent = "Mozilla/5.0 (compatible; ImageProxy)"
UserAgent is what the proxy sends: no browser, no app version.
Variables ¶
var ( ErrTooMany = errors.New("too many images") ErrTooLarge = errors.New("image too large") ErrTotalTooLarge = errors.New("images too large together") ErrPixels = errors.New("image has too many pixels") ErrTotalPixels = errors.New("images have too many pixels together") ErrFormat = errors.New("not a PNG, JPEG, GIF or WebP image") ErrScheme = errors.New("image address is not http or https") ErrHost = errors.New("image host is not public") )
The reasons an image is refused.
var ErrAvatarMiss = errors.New("avatar unavailable")
ErrAvatarMiss is a deterministic miss (404 or a rejected image), safe to cache for 24 hours.
Formats are the formats shown, as image.DecodeConfig names them.
Functions ¶
func CheckAvatarImage ¶ added in v0.3.0
func CheckAvatarImage(data []byte, source string) (out []byte, format string, width, height int, err error)
CheckAvatarImage validates source-specific image bytes; it does not establish BIMI message trust. ICO is converted to PNG; SVG remains vector data.
func Dims ¶ added in v0.1.0
Dims is data's format and size, read from its header: a PNG, JPEG, GIF or WebP image (by content) of at most MaxPixels.
func Download ¶ added in v0.1.0
Download gets src (http or https) with c: at most MaxImageBytes of it.
func DownloadAvatar ¶ added in v0.3.0
DownloadAvatar streams at most 256 KiB and permits only HTTPS, including redirects.
func Fetch ¶ added in v0.1.0
func Fetch(ctx context.Context, c *http.Client, src string) (data []byte, format string, width, height int, err error)
Fetch downloads src and checks it: its bytes, format and size in pixels.
func NewAvatarClient ¶ added in v0.3.0
NewAvatarClient retains the base transport's public-address dial checks and redirect policy. The injected base is trusted, just as for ordinary images.
Types ¶
type Budget ¶
type Budget struct {
// contains filtered or unexported fields
}
Budget is one message's images' budget: their count, bytes and decoded pixels, inline and remote together. It is safe for concurrent use.
func (*Budget) TakeInline ¶
TakeInline takes an inline image of size bytes and px pixels: its count, bytes and pixels, all or none of them but the count (as a remote image's, which is counted once asked for).
func (*Budget) TakePixels ¶
TakePixels takes px decoded pixels; ErrTotalPixels when fewer are left.