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 }