2013-07-17 01:17:12 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
|
|
|
package godoc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-09-20 17:58:29 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-07-17 01:17:12 -06:00
|
|
|
"runtime"
|
2017-08-02 14:37:59 -06:00
|
|
|
"strings"
|
2018-09-04 10:55:45 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/godoc/env"
|
2013-07-17 01:17:12 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Page describes the contents of the top-level godoc webpage.
|
|
|
|
type Page struct {
|
|
|
|
Title string
|
|
|
|
Tabtitle string
|
|
|
|
Subtitle string
|
2016-09-16 06:42:50 -06:00
|
|
|
SrcPath string
|
2013-07-17 01:17:12 -06:00
|
|
|
Query string
|
|
|
|
Body []byte
|
2017-08-02 14:37:59 -06:00
|
|
|
GoogleCN bool // page is being served from golang.google.cn
|
2018-05-11 23:13:07 -06:00
|
|
|
TreeView bool // page needs to contain treeview related js and css
|
2013-07-17 01:17:12 -06:00
|
|
|
|
2018-09-04 10:55:45 -06:00
|
|
|
// filled in by ServePage
|
|
|
|
SearchBox bool
|
|
|
|
Playground bool
|
|
|
|
Version string
|
|
|
|
GoogleAnalytics string
|
2013-07-17 01:17:12 -06:00
|
|
|
}
|
|
|
|
|
2013-07-17 17:52:45 -06:00
|
|
|
func (p *Presentation) ServePage(w http.ResponseWriter, page Page) {
|
2013-07-17 01:17:12 -06:00
|
|
|
if page.Tabtitle == "" {
|
|
|
|
page.Tabtitle = page.Title
|
|
|
|
}
|
2013-07-17 17:52:45 -06:00
|
|
|
page.SearchBox = p.Corpus.IndexEnabled
|
2013-07-17 21:14:09 -06:00
|
|
|
page.Playground = p.ShowPlayground
|
2013-07-17 01:17:12 -06:00
|
|
|
page.Version = runtime.Version()
|
2018-09-04 10:55:45 -06:00
|
|
|
page.GoogleAnalytics = p.GoogleAnalytics
|
2014-01-29 08:53:45 -07:00
|
|
|
applyTemplateToResponseWriter(w, p.GodocHTML, page)
|
2013-07-17 01:17:12 -06:00
|
|
|
}
|
|
|
|
|
2013-07-17 17:52:45 -06:00
|
|
|
func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
|
2013-07-17 01:17:12 -06:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2016-09-20 17:58:29 -06:00
|
|
|
if perr, ok := err.(*os.PathError); ok {
|
|
|
|
rel, err := filepath.Rel(runtime.GOROOT(), perr.Path)
|
|
|
|
if err != nil {
|
|
|
|
perr.Path = "REDACTED"
|
|
|
|
} else {
|
|
|
|
perr.Path = filepath.Join("$GOROOT", rel)
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 17:52:45 -06:00
|
|
|
p.ServePage(w, Page{
|
2018-09-04 10:55:45 -06:00
|
|
|
Title: "File " + relpath,
|
|
|
|
Subtitle: relpath,
|
|
|
|
Body: applyTemplate(p.ErrorHTML, "errorHTML", err),
|
|
|
|
GoogleCN: googleCN(r),
|
|
|
|
GoogleAnalytics: p.GoogleAnalytics,
|
2013-07-17 01:17:12 -06:00
|
|
|
})
|
|
|
|
}
|
2015-09-01 17:49:30 -06:00
|
|
|
|
2017-08-02 14:37:59 -06:00
|
|
|
func googleCN(r *http.Request) bool {
|
|
|
|
if r.FormValue("googlecn") != "" {
|
|
|
|
return true
|
|
|
|
}
|
2018-09-04 10:55:45 -06:00
|
|
|
if !env.IsProd() {
|
2017-08-02 14:37:59 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(r.Host, ".cn") {
|
2015-09-01 17:49:30 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch r.Header.Get("X-AppEngine-Country") {
|
2015-09-28 01:00:17 -06:00
|
|
|
case "", "ZZ", "CN":
|
2017-08-02 14:37:59 -06:00
|
|
|
return true
|
2015-09-01 17:49:30 -06:00
|
|
|
}
|
2017-08-02 14:37:59 -06:00
|
|
|
return false
|
2015-09-01 17:49:30 -06:00
|
|
|
}
|