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"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Page describes the contents of the top-level godoc webpage.
|
|
|
|
type Page struct {
|
|
|
|
Title string
|
|
|
|
Tabtitle string
|
|
|
|
Subtitle string
|
|
|
|
Query string
|
|
|
|
Body []byte
|
2015-09-01 17:49:30 -06:00
|
|
|
Share bool
|
2013-07-17 01:17:12 -06:00
|
|
|
|
|
|
|
// filled in by servePage
|
|
|
|
SearchBox bool
|
|
|
|
Playground bool
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
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()
|
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)
|
2013-07-17 17:52:45 -06:00
|
|
|
p.ServePage(w, Page{
|
2013-07-17 01:17:12 -06:00
|
|
|
Title: "File " + relpath,
|
|
|
|
Subtitle: relpath,
|
2013-07-17 21:51:17 -06:00
|
|
|
Body: applyTemplate(p.ErrorHTML, "errorHTML", err), // err may contain an absolute path!
|
2015-09-01 17:49:30 -06:00
|
|
|
Share: allowShare(r),
|
2013-07-17 01:17:12 -06:00
|
|
|
})
|
|
|
|
}
|
2015-09-01 17:49:30 -06:00
|
|
|
|
|
|
|
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") {
|
2015-09-28 01:00:17 -06:00
|
|
|
case "", "ZZ", "CN":
|
2015-09-01 17:49:30 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|