Skip to content
Snippets Groups Projects
Commit 31841975 authored by ale's avatar ale
Browse files

Ensure that all redirects respect the request protocol

parent b525072c
No related branches found
No related tags found
1 merge request!2Add SSL functionality
......@@ -195,20 +195,30 @@ func serveRedirect(lb *loadBalancer, mount *pb.Mount, w http.ResponseWriter, r *
return
}
targetURL := fmt.Sprintf("http://%s%s", targetAddr, autoradio.MountPathToIcecastPath(mount.Path))
sendRedirect(w, r, targetURL)
targetURL := url.URL{
Scheme: schemeFromRequest(r),
Host: targetAddr,
Path: autoradio.MountPathToIcecastPath(mount.Path),
}
sendRedirect(w, r, targetURL.String())
}
// Serve a M3U response. This simply points back at the stream
// redirect handler by dropping the .m3u suffix in the request URL.
func sendM3U(w http.ResponseWriter, r *http.Request) {
// Build a fully qualified URL using the Host header.
m3u := fmt.Sprintf("http://%s%s\n", r.Host, strings.TrimSuffix(r.URL.Path, ".m3u"))
// Build a fully qualified URL using the Host header from the incoming request.
m3url := url.URL{
Scheme: schemeFromRequest(r),
Host: r.Host,
Path: strings.TrimSuffix(r.URL.Path, ".m3u"),
}
m3us := m3url.String()
w.Header().Set("Content-Length", strconv.Itoa(len(m3u)))
w.Header().Set("Content-Length", strconv.Itoa(len(m3us)))
w.Header().Set("Content-Type", "audio/x-mpegurl")
addDefaultHeaders(w)
io.WriteString(w, m3u) //nolint
io.WriteString(w, m3us) //nolint
}
func sendRedirect(w http.ResponseWriter, r *http.Request, targetURL string) {
......@@ -231,6 +241,13 @@ func addDefaultHeaders(w http.ResponseWriter) {
w.Header().Set("Cache-Control", "no-cache,no-store")
}
func schemeFromRequest(r *http.Request) string {
if r.TLS != nil {
return "https"
}
return "http"
}
// Parse the templates that are embedded with the binary (in bindata.go).
func mustParseEmbeddedTemplates() *template.Template {
root := template.New("")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment