cmd/golangorg: improve go.dev local server redirect handling and docs
Previously, following a cross-domain redirect would not get caught by
the local server link rewriter.
Change-Id: I2ba6c1b9a8c03cf155928be0c68eced0de6a5c42
Reviewed-on: https://go-review.googlesource.com/c/website/+/349851
Trust: Filippo Valsorda <filippo@golang.org>
Run-TryBot: Filippo Valsorda <filippo@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
diff --git a/cmd/golangorg/README.md b/cmd/golangorg/README.md
index e62db90..96d8143 100644
--- a/cmd/golangorg/README.md
+++ b/cmd/golangorg/README.md
@@ -3,6 +3,8 @@
## Local Development
For local development, simply build and run. It serves on localhost:6060.
+You can specify the domain name as the first path element, such as
+http://localhost:6060/go.dev/blog.
go run .
diff --git a/cmd/golangorg/server.go b/cmd/golangorg/server.go
index 1f29956..8aa55b9 100644
--- a/cmd/golangorg/server.go
+++ b/cmd/golangorg/server.go
@@ -19,6 +19,7 @@
"io/ioutil"
"log"
"net/http"
+ "net/url"
"os"
"path"
"path/filepath"
@@ -417,8 +418,8 @@
// hostPathHandler infers the host from the first element of the URL path
// when the actual host is a testing domain (localhost or *.appspot.com).
-// It also rewrites the output HTML in that case to link back to URLs on
-// the test site.
+// It also rewrites the output HTML and Location headers in that case to
+// link back to URLs on the test site.
func hostPathHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Host != "localhost" && !strings.HasPrefix(r.Host, "localhost:") && !strings.HasSuffix(r.Host, ".appspot.com") {
@@ -464,6 +465,8 @@
loc := r.Header().Get("Location")
if strings.HasPrefix(loc, "/") {
r.Header().Set("Location", "/"+r.host+loc)
+ } else if u, _ := url.Parse(loc); u != nil && validHosts[u.Host] {
+ r.Header().Set("Location", "/"+u.Host+"/"+u.Path+u.RawQuery)
}
r.ResponseWriter.WriteHeader(code)
}
diff --git a/go.dev/README.md b/go.dev/README.md
index 5368f41..220549c 100644
--- a/go.dev/README.md
+++ b/go.dev/README.md
@@ -9,7 +9,9 @@
To serve the go.dev pages, run
- go run ./cmd/frontend
+ go run ./cmd/golangorg
+
+and load http://localhost:6060/go.dev/
## Deploying
@@ -22,7 +24,3 @@
If the automatic deployment is not working, or to check on the status of a pending deployment,
see the “website-redeploy-go-dev” trigger in the
[Cloud Build console](https://console.cloud.google.com/cloud-build/builds?project=go-discovery).
-
-## Commands
-
-- Running the server: `go run ./cmd/frontend`