aboutsummaryrefslogtreecommitdiff
path: root/cmd/golangorg/server.go
diff options
context:
space:
mode:
authorFilippo Valsorda <hi@filippo.io>2021-09-15 00:55:16 +0200
committerRuss Cox <rsc@golang.org>2021-09-15 15:56:49 +0000
commit03c6600d8162b9aacba0d3e4e89839313304c709 (patch)
treecee7ceb27aa2e93838d83b72d8c1bb8d6c48667f /cmd/golangorg/server.go
parente69fbd757fd272b39dddc79e2ba3cd026b6b7cdd (diff)
downloadgo-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>
Diffstat (limited to 'cmd/golangorg/server.go')
-rw-r--r--cmd/golangorg/server.go7
1 files changed, 5 insertions, 2 deletions
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)
}