Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / godoc / page.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package godoc
6
7 import (
8         "net/http"
9         "os"
10         "path/filepath"
11         "runtime"
12         "strings"
13
14         "golang.org/x/tools/godoc/golangorgenv"
15 )
16
17 // Page describes the contents of the top-level godoc webpage.
18 type Page struct {
19         Title    string
20         Tabtitle string
21         Subtitle string
22         SrcPath  string
23         Query    string
24         Body     []byte
25         GoogleCN bool // page is being served from golang.google.cn
26         TreeView bool // page needs to contain treeview related js and css
27
28         // filled in by ServePage
29         SearchBox       bool
30         Playground      bool
31         Version         string
32         GoogleAnalytics string
33 }
34
35 func (p *Presentation) ServePage(w http.ResponseWriter, page Page) {
36         if page.Tabtitle == "" {
37                 page.Tabtitle = page.Title
38         }
39         page.SearchBox = p.Corpus.IndexEnabled
40         page.Playground = p.ShowPlayground
41         page.Version = runtime.Version()
42         page.GoogleAnalytics = p.GoogleAnalytics
43         applyTemplateToResponseWriter(w, p.GodocHTML, page)
44 }
45
46 func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
47         w.WriteHeader(http.StatusNotFound)
48         if perr, ok := err.(*os.PathError); ok {
49                 rel, err := filepath.Rel(runtime.GOROOT(), perr.Path)
50                 if err != nil {
51                         perr.Path = "REDACTED"
52                 } else {
53                         perr.Path = filepath.Join("$GOROOT", rel)
54                 }
55         }
56         p.ServePage(w, Page{
57                 Title:           "File " + relpath,
58                 Subtitle:        relpath,
59                 Body:            applyTemplate(p.ErrorHTML, "errorHTML", err),
60                 GoogleCN:        googleCN(r),
61                 GoogleAnalytics: p.GoogleAnalytics,
62         })
63 }
64
65 // googleCN reports whether request r is considered
66 // to be served from golang.google.cn.
67 func googleCN(r *http.Request) bool {
68         if r.FormValue("googlecn") != "" {
69                 return true
70         }
71         if strings.HasSuffix(r.Host, ".cn") {
72                 return true
73         }
74         if !golangorgenv.CheckCountry() {
75                 return false
76         }
77         switch r.Header.Get("X-Appengine-Country") {
78         case "", "ZZ", "CN":
79                 return true
80         }
81         return false
82 }