gddo-server: redirect invalid import paths to homepage

When a URL path for a package page fails the check for
module.CheckImportPath, redirect to the homepage. Otherwise, the page
will 400 on pkg.go.dev.

Change-Id: Ic337b968a3919b2e4fcd0ab4dd6b76c631f5edf9
Reviewed-on: https://go-review.googlesource.com/c/gddo/+/279035
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Julie Qiu 2020-12-20 22:53:42 -05:00
Родитель 5b05951d42
Коммит 17b648fae2
2 изменённых файлов: 14 добавлений и 5 удалений

Просмотреть файл

@ -16,6 +16,7 @@ import (
"strings"
"time"
"golang.org/x/mod/module"
"golang.org/x/net/context/ctxhttp"
)
@ -247,16 +248,20 @@ func pkgGoDevURL(godocURL *url.URL) *url.URL {
} else {
u.Path = "/"
}
case "":
u.Path = ""
case "/-/subrepo":
u.Path = "/search"
q.Set("q", "golang.org/x")
default:
{
_, isSVG := godocURL.Query()["status.svg"]
_, isPNG := godocURL.Query()["status.png"]
if isSVG || isPNG {
u.Path = "/badge" + godocURL.Path
break
// If the import path is invalid, redirect to
// https://golang.org/issue/43036, so that the users has more context
// on why this path does not work on pkg.go.dev.
if err := module.CheckImportPath(strings.TrimPrefix(godocURL.Path, "/")); err != nil {
u.Host = "golang.org"
u.Path = "/issue/43036"
return u
}
u.Path = godocURL.Path

Просмотреть файл

@ -167,6 +167,10 @@ func TestPkgGoDevURL(t *testing.T) {
from: "https://godoc.org/golang.org/x/vgo/vendor",
to: "https://pkg.go.dev/?utm_source=godoc",
},
{
from: "https://godoc.org/cryptoscope.co/go/specialκ",
to: "https://golang.org/issue/43036",
},
}
for _, tc := range testCases {