You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.7 KiB

  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. package godoc
  5. import (
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. )
  12. // Page describes the contents of the top-level godoc webpage.
  13. type Page struct {
  14. Title string
  15. Tabtitle string
  16. Subtitle string
  17. SrcPath string
  18. Query string
  19. Body []byte
  20. GoogleCN bool // page is being served from golang.google.cn
  21. // filled in by servePage
  22. SearchBox bool
  23. Playground bool
  24. Version string
  25. }
  26. func (p *Presentation) ServePage(w http.ResponseWriter, page Page) {
  27. if page.Tabtitle == "" {
  28. page.Tabtitle = page.Title
  29. }
  30. page.SearchBox = p.Corpus.IndexEnabled
  31. page.Playground = p.ShowPlayground
  32. page.Version = runtime.Version()
  33. applyTemplateToResponseWriter(w, p.GodocHTML, page)
  34. }
  35. func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
  36. w.WriteHeader(http.StatusNotFound)
  37. if perr, ok := err.(*os.PathError); ok {
  38. rel, err := filepath.Rel(runtime.GOROOT(), perr.Path)
  39. if err != nil {
  40. perr.Path = "REDACTED"
  41. } else {
  42. perr.Path = filepath.Join("$GOROOT", rel)
  43. }
  44. }
  45. p.ServePage(w, Page{
  46. Title: "File " + relpath,
  47. Subtitle: relpath,
  48. Body: applyTemplate(p.ErrorHTML, "errorHTML", err),
  49. GoogleCN: googleCN(r),
  50. })
  51. }
  52. var onAppengine = false // overridden in appengine.go when on app engine
  53. func googleCN(r *http.Request) bool {
  54. if r.FormValue("googlecn") != "" {
  55. return true
  56. }
  57. if !onAppengine {
  58. return false
  59. }
  60. if strings.HasSuffix(r.Host, ".cn") {
  61. return true
  62. }
  63. switch r.Header.Get("X-AppEngine-Country") {
  64. case "", "ZZ", "CN":
  65. return true
  66. }
  67. return false
  68. }