Skip to content

Commit

Permalink
internal/frontend: fix redirects for shortcuts with trailing slashes
Browse files Browse the repository at this point in the history
Opening /http/ on pkg.go.dev is considered a shortcut to
pkg.go.dev/net/http and should redirect there. However, http.Redirect()
creates a URL relative to the current URL (/http/) and redirects to
pkg.go.dev/http/net/http instead.

In this CL, relative redirect paths are replaced by absolute paths so
that /http/ redirects to /net/http.

Fixes golang/go#40166.

Change-Id: Ic3b9e6da59bfa2358b7f75e565b8a52662f17eb8
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/242138
Reviewed-by: Julie Qiu <julie@golang.org>
  • Loading branch information
akolar authored and julieqiu committed Jul 14, 2020
1 parent 47787dc commit 4646ba4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/frontend/details.go
Expand Up @@ -410,7 +410,7 @@ func parseStdLibURLPath(urlPath string) (path, requestedVersion string, err erro
if len(parts) == 1 {
return path, internal.LatestVersion, nil
}
requestedVersion = stdlib.VersionForTag(parts[1])
requestedVersion = stdlib.VersionForTag(strings.TrimSuffix(parts[1], "/"))
if requestedVersion == "" {
return "", "", fmt.Errorf("invalid Go tag for url: %q", urlPath)
}
Expand All @@ -428,7 +428,7 @@ func (s *Server) servePathNotFoundPage(w http.ResponseWriter, r *http.Request, f
log.Error(ctx, err)
}
if path != "" {
http.Redirect(w, r, path, http.StatusFound)
http.Redirect(w, r, fmt.Sprintf("/%s", path), http.StatusFound)
return
}

Expand Down
12 changes: 12 additions & 0 deletions internal/frontend/server_test.go
Expand Up @@ -851,6 +851,18 @@ func serverTestCases() ([]serverTestCase, []serverTestCase, []serverTestCase, []
wantStatusCode: http.StatusFound,
wantLocation: "/net/http",
},
{
name: "stdlib shortcut with trailing slash",
urlPath: "/http/",
wantStatusCode: http.StatusFound,
wantLocation: "/net/http",
},
{
name: "stdlib shortcut with args and trailing slash",
urlPath: "/http@go1.13/?tab=doc",
wantStatusCode: http.StatusFound,
wantLocation: "/net/http",
},
}
return testCases, noExpUserDirTCs, frontendFetchTCs, withPathTCs
}
Expand Down

0 comments on commit 4646ba4

Please sign in to comment.