app: redirect from HTTP to HTTPS Change-Id: I0cfe832d2d04042305418d7b7ee53a2cc129d513 Reviewed-on: https://go-review.googlesource.com/22363 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/app/goplay/compile.go b/app/goplay/compile.go index be1e3e7..0619587 100644 --- a/app/goplay/compile.go +++ b/app/goplay/compile.go
@@ -16,7 +16,7 @@ const runUrl = "http://golang.org/compile?output=json" func init() { - http.HandleFunc("/compile", compile) + http.HandleFunc("/compile", httpsOnlyHandler{compile}) } func compile(w http.ResponseWriter, r *http.Request) {
diff --git a/app/goplay/edit.go b/app/goplay/edit.go index ce29a78..8d9600a 100644 --- a/app/goplay/edit.go +++ b/app/goplay/edit.go
@@ -16,7 +16,7 @@ const hostname = "play.golang.org" func init() { - http.HandleFunc("/", edit) + http.HandleFunc("/", httpsOnlyHandler{edit}) } var editTemplate = template.Must(template.ParseFiles("goplay/edit.html"))
diff --git a/app/goplay/fmt.go b/app/goplay/fmt.go index a862343..4cda931 100644 --- a/app/goplay/fmt.go +++ b/app/goplay/fmt.go
@@ -13,7 +13,7 @@ ) func init() { - http.HandleFunc("/fmt", fmtHandler) + http.HandleFunc("/fmt", httpsOnlyHandler{fmtHandler}) } type fmtResponse struct {
diff --git a/app/goplay/https.go b/app/goplay/https.go new file mode 100644 index 0000000..93297ac --- /dev/null +++ b/app/goplay/https.go
@@ -0,0 +1,27 @@ +// Copyright 2016 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 goplay + +import ( + "net/http" + + "appengine" +) + +// httpsOnlyHandler redirects requests to "http://example.com/foo?bar" +// to "https://example.com/foo?bar" +type httpsOnlyHandler struct { + fn http.HandlerFunc +} + +func (h httpsOnlyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if !appengine.IsDevAppServer() && r.TLS == nil { + r.URL.Scheme = "https" + r.URL.Host = r.Host + http.Redirect(w, r, r.URL.String(), http.StatusFound) + return + } + h.fn(w, r) +}
diff --git a/app/goplay/play.go b/app/goplay/play.go index b8257f1..0dcee31 100644 --- a/app/goplay/play.go +++ b/app/goplay/play.go
@@ -12,7 +12,7 @@ ) func init() { - http.HandleFunc("/playground.js", play) + http.HandleFunc("/playground.js", httpsOnlyHandler{play}) } func play(w http.ResponseWriter, r *http.Request) {
diff --git a/app/goplay/share.go b/app/goplay/share.go index 72fca10..e0c16a0 100644 --- a/app/goplay/share.go +++ b/app/goplay/share.go
@@ -35,7 +35,7 @@ } func init() { - http.HandleFunc("/share", share) + http.HandleFunc("/share", httpsOnlyHandler{share}) } func share(w http.ResponseWriter, r *http.Request) {