Skip to content

Commit

Permalink
internal/frontend: add support for golang.org/dl
Browse files Browse the repository at this point in the history
pkg.go.dev/golang.org/dl now shows the contents of that module, instead
of a 400.

Fixes golang/go#43075

Change-Id: If8c6c78ed521fc7f1b93b7f23f70eacfe771587e
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/276232
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
  • Loading branch information
julieqiu committed Dec 8, 2020
1 parent 28e964b commit f90f377
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/frontend/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ var vcsHostsWithThreeElementRepoName = map[string]bool{
"gitee.com": true,
"github.com": true,
"gitlab.com": true,
"golang.org": true,
}

// maxPathsToFetch is the number of modulePaths that are fetched from a single
Expand All @@ -461,6 +460,12 @@ var maxPathsToFetch = 10
// candidateModulePaths returns the potential module paths that could contain
// the fullPath. The paths are returned in reverse length order.
func candidateModulePaths(fullPath string) (_ []string, err error) {
if !isValidPath(fullPath) {
return nil, &serverError{
status: http.StatusBadRequest,
err: fmt.Errorf("isValidPath(%q): false", fullPath),
}
}
var (
path string
modulePaths []string
Expand Down
9 changes: 9 additions & 0 deletions internal/frontend/urlinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ func isValidPath(fullPath string) bool {
return false
}
parts := strings.Split(fullPath, "/")
if parts[0] == "golang.org" {
if fullPath == "golang.org/dl" {
return true
}
if len(parts) >= 3 && parts[1] == "x" {
return true
}
return false
}
if _, ok := vcsHostsWithThreeElementRepoName[parts[0]]; ok {
if len(parts) < 3 {
return false
Expand Down
25 changes: 25 additions & 0 deletions internal/frontend/urlinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ func TestNewContextFromExps(t *testing.T) {
}
}

func TestIsValidPath(t *testing.T) {
tests := []struct {
path string
want bool
}{
{"net/http", true},
{"github.com/foo", false},
{"github.com/foo", false},
{"/github.com/foo/bar", false},
{"github.com/foo/bar/", false},
{"github.com/foo/bar", true},
{"github.com/foo/bar/baz", true},
{"golang.org/dl", true},
{"golang.org/x", false},
{"golang.org/x/tools", true},
{"golang.org/x/tools/go/packages", true},
}
for _, test := range tests {
got := isValidPath(test.path)
if got != test.want {
t.Errorf("isValidPath(ctx, ds, %q) = %t, want %t", test.path, got, test.want)
}
}
}

func TestIsSupportedVersion(t *testing.T) {
tests := []struct {
path, version string
Expand Down

0 comments on commit f90f377

Please sign in to comment.