| // Copyright 2019 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package frontend |
| |
| import ( |
| "context" |
| "net/http" |
| "strings" |
| |
| "golang.org/x/pkgsite/internal/derrors" |
| "golang.org/x/pkgsite/internal/postgres" |
| "golang.org/x/pkgsite/internal/stdlib" |
| ) |
| |
| // handlePackageDetailsRedirect redirects all redirects to "/pkg" to "/". |
| func (s *Server) handlePackageDetailsRedirect(w http.ResponseWriter, r *http.Request) { |
| urlPath := strings.TrimPrefix(r.URL.Path, "/pkg") |
| http.Redirect(w, r, urlPath, http.StatusMovedPermanently) |
| } |
| |
| // handleModuleDetailsRedirect redirects all redirects to "/mod" to "/". |
| func (s *Server) handleModuleDetailsRedirect(w http.ResponseWriter, r *http.Request) { |
| urlPath := strings.TrimPrefix(r.URL.Path, "/mod") |
| http.Redirect(w, r, urlPath, http.StatusMovedPermanently) |
| } |
| |
| // stdlibPathForShortcut returns a path in the stdlib that shortcut should redirect to, |
| // or the empty string if there is no such path. |
| func stdlibPathForShortcut(ctx context.Context, db *postgres.DB, shortcut string) (path string, err error) { |
| defer derrors.Wrap(&err, "stdlibPathForShortcut(ctx, %q)", shortcut) |
| if !stdlib.Contains(shortcut) { |
| return "", nil |
| } |
| matches, err := db.GetStdlibPathsWithSuffix(ctx, shortcut) |
| if err != nil { |
| return "", err |
| } |
| if len(matches) == 1 { |
| return matches[0], nil |
| } |
| // No matches, or ambiguous. |
| return "", nil |
| } |