internal/frontend: do not redirect github.com/golang/* requests
Requests to github.com/golang/* are no longer redirected, since there
are modules where the canonical module path has that prefix.
Only requests to stdlib packages are redirected.
Change-Id: I97a34fc25cccecfdb2957eb5759f818617c552ff
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/283912
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/internal/frontend/details.go b/internal/frontend/details.go
index 7d314fc..b67b20e 100644
--- a/internal/frontend/details.go
+++ b/internal/frontend/details.go
@@ -7,6 +7,7 @@
import (
"context"
"net/http"
+ "strings"
"github.com/google/safehtml/template"
"go.opencensus.io/stats"
@@ -31,6 +32,15 @@
s.staticPageHandler("index.tmpl", "Home")(w, r)
return nil
}
+ if strings.HasPrefix(r.URL.Path, "/github.com/golang/go") {
+ urlPath := strings.TrimPrefix(strings.TrimPrefix(r.URL.Path, "/github.com/golang/go"), "/src")
+ if urlPath == "" {
+ http.Redirect(w, r, "/std", http.StatusMovedPermanently)
+ return
+ }
+ http.Redirect(w, r, urlPath, http.StatusMovedPermanently)
+ return
+ }
ctx := r.Context()
// If page statistics are enabled, use the "exp" query param to adjust
diff --git a/internal/frontend/server.go b/internal/frontend/server.go
index eace0ac..f841446 100644
--- a/internal/frontend/server.go
+++ b/internal/frontend/server.go
@@ -137,19 +137,6 @@
handle("/license-policy", s.licensePolicyHandler())
handle("/about", http.RedirectHandler("https://go.dev/about", http.StatusFound))
handle("/badge/", http.HandlerFunc(s.badgeHandler))
- handle("/github.com/golang/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- suffix := strings.TrimPrefix(r.URL.Path, "/github.com/golang")
- if !strings.HasPrefix(r.URL.Path, "/github.com/golang/go") {
- http.Redirect(w, r, "/golang.org/x"+suffix, http.StatusMovedPermanently)
- return
- }
- urlPath := strings.TrimPrefix(strings.TrimPrefix(suffix, "/go"), "/src")
- if urlPath == "" {
- http.Redirect(w, r, "/std", http.StatusMovedPermanently)
- return
- }
- http.Redirect(w, r, urlPath, http.StatusMovedPermanently)
- }))
handle("/C", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Package "C" is a special case: redirect to /cmd/cgo.
// (This is what golang.org/C does.)
diff --git a/internal/frontend/server_test.go b/internal/frontend/server_test.go
index 8aa8306..477e361 100644
--- a/internal/frontend/server_test.go
+++ b/internal/frontend/server_test.go
@@ -653,22 +653,14 @@
wantLocation: "/time",
},
{
- name: "github golang x tools repo",
+ name: "github golang x tools repo 404 instead of redirect",
urlPath: "/github.com/golang/tools",
- wantStatusCode: http.StatusMovedPermanently,
- wantLocation: "/golang.org/x/tools",
+ wantStatusCode: http.StatusNotFound,
},
{
- name: "github golang x tools go packages",
+ name: "github golang x tools go packages 404 instead of redirect",
urlPath: "/github.com/golang/tools/go/packages",
- wantStatusCode: http.StatusMovedPermanently,
- wantLocation: "/golang.org/x/tools/go/packages",
- },
- {
- name: "github golang x tools gopls",
- urlPath: "/github.com/golang/tools/gopls",
- wantStatusCode: http.StatusMovedPermanently,
- wantLocation: "/golang.org/x/tools/gopls",
+ wantStatusCode: http.StatusNotFound,
},
{
name: "static",