handler

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: GPL-3.0 Imports: 27 Imported by: 0

Documentation

Overview

Package handler provides HTTP protocol handlers for package manager proxying.

Index

Constants

This section is empty.

Variables

View Source
var ErrMetadataTooLarge = errors.New("metadata response exceeds size limit")

ErrMetadataTooLarge is returned when upstream metadata exceeds maxMetadataSize.

View Source
var ErrUpstreamNotFound = fmt.Errorf("upstream: not found")

ErrUpstreamNotFound indicates the upstream returned 404.

Functions

func JSONError

func JSONError(w http.ResponseWriter, status int, message string)

JSONError writes a JSON error response.

func ReadMetadata

func ReadMetadata(r io.Reader) ([]byte, error)

ReadMetadata reads an upstream response body with a size limit to prevent OOM from unexpectedly large responses. Returns ErrMetadataTooLarge if the response is truncated by the limit.

func ServeArtifact

func ServeArtifact(w http.ResponseWriter, result *CacheResult)

ServeArtifact writes a CacheResult to an HTTP response.

Types

type CRANHandler

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

CRANHandler handles CRAN (R) registry protocol requests.

func NewCRANHandler

func NewCRANHandler(proxy *Proxy, proxyURL string) *CRANHandler

NewCRANHandler creates a new CRAN protocol handler.

func (*CRANHandler) Routes

func (h *CRANHandler) Routes() http.Handler

Routes returns the HTTP handler for CRAN requests.

type CacheResult

type CacheResult struct {
	Reader      io.ReadCloser
	RedirectURL string
	Size        int64
	ContentType string
	Hash        string
	Cached      bool
}

CacheResult contains information about a cached or fetched artifact.

type CargoConfig

type CargoConfig struct {
	DL  string `json:"dl"`
	API string `json:"api,omitempty"`
}

CargoConfig is the registry configuration returned by config.json.

type CargoHandler

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

CargoHandler handles cargo registry protocol requests.

func NewCargoHandler

func NewCargoHandler(proxy *Proxy, proxyURL string) *CargoHandler

NewCargoHandler creates a new cargo protocol handler.

func (*CargoHandler) Routes

func (h *CargoHandler) Routes() http.Handler

Routes returns the HTTP handler for cargo requests. Mount this at /cargo on your router.

type ComposerHandler

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

ComposerHandler handles Composer/Packagist registry protocol requests.

func NewComposerHandler

func NewComposerHandler(proxy *Proxy, proxyURL string) *ComposerHandler

NewComposerHandler creates a new Composer protocol handler.

func (*ComposerHandler) Routes

func (h *ComposerHandler) Routes() http.Handler

Routes returns the HTTP handler for Composer requests.

type ConanHandler

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

ConanHandler handles Conan registry protocol requests.

func NewConanHandler

func NewConanHandler(proxy *Proxy, proxyURL string) *ConanHandler

NewConanHandler creates a new Conan protocol handler.

func (*ConanHandler) Routes

func (h *ConanHandler) Routes() http.Handler

Routes returns the HTTP handler for Conan requests.

type CondaHandler

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

CondaHandler handles Conda/Anaconda registry protocol requests.

func NewCondaHandler

func NewCondaHandler(proxy *Proxy, proxyURL string) *CondaHandler

NewCondaHandler creates a new Conda protocol handler.

func (*CondaHandler) Routes

func (h *CondaHandler) Routes() http.Handler

Routes returns the HTTP handler for Conda requests.

type ContainerHandler

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

ContainerHandler handles OCI/Docker container registry protocol requests. It implements the OCI Distribution Spec for pulling images. Reference: https://github.com/opencontainers/distribution-spec/blob/main/spec.md

func NewContainerHandler

func NewContainerHandler(proxy *Proxy, proxyURL string) *ContainerHandler

NewContainerHandler creates a new container registry protocol handler.

func (*ContainerHandler) Routes

