From bd17c084dfc55e91b0bb50cc3d33bd5e31e498c9 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Tue, 5 Mar 2019 16:56:02 -0500 Subject: [PATCH] godoc/env: replace with golangorgenv This change replaces the env package with a new golangorgenv package. The previous env package existed primarily to configure the godoc instance that was running golang.org. By now, the golang.org website has been factored out to x/website, which has its own env package, but ends up still using this env package indirectly via x/tools/godoc. The goal of this change is to make env available for other services that run on subdomains of golang.org, so they can continue to safely rely on the x/tools/playground, which will be modified in the next commit to also use the new golangorgenv. The golangorgenv package replaces the IsProd function with a more specific one. Start using it in packages x/tools/{,cmd}/godoc. Also, re-arrange the order of checks to give the host suffix check higher priority than the environment variable check. This way, if the environment variable isn't set, the host suffix check gets a chance to run. When getting the value of "X-AppEngine-Country" header, use its canonical format "X-Appengine-Country" to avoid an allocation. This does not change behavior. Updates golang/go#30486 Change-Id: I97b47211a45ca0351f31fcb4fa6d408a4b0c4c7c Reviewed-on: https://go-review.googlesource.com/c/tools/+/165459 Reviewed-by: Brad Fitzpatrick --- cmd/godoc/handlers.go | 4 ++-- .../env.go => golangorgenv/golangorgenv.go} | 19 ++++++++++--------- godoc/page.go | 12 +++++++----- 3 files changed, 19 insertions(+), 16 deletions(-) rename godoc/{env/env.go => golangorgenv/golangorgenv.go} (51%) diff --git a/cmd/godoc/handlers.go b/cmd/godoc/handlers.go index d393992eb9..2e98501898 100644 --- a/cmd/godoc/handlers.go +++ b/cmd/godoc/handlers.go @@ -21,7 +21,7 @@ import ( "text/template" "golang.org/x/tools/godoc" - "golang.org/x/tools/godoc/env" + "golang.org/x/tools/godoc/golangorgenv" "golang.org/x/tools/godoc/redirect" "golang.org/x/tools/godoc/vfs" ) @@ -40,7 +40,7 @@ type hostEnforcerHandler struct { } func (h hostEnforcerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if !env.EnforceHosts() { + if !golangorgenv.EnforceHosts() { h.h.ServeHTTP(w, r) return } diff --git a/godoc/env/env.go b/godoc/golangorgenv/golangorgenv.go similarity index 51% rename from godoc/env/env.go rename to godoc/golangorgenv/golangorgenv.go index e1f55cd347..0b96eec0d5 100644 --- a/godoc/env/env.go +++ b/godoc/golangorgenv/golangorgenv.go @@ -2,9 +2,9 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Package env provides environment information for the godoc server running on -// golang.org. -package env +// Package golangorgenv provides environment information for programs running at +// golang.org and its subdomains. +package golangorgenv import ( "log" @@ -13,14 +13,13 @@ import ( ) var ( - isProd = boolEnv("GODOC_PROD") - enforceHosts = boolEnv("GODOC_ENFORCE_HOSTS") + checkCountry = boolEnv("GOLANGORG_CHECK_COUNTRY") + enforceHosts = boolEnv("GOLANGORG_ENFORCE_HOSTS") ) -// IsProd reports whether the server is running in its production configuration -// on golang.org. -func IsProd() bool { - return isProd +// CheckCountry reports whether country restrictions should be enforced. +func CheckCountry() bool { + return checkCountry } // EnforceHosts reports whether host filtering should be enforced. @@ -31,6 +30,8 @@ func EnforceHosts() bool { func boolEnv(key string) bool { v := os.Getenv(key) if v == "" { + // TODO(dmitshur): In the future, consider detecting if running in App Engine, + // and if so, making the environment variables mandatory rather than optional. return false } b, err := strconv.ParseBool(v) diff --git a/godoc/page.go b/godoc/page.go index ae1eaa5b12..daf4dc98ab 100644 --- a/godoc/page.go +++ b/godoc/page.go @@ -11,7 +11,7 @@ import ( "runtime" "strings" - "golang.org/x/tools/godoc/env" + "golang.org/x/tools/godoc/golangorgenv" ) // Page describes the contents of the top-level godoc webpage. @@ -62,17 +62,19 @@ func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpat }) } +// googleCN reports whether request r is considered +// to be served from golang.google.cn. func googleCN(r *http.Request) bool { if r.FormValue("googlecn") != "" { return true } - if !env.IsProd() { - return false - } if strings.HasSuffix(r.Host, ".cn") { return true } - switch r.Header.Get("X-AppEngine-Country") { + if !golangorgenv.CheckCountry() { + return false + } + switch r.Header.Get("X-Appengine-Country") { case "", "ZZ", "CN": return true }