Skip to content

Commit

Permalink
actually fix http -> https redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Jan 22, 2019
1 parent d28895b commit cceb0e2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"log"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
Expand Down Expand Up @@ -53,7 +54,7 @@ func listenAndServe(addr string, mux *http.ServeMux, tlfConf *tls.Config, stop <
globals.tlsRedirectHTTP, server.Addr)

// This is a second HTTP server listenning on a different port.
go http.ListenAndServe(globals.tlsRedirectHTTP, tlsRedirect(addr))
go http.ListenAndServe(globals.tlsRedirectHTTP, tlsRedirect(server.Addr))
}

log.Printf("Listening for client HTTPS connections on [%s]", server.Addr)
Expand Down Expand Up @@ -210,10 +211,13 @@ func tlsRedirect(toPort string) http.HandlerFunc {
}

return func(wrt http.ResponseWriter, req *http.Request) {
// Host name is guaranteed to be valid because of TLS whitelist.
host, _, _ := net.SplitHostPort(req.Host)
host, _, err := net.SplitHostPort(req.Host)
if err != nil {
// If SplitHostPort has failed assume it's because :port part is missing.
host = req.Host
}

target := *req.URL
target, _ := url.ParseRequestURI(req.RequestURI)
target.Scheme = "https"

// Ensure valid redirect target.
Expand All @@ -224,6 +228,10 @@ func tlsRedirect(toPort string) http.HandlerFunc {
target.Host = host
}

if target.Path == "" {
target.Path = "/"
}

http.Redirect(wrt, req, target.String(), http.StatusTemporaryRedirect)
}
}
Expand Down

0 comments on commit cceb0e2

Please sign in to comment.