func (h *ContainerHandler) Routes() http.Handler

Routes returns the HTTP handler for container registry requests. Mount this at /v2 on your router.

type DebianHandler

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

DebianHandler handles APT/Debian repository protocol requests. It proxies requests to upstream Debian/Ubuntu repositories and caches .deb packages.

func NewDebianHandler

func NewDebianHandler(proxy *Proxy, proxyURL string) *DebianHandler

NewDebianHandler creates a new Debian/APT protocol handler.

func (*DebianHandler) Routes

func (h *DebianHandler) Routes() http.Handler

Routes returns the HTTP handler for Debian requests. Mount this at /debian on your router.

type GemHandler

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

GemHandler handles RubyGems registry protocol requests.

func NewGemHandler

func NewGemHandler(proxy *Proxy, proxyURL string) *GemHandler

NewGemHandler creates a new RubyGems protocol handler.

func (*GemHandler) Routes

func (h *GemHandler) Routes() http.Handler

Routes returns the HTTP handler for RubyGems requests.

type GoHandler

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

GoHandler handles Go module proxy protocol requests.

func NewGoHandler

func NewGoHandler(proxy *Proxy, proxyURL string) *GoHandler

NewGoHandler creates a new Go module proxy handler.

func (*GoHandler) Routes

func (h *GoHandler) Routes() http.Handler

Routes returns the HTTP handler for Go proxy requests.

type HexHandler

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

HexHandler handles Hex.pm registry protocol requests.

func NewHexHandler

func NewHexHandler(proxy *Proxy, proxyURL string) *HexHandler

NewHexHandler creates a new Hex.pm protocol handler.

func (*HexHandler) Routes

func (h *HexHandler) Routes() http.Handler

Routes returns the HTTP handler for Hex requests.

type MavenHandler

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

MavenHandler handles Maven repository protocol requests.

func NewMavenHandler

func NewMavenHandler(proxy *Proxy, proxyURL string) *MavenHandler

NewMavenHandler creates a new Maven repository handler.

func (*MavenHandler) Routes

func (h *MavenHandler) Routes() http.Handler

Routes returns the HTTP handler for Maven requests.

type NPMHandler

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

NPMHandler handles npm registry protocol requests.

func NewNPMHandler

func NewNPMHandler(proxy *Proxy, proxyURL string) *NPMHandler

NewNPMHandler creates a new npm protocol handler.

func (*NPMHandler) Routes

func (h *NPMHandler) Routes() http.Handler

Routes returns the HTTP handler for npm requests. Mount this at /npm on your router.

type NuGetHandler

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

NuGetHandler handles NuGet V3 API protocol requests.

func NewNuGetHandler

func NewNuGetHandler(proxy *Proxy, proxyURL string) *NuGetHandler

NewNuGetHandler creates a new NuGet protocol handler.

func (*NuGetHandler) Routes

func (h *NuGetHandler) Routes() http.Handler

Routes returns the HTTP handler for NuGet requests.

type Proxy

type Proxy struct {
	DB             *database.DB
	Storage        storage.Storage
	Fetcher        fetch.FetcherInterface
	Resolver       *fetch.Resolver
	Logger         *slog.Logger
	Cooldown       *cooldown.Config
	CacheMetadata  bool
	MetadataTTL    time.Duration
	DirectServe    bool
	DirectServeTTL time.Duration
	// DirectServeBaseURL, if set, replaces the scheme and host of presigned
	// URLs so clients receive a public address even when the proxy reaches
	// storage at an internal one.
	DirectServeBaseURL string
	HTTPClient         *http.Client
}

Proxy provides shared functionality for protocol handlers.

func NewProxy

func NewProxy(db *database.DB, store storage.Storage, fetcher fetch.FetcherInterface, resolver *fetch.Resolver, logger *slog.Logger) *Proxy

NewProxy creates a new Proxy with the given dependencies.

func (*Proxy) FetchOrCacheMetadata

