[release-branch.go1.11] godoc/dl: serve "go get" meta for golang.org/dl/gotip

Change-Id: I05db10e9b3f4983d23af4dc5fd4cce9b3979e9c5
Reviewed-on: https://go-review.googlesource.com/c/153097
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
(cherry picked from commit 4d6773f6fa)
Reviewed-on: https://go-review.googlesource.com/c/153862
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Filippo Valsorda 2018-12-06 20:45:49 -05:00
parent 147f5680bc
commit 9e8c73a9cf
1 changed files with 29 additions and 30 deletions

View File

@ -137,9 +137,10 @@ func (h server) uploadHandler(w http.ResponseWriter, r *http.Request) {
} }
func (h server) getHandler(w http.ResponseWriter, r *http.Request) { func (h server) getHandler(w http.ResponseWriter, r *http.Request) {
// For go get golang.org/dl/go1.x.y, we need to serve the isGoGet := (r.Method == "GET" || r.Method == "HEAD") && r.FormValue("go-get") == "1"
// same meta tags at /dl for cmd/go to validate against /dl/go1.x.y: // For go get, we need to serve the same meta tags at /dl for cmd/go to
if r.URL.Path == "/dl" && (r.Method == "GET" || r.Method == "HEAD") && r.FormValue("go-get") == "1" { // validate against the import path.
if r.URL.Path == "/dl" && isGoGet {
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, `<!DOCTYPE html><html><head> fmt.Fprintf(w, `<!DOCTYPE html><html><head>
<meta name="go-import" content="golang.org/dl git https://go.googlesource.com/dl"> <meta name="go-import" content="golang.org/dl git https://go.googlesource.com/dl">
@ -152,39 +153,37 @@ func (h server) getHandler(w http.ResponseWriter, r *http.Request) {
} }
name := strings.TrimPrefix(r.URL.Path, "/dl/") name := strings.TrimPrefix(r.URL.Path, "/dl/")
if name == "" { var redirectURL string
switch {
case name == "":
h.listHandler(w, r) h.listHandler(w, r)
return return
} case fileRe.MatchString(name):
if fileRe.MatchString(name) {
http.Redirect(w, r, downloadBaseURL+name, http.StatusFound) http.Redirect(w, r, downloadBaseURL+name, http.StatusFound)
return return
case name == "gotip":
redirectURL = "https://godoc.org/golang.org/dl/gotip"
case goGetRe.MatchString(name):
redirectURL = "https://golang.org/dl/#" + name
default:
http.NotFound(w, r)
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") w.Header().Set("Content-Type", "text/html; charset=utf-8")
isGoGet = r.FormValue("go-get") == "1"
}
if !isGoGet { if !isGoGet {
w.Header().Set("Location", "https://golang.org/dl/#"+name) w.Header().Set("Location", redirectURL)
w.WriteHeader(http.StatusFound)
} }
fmt.Fprintf(w, `<!DOCTYPE html> fmt.Fprintf(w, `<!DOCTYPE html>
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="golang.org/dl git https://go.googlesource.com/dl"> <meta name="go-import" content="golang.org/dl git https://go.googlesource.com/dl">
<meta http-equiv="refresh" content="0; url=https://golang.org/dl/#%s"> <meta http-equiv="refresh" content="0; url=%s">
</head> </head>
<body> <body>
Nothing to see here; <a href="https://golang.org/dl/#%s">move along</a>. Nothing to see here; <a href="%s">move along</a>.
</body> </body>
</html> </html>
`, html.EscapeString(name), html.EscapeString(name)) `, html.EscapeString(redirectURL), html.EscapeString(redirectURL))
return
}
http.NotFound(w, r)
} }
func (h server) initHandler(w http.ResponseWriter, r *http.Request) { func (h server) initHandler(w http.ResponseWriter, r *http.Request) {