diff --git a/godoc/README.md b/godoc/README.md index 1c495c8d64..52bc8a4ef3 100644 --- a/godoc/README.md +++ b/godoc/README.md @@ -10,7 +10,7 @@ binary. It can be tedious to recompile assets every time, but you can pass a flag to load CSS/JS/templates from disk every time a page loads: ``` -godoc --templates=$GOPATH/src/golang.org/x/tools/godoc/static --http=:6060 +godoc -templates=$GOPATH/src/golang.org/x/tools/godoc/static -http=:6060 ``` ## Recompiling static assets diff --git a/godoc/godoc.go b/godoc/godoc.go index a9e8b3bb4b..5c6d98207b 100644 --- a/godoc/godoc.go +++ b/godoc/godoc.go @@ -421,9 +421,9 @@ 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 + Dirname string // directory containing the package + Err error // error or nil + GoogleCN bool // page is being served from golang.google.cn Mode PageInfoMode // display metadata from query string @@ -683,8 +683,8 @@ func (p *Presentation) example_htmlFunc(info *PageInfo, funcName string) string err := p.ExampleHTML.Execute(&buf, struct { Name, Doc, Code, Play, Output string - Share bool - }{eg.Name, eg.Doc, code, play, out, info.Share}) + GoogleCN bool + }{eg.Name, eg.Doc, code, play, out, info.GoogleCN}) if err != nil { log.Print(err) } diff --git a/godoc/page.go b/godoc/page.go index 0c7bf00ade..10e86e5c8f 100644 --- a/godoc/page.go +++ b/godoc/page.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "runtime" + "strings" ) // Page describes the contents of the top-level godoc webpage. @@ -19,7 +20,7 @@ type Page struct { SrcPath string Query string Body []byte - Share bool + GoogleCN bool // page is being served from golang.google.cn // filled in by servePage SearchBox bool @@ -51,19 +52,25 @@ func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpat Title: "File " + relpath, Subtitle: relpath, Body: applyTemplate(p.ErrorHTML, "errorHTML", err), - Share: allowShare(r), + GoogleCN: googleCN(r), }) } -var onAppengine = false // overriden in appengine.go when on app engine +var onAppengine = false // overridden in appengine.go when on app engine -func allowShare(r *http.Request) bool { +func googleCN(r *http.Request) bool { + if r.FormValue("googlecn") != "" { + return true + } if !onAppengine { + return false + } + if strings.HasSuffix(r.Host, ".cn") { return true } switch r.Header.Get("X-AppEngine-Country") { case "", "ZZ", "CN": - return false + return true } - return true + return false } diff --git a/godoc/proxy/proxy.go b/godoc/proxy/proxy.go index e8cb18eb33..0ddaa008cd 100644 --- a/godoc/proxy/proxy.go +++ b/godoc/proxy/proxy.go @@ -19,6 +19,7 @@ import ( "net/http" "net/http/httputil" "net/url" + "strings" "time" "golang.org/x/net/context" @@ -147,8 +148,8 @@ func cacheKey(body string) string { } func share(w http.ResponseWriter, r *http.Request) { - if !allowShare(r) { - http.Error(w, "Forbidden", http.StatusForbidden) + if googleCN(r) { + http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) return } target, _ := url.Parse(playgroundURL) @@ -157,13 +158,19 @@ func share(w http.ResponseWriter, r *http.Request) { p.ServeHTTP(w, r) } -func allowShare(r *http.Request) bool { +func googleCN(r *http.Request) bool { + if r.FormValue("googlecn") != "" { + return true + } if appengine.IsDevAppServer() { + return false + } + if strings.HasSuffix(r.Host, ".cn") { return true } switch r.Header.Get("X-AppEngine-Country") { case "", "ZZ", "CN": - return false + return true } - return true + return false } diff --git a/godoc/search.go b/godoc/search.go index 63d9e76d3b..d61193f218 100644 --- a/godoc/search.go +++ b/godoc/search.go @@ -126,7 +126,7 @@ func (p *Presentation) HandleSearch(w http.ResponseWriter, r *http.Request) { Tabtitle: query, Query: query, Body: body.Bytes(), - Share: allowShare(r), + GoogleCN: googleCN(r), }) } diff --git a/godoc/server.go b/godoc/server.go index c9b4056658..3b452e5eda 100644 --- a/godoc/server.go +++ b/godoc/server.go @@ -312,13 +312,13 @@ func (h *handlerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { info.TypeInfoIndex[ti.Name] = i } - info.Share = allowShare(r) + info.GoogleCN = googleCN(r) h.p.ServePage(w, Page{ Title: title, Tabtitle: tabtitle, Subtitle: subtitle, Body: applyTemplate(h.p.PackageHTML, "packageHTML", info), - Share: info.Share, + GoogleCN: info.GoogleCN, }) } @@ -583,7 +583,7 @@ func (p *Presentation) serveTextFile(w http.ResponseWriter, r *http.Request, abs SrcPath: relpath, Tabtitle: relpath, Body: buf.Bytes(), - Share: allowShare(r), + GoogleCN: googleCN(r), }) } @@ -654,7 +654,7 @@ func (p *Presentation) serveDirectory(w http.ResponseWriter, r *http.Request, ab SrcPath: relpath, Tabtitle: relpath, Body: applyTemplate(p.DirlistHTML, "dirlistHTML", list), - Share: allowShare(r), + GoogleCN: googleCN(r), }) } @@ -683,7 +683,7 @@ func (p *Presentation) ServeHTMLDoc(w http.ResponseWriter, r *http.Request, absp page := Page{ Title: meta.Title, Subtitle: meta.Subtitle, - Share: allowShare(r), + GoogleCN: googleCN(r), } // evaluate as template if indicated diff --git a/godoc/static/example.html b/godoc/static/example.html index 3bc0eb9b6b..1e86b25884 100644 --- a/godoc/static/example.html +++ b/godoc/static/example.html @@ -13,7 +13,7 @@
Run Format - {{if $.Share}} + {{if not $.GoogleCN}} Share {{end}}
diff --git a/godoc/static/godoc.html b/godoc/static/godoc.html index 92b10aa754..b7f6c11b9e 100644 --- a/godoc/static/godoc.html +++ b/godoc/static/godoc.html @@ -55,7 +55,7 @@ func main() {
Run Format - {{if $.Share}} + {{if not $.GoogleCN}} Share {{end}}
@@ -95,7 +95,7 @@ Except as not the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code is licensed under a BSD license.
-Terms of Service | +Terms of Service | Privacy Policy