func (p *Proxy) FetchOrCacheMetadata(ctx context.Context, ecosystem, cacheKey, upstreamURL string, acceptHeaders ...string) ([]byte, string, error)

FetchOrCacheMetadata fetches metadata from upstream with caching. On success it returns the raw response bytes and content type. If upstream fails and a cached copy exists, the cached version is returned. cacheKey is typically the package name but can include subpath components. Optional acceptHeaders specify the Accept header(s) to send; defaults to application/json.

func (*Proxy) GetOrFetchArtifact

func (p *Proxy) GetOrFetchArtifact(ctx context.Context, ecosystem, name, version, filename string) (*CacheResult, error)

GetOrFetchArtifact retrieves an artifact from cache or fetches from upstream.

func (*Proxy) GetOrFetchArtifactFromURL

func (p *Proxy) GetOrFetchArtifactFromURL(ctx context.Context, ecosystem, name, version, filename, downloadURL string) (*CacheResult, error)

GetOrFetchArtifactFromURL retrieves an artifact from cache or fetches from a specific URL. This is useful for registries where download URLs are determined from metadata.

func (*Proxy) GetOrFetchArtifactFromURLWithHeaders

func (p *Proxy) GetOrFetchArtifactFromURLWithHeaders(ctx context.Context, ecosystem, name, version, filename, downloadURL string, headers http.Header) (*CacheResult, error)

GetOrFetchArtifactFromURLWithHeaders retrieves an artifact from cache or fetches from a URL with additional HTTP headers. This is needed for registries that require authentication (e.g. Docker Hub requires a Bearer token even for public images).

func (*Proxy) ProxyCached

func (p *Proxy) ProxyCached(w http.ResponseWriter, r *http.Request, upstreamURL, ecosystem, cacheKey string, acceptHeaders ...string)

ProxyCached fetches metadata from upstream (with optional caching for offline fallback) and writes it to the response. Optional acceptHeaders specify the Accept header to send. When metadata caching is disabled, the response is streamed directly to avoid buffering large metadata responses (e.g. npm packages with many versions) in memory.

func (*Proxy) ProxyFile

func (p *Proxy) ProxyFile(w http.ResponseWriter, r *http.Request, upstreamURL string)

ProxyFile forwards a file request to upstream, copying all response headers.

func (*Proxy) ProxyUpstream

func (p *Proxy) ProxyUpstream(w http.ResponseWriter, r *http.Request, upstreamURL string, forwardHeaders []string)

ProxyUpstream forwards a request to an upstream URL without caching. It copies the request, forwards specified headers, and streams the response back. If forwardHeaders is nil, all response headers are copied.

type PubHandler

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

PubHandler handles pub.dev registry protocol requests.

func NewPubHandler

func NewPubHandler(proxy *Proxy, proxyURL string) *PubHandler

NewPubHandler creates a new pub.dev protocol handler.

func (*PubHandler) Routes

func (h *PubHandler) Routes() http.Handler

Routes returns the HTTP handler for pub requests.

type PyPIHandler

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

PyPIHandler handles PyPI registry protocol requests.

func NewPyPIHandler

func NewPyPIHandler(proxy *Proxy, proxyURL string) *PyPIHandler

NewPyPIHandler creates a new PyPI protocol handler.

func (*PyPIHandler) Routes

func (h *PyPIHandler) Routes() http.Handler

Routes returns the HTTP handler for PyPI requests.

type RPMHandler

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

RPMHandler handles RPM/Yum repository protocol requests. It proxies requests to upstream RPM repositories and caches .rpm packages.

func NewRPMHandler

func NewRPMHandler(proxy *Proxy, proxyURL string) *RPMHandler

NewRPMHandler creates a new RPM/Yum protocol handler.

func (*RPMHandler) Routes

func (h *RPMHandler) Routes() http.Handler

Routes returns the HTTP handler for RPM requests. Mount this at /rpm on your router.

Jump to

Keyboard shortcuts

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