From 6b41c776c8733a36ba4586aa0bfaf5b6878c41d8 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Thu, 24 Sep 2015 14:33:14 +1000 Subject: [PATCH] cmd/godoc: always serve fmt handler, HTTPS fix redirection Change-Id: Ib00a54f40b3c52c9bc6a07623259c5c82f9d28d9 Reviewed-on: https://go-review.googlesource.com/14933 Reviewed-by: Brad Fitzpatrick --- cmd/godoc/handlers.go | 26 +++++++++++++++++++++++++- cmd/godoc/play.go | 41 +++-------------------------------------- 2 files changed, 28 insertions(+), 39 deletions(-) diff --git a/cmd/godoc/handlers.go b/cmd/godoc/handlers.go index 25e4cd80..37dc7db3 100644 --- a/cmd/godoc/handlers.go +++ b/cmd/godoc/handlers.go @@ -13,6 +13,8 @@ package main import ( + "encoding/json" + "go/format" "log" "net/http" "strings" @@ -44,7 +46,9 @@ func (h hostEnforcerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if r.TLS == nil || !h.validHost(r.Host) { r.URL.Scheme = "https" - if !h.validHost(r.Host) { + if h.validHost(r.Host) { + r.URL.Host = r.Host + } else { r.URL.Host = "golang.org" } http.Redirect(w, r, r.URL.String(), http.StatusFound) @@ -71,7 +75,9 @@ func registerHandlers(pres *godoc.Presentation) *http.ServeMux { mux.Handle("/robots.txt", pres.FileServer()) mux.Handle("/", pres) mux.Handle("/pkg/C/", redirect.Handler("/cmd/cgo/")) + mux.HandleFunc("/fmt", fmtHandler) redirect.Register(mux) + http.Handle("/", hostEnforcerHandler{mux}) return mux @@ -119,3 +125,21 @@ func readTemplates(p *godoc.Presentation, html bool) { p.SearchDescXML = readTemplate("opensearch.xml") } } + +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) + } + json.NewEncoder(w).Encode(resp) +} diff --git a/cmd/godoc/play.go b/cmd/godoc/play.go index b8c55345..02f477d3 100644 --- a/cmd/godoc/play.go +++ b/cmd/godoc/play.go @@ -6,41 +6,6 @@ package main -import ( - "encoding/json" - "fmt" - "go/format" - "net/http" - - // This package registers "/compile" and "/share" handlers - // that redirect to the golang.org playground. - _ "golang.org/x/tools/playground" -) - -func init() { - http.HandleFunc("/fmt", fmtHandler) -} - -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) - } - json.NewEncoder(w).Encode(resp) -} - -// disabledHandler serves a 501 "Not Implemented" response. -func disabledHandler(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusNotImplemented) - fmt.Fprint(w, "This functionality is not available via local godoc.") -} +// This package registers "/compile" and "/share" handlers +// that redirect to the golang.org playground. +import _ "golang.org/x/tools/playground"