From 1330b289ad6d59313d86910fa35d5022cd871e7f Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Wed, 2 Sep 2015 09:49:30 +1000 Subject: [PATCH] godoc: hide and block share functionality from specific countries This will permit us to serve *.golang.org to Chinese users. Change-Id: I5217753aa67931522c7e6be106477534c99a20b2 Reviewed-on: https://go-review.googlesource.com/14194 Reviewed-by: Andrew Gerrand --- godoc/appengine.go | 13 +++++++++++++ godoc/godoc.go | 4 +++- godoc/page.go | 15 +++++++++++++++ godoc/search.go | 1 + godoc/server.go | 19 +++++++++++++------ godoc/static/example.html | 2 ++ godoc/static/godoc.html | 2 ++ godoc/static/static.go | 4 ++++ 8 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 godoc/appengine.go diff --git a/godoc/appengine.go b/godoc/appengine.go new file mode 100644 index 0000000000..2a685585c7 --- /dev/null +++ b/godoc/appengine.go @@ -0,0 +1,13 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build appengine + +package godoc + +import "appengine" + +func init() { + onAppengine = !appengine.IsDevAppServer() +} diff --git a/godoc/godoc.go b/godoc/godoc.go index 6b176c6ab2..0af4ef4b4b 100644 --- a/godoc/godoc.go +++ b/godoc/godoc.go @@ -281,6 +281,7 @@ func sanitizeFunc(src string) string { type PageInfo struct { Dirname string // directory containing the package Err error // error or nil + Share bool // show share button on examples // package info FSet *token.FileSet // nil if no package documentation @@ -490,7 +491,8 @@ func (p *Presentation) example_htmlFunc(info *PageInfo, funcName string) string err := p.ExampleHTML.Execute(&buf, struct { Name, Doc, Code, Play, Output string - }{eg.Name, eg.Doc, code, play, out}) + Share bool + }{eg.Name, eg.Doc, code, play, out, info.Share}) if err != nil { log.Print(err) } diff --git a/godoc/page.go b/godoc/page.go index b296b27840..50e969009d 100644 --- a/godoc/page.go +++ b/godoc/page.go @@ -16,6 +16,7 @@ type Page struct { Subtitle string Query string Body []byte + Share bool // filled in by servePage SearchBox bool @@ -39,5 +40,19 @@ func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpat Title: "File " + relpath, Subtitle: relpath, Body: applyTemplate(p.ErrorHTML, "errorHTML", err), // err may contain an absolute path! + Share: allowShare(r), }) } + +var onAppengine = false // overriden in appengine.go when on app engine + +func allowShare(r *http.Request) bool { + if !onAppengine { + return true + } + switch r.Header.Get("X-AppEngine-Country") { + case "", "ZZ", "HK", "CN", "RC": + return false + } + return true +} diff --git a/godoc/search.go b/godoc/search.go index e12633065e..ff3f2bebbd 100644 --- a/godoc/search.go +++ b/godoc/search.go @@ -126,6 +126,7 @@ func (p *Presentation) HandleSearch(w http.ResponseWriter, r *http.Request) { Tabtitle: query, Query: query, Body: body.Bytes(), + Share: allowShare(r), }) } diff --git a/godoc/server.go b/godoc/server.go index 2c18efbbeb..bbc3409c63 100644 --- a/godoc/server.go +++ b/godoc/server.go @@ -300,11 +300,13 @@ func (h *handlerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { info.TypeInfoIndex[ti.Name] = i } + info.Share = allowShare(r) h.p.ServePage(w, Page{ Title: title, Tabtitle: tabtitle, Subtitle: subtitle, Body: applyTemplate(h.p.PackageHTML, "packageHTML", info), + Share: info.Share, }) } @@ -546,6 +548,7 @@ func (p *Presentation) serveTextFile(w http.ResponseWriter, r *http.Request, abs Title: title + " " + relpath, Tabtitle: relpath, Body: buf.Bytes(), + Share: allowShare(r), }) } @@ -606,6 +609,7 @@ func (p *Presentation) serveDirectory(w http.ResponseWriter, r *http.Request, ab Title: "Directory " + relpath, Tabtitle: relpath, Body: applyTemplate(p.DirlistHTML, "dirlistHTML", list), + Share: allowShare(r), }) } @@ -631,6 +635,12 @@ func (p *Presentation) ServeHTMLDoc(w http.ResponseWriter, r *http.Request, absp log.Printf("decoding metadata %s: %v", relpath, err) } + page := Page{ + Title: meta.Title, + Subtitle: meta.Subtitle, + Share: allowShare(r), + } + // evaluate as template if indicated if meta.Template { tmpl, err := template.New("main").Funcs(p.TemplateFuncs()).Parse(string(src)) @@ -640,7 +650,7 @@ func (p *Presentation) ServeHTMLDoc(w http.ResponseWriter, r *http.Request, absp return } var buf bytes.Buffer - if err := tmpl.Execute(&buf, nil); err != nil { + if err := tmpl.Execute(&buf, page); err != nil { log.Printf("executing template %s: %v", relpath, err) p.ServeError(w, r, relpath, err) return @@ -655,11 +665,8 @@ func (p *Presentation) ServeHTMLDoc(w http.ResponseWriter, r *http.Request, absp src = buf.Bytes() } - p.ServePage(w, Page{ - Title: meta.Title, - Subtitle: meta.Subtitle, - Body: src, - }) + page.Body = src + p.ServePage(w, page) } func (p *Presentation) ServeFile(w http.ResponseWriter, r *http.Request) { diff --git a/godoc/static/example.html b/godoc/static/example.html index cda2a8491e..4f4e09e871 100644 --- a/godoc/static/example.html +++ b/godoc/static/example.html @@ -13,7 +13,9 @@
Run Format + {{if $.Share}} + {{end}}
{{else}} diff --git a/godoc/static/godoc.html b/godoc/static/godoc.html index 6d6d1b69d4..4605057667 100644 --- a/godoc/static/godoc.html +++ b/godoc/static/godoc.html @@ -55,7 +55,9 @@ func main() {
Run Format + {{if $.Share}} + {{end}}
{{end}} diff --git a/godoc/static/static.go b/godoc/static/static.go index a75c2c6fad..9c94e713cd 100644 --- a/godoc/static/static.go +++ b/godoc/static/static.go @@ -441,7 +441,9 @@ var Files = map[string]string{
Run Format + {{if $.Share}} + {{end}}
{{else}} @@ -513,7 +515,9 @@ func main() {
Run Format + {{if $.Share}} + {{end}}
{{end}}