2013-07-18 22:02:03 -06:00
|
|
|
// Copyright 2010 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.
|
|
|
|
|
|
|
|
// The /doc/codewalk/ tree is synthesized from codewalk descriptions,
|
|
|
|
// files named $GOROOT/doc/codewalk/*.xml.
|
|
|
|
// For an example and a description of the format, see
|
|
|
|
// http://golang.org/doc/codewalk/codewalk or run godoc -http=:6060
|
|
|
|
// and see http://localhost:6060/doc/codewalk/codewalk .
|
|
|
|
// That page is itself a codewalk; the source code for it is
|
|
|
|
// $GOROOT/doc/codewalk/codewalk.xml.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-23 22:33:14 -06:00
|
|
|
"encoding/json"
|
|
|
|
"go/format"
|
2013-07-29 22:23:23 -06:00
|
|
|
"log"
|
2013-07-18 22:02:03 -06:00
|
|
|
"net/http"
|
2015-09-21 21:53:20 -06:00
|
|
|
"strings"
|
2013-07-29 22:23:23 -06:00
|
|
|
"text/template"
|
2013-07-18 22:02:03 -06:00
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/godoc"
|
2018-09-04 10:55:45 -06:00
|
|
|
"golang.org/x/tools/godoc/env"
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/godoc/redirect"
|
|
|
|
"golang.org/x/tools/godoc/vfs"
|
2013-07-29 22:23:23 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pres *godoc.Presentation
|
|
|
|
fs = vfs.NameSpace{}
|
2013-07-18 22:02:03 -06:00
|
|
|
)
|
|
|
|
|
2015-09-21 21:53:20 -06:00
|
|
|
// hostEnforcerHandler redirects requests to "http://foo.golang.org/bar"
|
|
|
|
// to "https://golang.org/bar".
|
2018-01-19 20:12:17 -07:00
|
|
|
// It permits requests to the host "godoc-test.golang.org" for testing and
|
|
|
|
// golang.google.cn for Chinese users.
|
2015-09-21 21:53:20 -06:00
|
|
|
type hostEnforcerHandler struct {
|
|
|
|
h http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h hostEnforcerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2018-09-04 10:55:45 -06:00
|
|
|
if !env.EnforceHosts() {
|
2015-09-21 21:53:20 -06:00
|
|
|
h.h.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2018-10-02 17:44:50 -06:00
|
|
|
if !h.isHTTPS(r) || !h.validHost(r.Host) {
|
2015-09-21 21:53:20 -06:00
|
|
|
r.URL.Scheme = "https"
|
2015-09-23 22:33:14 -06:00
|
|
|
if h.validHost(r.Host) {
|
|
|
|
r.URL.Host = r.Host
|
|
|
|
} else {
|
2015-09-21 21:53:20 -06:00
|
|
|
r.URL.Host = "golang.org"
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, r.URL.String(), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
2018-07-03 21:44:33 -06:00
|
|
|
w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
|
2015-09-21 21:53:20 -06:00
|
|
|
h.h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2018-10-02 17:44:50 -06:00
|
|
|
func (h hostEnforcerHandler) isHTTPS(r *http.Request) bool {
|
|
|
|
return r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https"
|
|
|
|
}
|
|
|
|
|
2015-09-21 21:53:20 -06:00
|
|
|
func (h hostEnforcerHandler) validHost(host string) bool {
|
|
|
|
switch strings.ToLower(host) {
|
2018-10-02 17:44:50 -06:00
|
|
|
case "golang.org", "golang.google.cn":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(host, "-dot-golang-org.appspot.com") {
|
|
|
|
// staging/test
|
2015-09-21 21:53:20 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:17:19 -06:00
|
|
|
func registerHandlers(pres *godoc.Presentation) *http.ServeMux {
|
2013-07-18 22:02:03 -06:00
|
|
|
if pres == nil {
|
|
|
|
panic("nil Presentation")
|
|
|
|
}
|
2015-09-21 21:53:20 -06:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/doc/codewalk/", codewalk)
|
|
|
|
mux.Handle("/doc/play/", pres.FileServer())
|
|
|
|
mux.Handle("/robots.txt", pres.FileServer())
|
|
|
|
mux.Handle("/", pres)
|
|
|
|
mux.Handle("/pkg/C/", redirect.Handler("/cmd/cgo/"))
|
2015-09-23 22:33:14 -06:00
|
|
|
mux.HandleFunc("/fmt", fmtHandler)
|
2015-09-21 21:53:20 -06:00
|
|
|
redirect.Register(mux)
|
2015-09-23 22:33:14 -06:00
|
|
|
|
2015-09-21 21:53:20 -06:00
|
|
|
http.Handle("/", hostEnforcerHandler{mux})
|
2015-09-22 20:17:19 -06:00
|
|
|
|
|
|
|
return mux
|
2013-07-18 22:02:03 -06:00
|
|
|
}
|
2013-07-29 22:23:23 -06:00
|
|
|
|
|
|
|
func readTemplate(name string) *template.Template {
|
|
|
|
if pres == nil {
|
|
|
|
panic("no global Presentation set yet")
|
|
|
|
}
|
|
|
|
path := "lib/godoc/" + name
|
|
|
|
|
|
|
|
// use underlying file system fs to read the template file
|
|
|
|
// (cannot use template ParseFile functions directly)
|
|
|
|
data, err := vfs.ReadFile(fs, path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("readTemplate: ", err)
|
|
|
|
}
|
|
|
|
// be explicit with errors (for app engine use)
|
|
|
|
t, err := template.New(name).Funcs(pres.FuncMap()).Parse(string(data))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("readTemplate: ", err)
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2013-08-19 23:29:56 -06:00
|
|
|
func readTemplates(p *godoc.Presentation, html bool) {
|
2013-07-29 22:23:23 -06:00
|
|
|
p.PackageText = readTemplate("package.txt")
|
|
|
|
p.SearchText = readTemplate("search.txt")
|
2013-08-19 23:29:56 -06:00
|
|
|
|
2013-11-20 07:56:10 -07:00
|
|
|
if html || p.HTMLMode {
|
2013-08-19 23:29:56 -06:00
|
|
|
codewalkHTML = readTemplate("codewalk.html")
|
|
|
|
codewalkdirHTML = readTemplate("codewalkdir.html")
|
2014-03-14 16:58:22 -06:00
|
|
|
p.CallGraphHTML = readTemplate("callgraph.html")
|
2013-08-19 23:29:56 -06:00
|
|
|
p.DirlistHTML = readTemplate("dirlist.html")
|
|
|
|
p.ErrorHTML = readTemplate("error.html")
|
|
|
|
p.ExampleHTML = readTemplate("example.html")
|
|
|
|
p.GodocHTML = readTemplate("godoc.html")
|
2014-03-14 16:58:22 -06:00
|
|
|
p.ImplementsHTML = readTemplate("implements.html")
|
|
|
|
p.MethodSetHTML = readTemplate("methodset.html")
|
2013-08-19 23:29:56 -06:00
|
|
|
p.PackageHTML = readTemplate("package.html")
|
2018-03-17 13:16:28 -06:00
|
|
|
p.PackageRootHTML = readTemplate("packageroot.html")
|
2013-08-19 23:29:56 -06:00
|
|
|
p.SearchHTML = readTemplate("search.html")
|
2014-01-06 07:51:01 -07:00
|
|
|
p.SearchDocHTML = readTemplate("searchdoc.html")
|
|
|
|
p.SearchCodeHTML = readTemplate("searchcode.html")
|
|
|
|
p.SearchTxtHTML = readTemplate("searchtxt.html")
|
2013-08-19 23:29:56 -06:00
|
|
|
p.SearchDescXML = readTemplate("opensearch.xml")
|
|
|
|
}
|
2013-07-29 22:23:23 -06:00
|
|
|
}
|
2015-09-23 22:33:14 -06:00
|
|
|
|
|
|
|
type fmtResponse struct {
|
|
|
|
Body string
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmtHandler takes a Go program in its "body" form value, formats it with
|
|
|
|
// standard gofmt formatting, and writes a fmtResponse as a JSON object.
|
|
|
|
func fmtHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
resp := new(fmtResponse)
|
|
|
|
body, err := format.Source([]byte(r.FormValue("body")))
|
|
|
|
if err != nil {
|
|
|
|
resp.Error = err.Error()
|
|
|
|
} else {
|
|
|
|
resp.Body = string(body)
|
|
|
|
}
|
2015-09-24 20:10:38 -06:00
|
|
|
w.Header().Set("Content-type", "application/json; charset=utf-8")
|
2015-09-23 22:33:14 -06:00
|
|
|
json.NewEncoder(w).Encode(resp)
|
|
|
|
}
|