diff options
| author | Filippo Valsorda <hi@filippo.io> | 2021-09-15 00:55:16 +0200 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2021-09-15 15:56:49 +0000 |
| commit | 03c6600d8162b9aacba0d3e4e89839313304c709 (patch) | |
| tree | cee7ceb27aa2e93838d83b72d8c1bb8d6c48667f | |
| parent | e69fbd757fd272b39dddc79e2ba3cd026b6b7cdd (diff) | |
| download | go-x-website-03c6600d8162b9aacba0d3e4e89839313304c709.tar.xz | |
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>
| -rw-r--r-- | cmd/golangorg/README.md | 2 | ||||
| -rw-r--r-- | cmd/golangorg/server.go | 7 | ||||
| -rw-r--r-- | go.dev/README.md | 8 |
3 files changed, 10 insertions, 7 deletions
diff --git a/cmd/golangorg/README.md b/cmd/golangorg/README.md index e62db90b..96d81430 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 1f299564..8aa55b9e 100644 --- a/cmd/golangorg/server.go +++ b/cmd/golangorg/server.go @@ -19,6 +19,7 @@ import ( "io/ioutil" "log" "net/http" + "net/url" "os" "path" "path/filepath" @@ -417,8 +418,8 @@ func hostEnforcerHandler(h http.Handler) http.Handler { // 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 @@ func (r *linkRewriter) WriteHeader(code int) { 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 5368f412..220549c0 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 @@ Otherwise, the new deployment can be found in the 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` |
