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>
diff --git a/gddo-server/pkgsite.go b/gddo-server/pkgsite.go
index c5e0aa3..5fa7ef0 100644
--- a/gddo-server/pkgsite.go
+++ b/gddo-server/pkgsite.go
@@ -16,6 +16,7 @@
"strings"
"time"
+ "golang.org/x/mod/module"
"golang.org/x/net/context/ctxhttp"
)
@@ -247,16 +248,20 @@
} 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
diff --git a/gddo-server/pkgsite_test.go b/gddo-server/pkgsite_test.go
index 736d178..ac2808a 100644
--- a/gddo-server/pkgsite_test.go
+++ b/gddo-server/pkgsite_test.go
@@ -167,6 +167,10 @@
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 {