Watch
1
0
Fork
You've already forked pkg-proxy
1
mirror of https://github.com/git-pkgs/proxy.git synced 2026-11-09 23:59:16 -05:00
pkg-proxy/internal/handler/maven.go

218 lines
7.3 KiB
Go
Raw Permalink Normal View History

2026-01-20 21:52:44 +00:00
package handler
import (
"errors"
2026-01-20 21:52:44 +00:00
"fmt"
"net/http"
"path"
"strings"
"github.com/git-pkgs/proxy/internal/retention"
"github.com/git-pkgs/purl"
2026-01-20 21:52:44 +00:00
)
const (
mavenCentralUpstream = "https://repo1.maven.org/maven2"
gradlePluginPortalUpstream = "https://plugins.gradle.org/m2"
minMavenParts = 4 // group path segments + artifact + version + filename
2026-01-20 21:52:44 +00:00
)
// Maven files are stored under pkg:maven/{group}/{artifact}, built from the
// group:artifact name, which is what the default key mapping produces from
// a package rule. Names keep their case, as Maven coordinates do.
func init() {
retention.Default.Register(retention.Spec{Key: "maven", CanonicalPackage: mavenRetentionKey})
}
// mavenRetentionKey maps a package rule to the stored package PURL. A key
// without a group, or with the group written as a path (com/google/guava)
// instead of dotted, parses fine but can never match a stored package, so
// it is refused rather than accepted as a rule that does nothing.
func mavenRetentionKey(p *purl.PURL) (string, bool) {
if p == nil || p.Namespace == "" || strings.Contains(p.Namespace, "/") {
return "", false
}
return retention.DefaultCanonical("maven")(p)
}
2026-01-20 21:52:44 +00:00
// MavenHandler handles Maven repository protocol requests.
type MavenHandler struct {
proxy *Proxy
upstreamURL string
pluginPortalUpstreamURL string
proxyURL string
2026-01-20 21:52:44 +00:00
}
// NewMavenHandler creates a new Maven repository handler.
func NewMavenHandler(proxy *Proxy, proxyURL, upstreamURL, pluginPortalUpstreamURL string) *MavenHandler {
if strings.TrimSpace(upstreamURL) == "" {
upstreamURL = mavenCentralUpstream
}
if strings.TrimSpace(pluginPortalUpstreamURL) == "" {
pluginPortalUpstreamURL = gradlePluginPortalUpstream
}
2026-01-20 21:52:44 +00:00
return &MavenHandler{
proxy: proxy,
upstreamURL: strings.TrimSuffix(upstreamURL, "/"),
pluginPortalUpstreamURL: strings.TrimSuffix(pluginPortalUpstreamURL, "/"),
proxyURL: strings.TrimSuffix(proxyURL, "/"),
2026-01-20 21:52:44 +00:00
}
}
// Routes returns the HTTP handler for Maven requests.
func (h *MavenHandler) Routes() http.Handler {
mux := http.NewServeMux()
// Maven repository layout: /{group}/{artifact}/{version}/{filename}
// e.g., /com/google/guava/guava/32.1.3-jre/guava-32.1.3-jre.jar
mux.HandleFunc("GET /", h.handleRequest)
return mux
}
// handleRequest routes Maven requests based on the path.
func (h *MavenHandler) handleRequest(w http.ResponseWriter, r *http.Request) {
urlPath := strings.TrimPrefix(r.URL.Path, "/")
if urlPath == "" {
http.NotFound(w, r)
return
}
filename := path.Base(urlPath)
if h.isVersionListingFile(filename) {
h.handleMetadata(w, r, urlPath)
2026-01-20 21:52:44 +00:00
return
}
if h.isArtifactFile(filename) || h.isChecksumFile(filename) {
2026-01-20 21:52:44 +00:00
h.handleDownload(w, r, urlPath)
return
}
// Proxy everything else (directory listings etc.)
2026-01-20 21:52:44 +00:00
h.proxyUpstream(w, r)
}
func (h *MavenHandler) handleMetadata(w http.ResponseWriter, r *http.Request, urlPath string) {
cacheKey := strings.ReplaceAll(urlPath, "/", "_")
upstreamURL := fmt.Sprintf("%s/%s", h.upstreamURL, urlPath)
body, contentType, err := h.proxy.FetchOrCacheMetadata(r.Context(), "maven", cacheKey, upstreamURL, "*/*")
if err != nil {
if errors.Is(err, ErrUpstreamNotFound) {
pluginPortalURL := fmt.Sprintf("%s/%s", h.pluginPortalUpstreamURL, urlPath)
h.proxy.Logger.Info("maven metadata unavailable in primary upstream, trying Gradle Plugin Portal",
"path", urlPath)
body, contentType, err = h.proxy.FetchOrCacheMetadata(r.Context(), "maven", cacheKey, pluginPortalURL, "*/*")
}
}
if err != nil {
if errors.Is(err, ErrUpstreamNotFound) {
http.Error(w, "not found", http.StatusNotFound)
return
}
if writeUpstreamHeld(w, err) {
return
}
h.proxy.Logger.Error("metadata fetch failed", "error", err)
http.Error(w, "failed to fetch from upstream", http.StatusBadGateway)
return
}
h.proxy.writeMetadataCachedResponse(w, r, "maven", cacheKey, body, contentType)
}
2026-01-20 21:52:44 +00:00
// handleDownload serves an artifact file, fetching and caching from upstream if needed.
func (h *MavenHandler) handleDownload(w http.ResponseWriter, r *http.Request, urlPath string) {
// Parse Maven path: group/artifact/version/filename
// e.g., com/google/guava/guava/32.1.3-jre/guava-32.1.3-jre.jar
group, artifact, version, filename := h.parsePath(urlPath)
if artifact == "" {
h.proxyUpstream(w, r)
return
}
// Maven uses group:artifact as the package name
name := fmt.Sprintf("%s:%s", group, artifact)
h.proxy.Logger.Info("maven download request",
"group", group, "artifact", artifact, "version", version, "filename", filename)
upstreamURL := fmt.Sprintf("%s/%s", h.upstreamURL, urlPath)
result, err := h.proxy.GetOrFetchArtifactFromURL(r.Context(), "maven", name, version, filename, upstreamURL)
if err != nil {
if errors.Is(err, ErrUpstreamNotFound) {
pluginPortalURL := fmt.Sprintf("%s/%s", h.pluginPortalUpstreamURL, urlPath)
h.proxy.Logger.Info("maven artifact not found in primary upstream, trying Gradle Plugin Portal",
"group", group, "artifact", artifact, "version", version, "filename", filename)
result, err = h.proxy.GetOrFetchArtifactFromURL(r.Context(), "maven", name, version, filename, pluginPortalURL)
}
}
if err != nil {
h.proxy.serveArtifactError(w, err, "failed to fetch artifact")
2026-01-20 21:52:44 +00:00
return
}
Support HTTP byte-range requests for local file cached artifact downloads (#404) * feat(storage): expose efficient seek for local files Open file-backed artifacts with os.Open so local readers support efficient seeking. Leave cloud storage readers unchanged. Keep the legacy sidecar mapping test focused on fileblob's reader: Blob.Open now reads local files directly and no longer consults the sidecar. Continue checking sidecar cleanup and post-store reads. * feat(handler): preserve efficient seek through integrity checks Keep the cache result's single Reader field and preserve io.Seeker when the underlying storage reader supports efficient seeking. Sequential reads continue through whole-object integrity verification. A successful seek switches subsequent reads to the source for range responses, where full-object verification is not possible. Test both modes and verify the source is closed once. * feat(handler): add request-aware byte range responses Add a request-aware artifact serving helper for bounded, open-ended, and suffix byte ranges, including If-Range handling, 206 responses, and 416 responses for valid unsatisfiable ranges. Ignore malformed and multi-range requests by serving the full artifact. Advertise byte-range support only for seekable readers, preserving HEAD, redirect, and non-seekable behavior. Clarify that net/http recovers ErrAbortHandler per request and keeps the server running. * feat(handler): enable byte ranges across artifact downloads Forward each artifact request to the shared range-aware response helper, including cached generic release assets and OCI blobs. This lets the handler honor ranges only when its reader supports efficient seeking, without changing direct-storage redirect behavior. Add warm-cache endpoint tests that assert partial response headers and bytes without an upstream refetch. Document supported range behavior, unsupported reader fallback, and the integrity limitation of partial reads.
2026-10-04 09:48:41 -07:00
ServeArtifactRequest(w, r, result)
2026-01-20 21:52:44 +00:00
}
// parsePath extracts Maven coordinates from a URL path.
// e.g., "com/google/guava/guava/32.1.3-jre/guava-32.1.3-jre.jar"
// -> ("com.google.guava", "guava", "32.1.3-jre", "guava-32.1.3-jre.jar")
func (h *MavenHandler) parsePath(urlPath string) (group, artifact, version, filename string) {
parts := strings.Split(urlPath, "/")
if len(parts) < minMavenParts {
2026-01-20 21:52:44 +00:00
return "", "", "", ""
}
filename = parts[len(parts)-1]
version = parts[len(parts)-2]
artifact = parts[len(parts)-3]
groupParts := parts[:len(parts)-3]
group = strings.Join(groupParts, ".")
return group, artifact, version, filename
}
// isArtifactFile returns true if the filename looks like a Maven artifact.
func (h *MavenHandler) isArtifactFile(filename string) bool {
// Common artifact extensions, and the archives distributions such as
// apache-maven-{version}-bin.tar.gz are published as.
extensions := []string{
".jar", ".war", ".ear", ".pom", ".aar", ".klib", ".module",
".zip", ".tar.gz", ".tgz", ".tar.bz2", ".tar.xz",
}
2026-01-20 21:52:44 +00:00
for _, ext := range extensions {
if strings.HasSuffix(filename, ext) {
return true
}
}
return false
}
// isVersionListingFile matches maven-metadata.xml and its checksums. The
// listing changes with every release, so it is cached for the metadata TTL
// rather than kept like an artifact.
func (h *MavenHandler) isVersionListingFile(filename string) bool {
return strings.HasPrefix(filename, "maven-metadata.xml")
}
// isChecksumFile matches the checksum and signature files published beside
// an artifact. They are as immutable as the artifact, so they are cached
// with it rather than refetched on every request.
func (h *MavenHandler) isChecksumFile(filename string) bool {
for _, ext := range []string{".sha1", ".sha256", ".sha512", ".md5", ".asc"} {
if strings.HasSuffix(filename, ext) {
return true
}
}
return false
2026-01-20 21:52:44 +00:00
}
// proxyUpstream forwards a request to Maven Central without caching.
func (h *MavenHandler) proxyUpstream(w http.ResponseWriter, r *http.Request) {
h.proxy.ProxyUpstream(w, r, h.upstreamURL+r.URL.Path, nil)
2026-01-20 21:52:44 +00:00
}