From 53006ac4c204faf590b60aaeda0f13d0d3361927 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Fri, 22 Apr 2016 13:40:43 +1000 Subject: [PATCH] cmd/tip: redirect from HTTP to HTTPS; update to Go 1.6 Change-Id: I7b219a991df4f71d068b62a22f69acb123ac31f0 Reviewed-on: https://go-review.googlesource.com/22367 Reviewed-by: Brad Fitzpatrick --- cmd/tip/Dockerfile | 4 ++-- cmd/tip/tip.go | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cmd/tip/Dockerfile b/cmd/tip/Dockerfile index 8364cdb8..d258f7f7 100644 --- a/cmd/tip/Dockerfile +++ b/cmd/tip/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.5 +FROM golang:1.6 RUN apt-get update && apt-get install --no-install-recommends -y -q build-essential git @@ -10,4 +10,4 @@ ADD . /go/src/tip RUN go install tip ENTRYPOINT ["/go/bin/tip"] # Kubernetes expects us to listen on port 8080 -EXPOSE 8080 +EXPOSE 8080 diff --git a/cmd/tip/tip.go b/cmd/tip/tip.go index 43894144..e521024f 100644 --- a/cmd/tip/tip.go +++ b/cmd/tip/tip.go @@ -44,7 +44,7 @@ func main() { p := &Proxy{builder: b} go p.run() - http.Handle("/", p) + http.Handle("/", httpsOnlyHandler{p}) http.HandleFunc("/_ah/health", p.serveHealthCheck) log.Print("Starting up") @@ -323,3 +323,19 @@ func getOK(url string) (body []byte, err error) { } return body, nil } + +// httpsOnlyHandler redirects requests to "http://example.com/foo?bar" +// to "https://example.com/foo?bar" +type httpsOnlyHandler struct { + h http.Handler +} + +func (h httpsOnlyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("X-Appengine-Https") == "off" { + r.URL.Scheme = "https" + r.URL.Host = r.Host + http.Redirect(w, r, r.URL.String(), http.StatusFound) + return + } + h.h.ServeHTTP(w, r) +}