Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/handler/cooldown_supported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import (

func TestCooldownSupportedCoversHandlers(t *testing.T) {
calls := regexp.MustCompile(`(?:IsAllowed|Evaluate)\("([a-z]+)"|withheld\(h\.proxy, "([a-z]+)"|IsAllowed\((helmMetadataEcosystem)`)
// Metadata rewrites moved out of the handlers (#501) check cooldown in
// internal/metadata.
files, err := filepath.Glob("*.go")
if err != nil {
t.Fatal(err)
}
extracted, err := filepath.Glob("../metadata/*/*.go")
if err != nil {
t.Fatal(err)
}
files = append(files, extracted...)
found := map[string]bool{}
for _, f := range files {
if strings.HasSuffix(f, "_test.go") {
Expand Down
54 changes: 7 additions & 47 deletions internal/handler/nuget.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package handler

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"

"github.com/git-pkgs/proxy/internal/metadata/nuget"
"github.com/git-pkgs/proxy/internal/retention"
"github.com/git-pkgs/purl"
)
Expand Down Expand Up @@ -122,56 +122,16 @@ func (h *NuGetHandler) handleServiceIndex(w http.ResponseWriter, r *http.Request
_, _ = w.Write(rewritten)
}

// rewriteServiceIndex rewrites service URLs in the index to point at this proxy.
// rewriteServiceIndex rewrites service URLs in the index to point at this
// proxy (see nuget.RewriteServiceIndex).
func (h *NuGetHandler) rewriteServiceIndex(body []byte) ([]byte, error) {
var index map[string]any
if err := json.Unmarshal(body, &index); err != nil {
return nil, err
}

resources, ok := index["resources"].([]any)
if !ok {
return body, nil
}

for _, res := range resources {
rmap, ok := res.(map[string]any)
if !ok {
continue
}

id, _ := rmap["@id"].(string)
rtype, _ := rmap["@type"].(string)

// Rewrite URLs for services we proxy. The service type determines the
// local route because an upstream index may advertise a different host.
if id != "" {
rmap["@id"] = h.rewriteNuGetURL(id, rtype)
}
}

return json.Marshal(index)
return nuget.RewriteServiceIndex(body, h.proxyURL)
}

// rewriteNuGetURL rewrites a NuGet service URL based on its advertised type.
// Service types the proxy does not handle are returned unchanged.
// rewriteNuGetURL rewrites a NuGet service URL based on its advertised type
// (see nuget.ServiceURL).
func (h *NuGetHandler) rewriteNuGetURL(origURL, serviceType string) string {
switch serviceType {
case "PackageBaseAddress/3.0.0":
return h.proxyURL + "/nuget/v3-flatcontainer/"
case "RegistrationsBaseUrl", "RegistrationsBaseUrl/3.0.0-beta", "RegistrationsBaseUrl/3.0.0-rc":
return h.proxyURL + "/nuget/v3/registration5-semver1/"
case "RegistrationsBaseUrl/3.4.0":
return h.proxyURL + "/nuget/v3/registration5-gz-semver1/"
case "RegistrationsBaseUrl/3.6.0", "RegistrationsBaseUrl/Versioned":
return h.proxyURL + "/nuget/v3/registration5-gz-semver2/"
case "SearchQueryService", "SearchQueryService/3.0.0-rc", "SearchQueryService/3.5.0":
return h.proxyURL + "/nuget/query"
case "SearchAutocompleteService", "SearchAutocompleteService/3.5.0":
return h.proxyURL + "/nuget/autocomplete"
default:
return origURL
}
return nuget.ServiceURL(h.proxyURL, origURL, serviceType)
}

// handleDownload serves a package file, fetching and caching from upstream if needed.
Expand Down
Loading