From be728107ea8275e6f58ba07e246b94181acaab24 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 20 Jul 2018 04:14:21 +0000 Subject: [PATCH] [release-branch.go1.10] godoc/dl: also serve go-import meta tags at golang.org/dl for cmd/go Updates golang/go#23223 Change-Id: Iacecbb5e095fd3d6acb3f8e1fb238db63d1e0b6d Reviewed-on: https://go-review.googlesource.com/125195 Reviewed-by: Andrew Bonventre (cherry picked from commit 99195f4d4ffa6331a9bc856c72697a15d9842950) Reviewed-on: https://go-review.googlesource.com/125176 --- godoc/dl/dl.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/godoc/dl/dl.go b/godoc/dl/dl.go index 629b6caf..56c0f622 100644 --- a/godoc/dl/dl.go +++ b/godoc/dl/dl.go @@ -41,7 +41,7 @@ const ( ) func RegisterHandlers(mux *http.ServeMux) { - mux.Handle("/dl", http.RedirectHandler("/dl/", http.StatusFound)) + mux.HandleFunc("/dl", getHandler) mux.HandleFunc("/dl/", getHandler) // also serves listHandler mux.HandleFunc("/dl/upload", uploadHandler) mux.HandleFunc("/dl/init", initHandler) @@ -428,6 +428,20 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { } func getHandler(w http.ResponseWriter, r *http.Request) { + // For go get golang.org/dl/go1.x.y, we need to serve the + // same meta tags at /dl for cmd/go to validate against /dl/go1.x.y: + if r.URL.Path == "/dl" && (r.Method == "GET" || r.Method == "HEAD") && r.FormValue("go-get") == "1" { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + fmt.Fprintf(w, ` + +`) + return + } + if r.URL.Path == "/dl" { + http.Redirect(w, r, "/dl/", http.StatusFound) + return + } + name := strings.TrimPrefix(r.URL.Path, "/dl/") if name == "" { listHandler(w, r) @@ -438,11 +452,15 @@ func getHandler(w http.ResponseWriter, r *http.Request) { return } if goGetRe.MatchString(name) { + var isGoGet bool if r.Method == "GET" || r.Method == "HEAD" { w.Header().Set("Content-Type", "text/html; charset=utf-8") + isGoGet = r.FormValue("go-get") == "1" + } + if !isGoGet { + w.Header().Set("Location", "https://golang.org/dl/#"+name) + w.WriteHeader(http.StatusFound) } - w.Header().Set("Location", "https://golang.org/dl/#"+name) - w.WriteHeader(http.StatusFound) fmt.Fprintf(w, `