Commit f8e2f755 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Implement redirection to canonical host.

parent ef1c211b
...@@ -27,6 +27,8 @@ func main() { ...@@ -27,6 +27,8 @@ func main() {
flag.StringVar(&httpAddr, "http", ":8443", "web server `address`") flag.StringVar(&httpAddr, "http", ":8443", "web server `address`")
flag.StringVar(&webserver.StaticRoot, "static", "./static/", flag.StringVar(&webserver.StaticRoot, "static", "./static/",
"web server root `directory`") "web server root `directory`")
flag.StringVar(&webserver.Redirect, "redirect", "",
"redirect to canonical `host`")
flag.StringVar(&dataDir, "data", "./data/", flag.StringVar(&dataDir, "data", "./data/",
"data `directory`") "data `directory`")
flag.StringVar(&group.Directory, "groups", "./groups/", flag.StringVar(&group.Directory, "groups", "./groups/",
......
...@@ -31,6 +31,8 @@ var server atomic.Value ...@@ -31,6 +31,8 @@ var server atomic.Value
var StaticRoot string var StaticRoot string
var Redirect string
func Serve(address string, dataDir string) error { func Serve(address string, dataDir string) error {
http.Handle("/", &fileHandler{http.Dir(StaticRoot)}) http.Handle("/", &fileHandler{http.Dir(StaticRoot)})
http.HandleFunc("/group/", groupHandler) http.HandleFunc("/group/", groupHandler)
...@@ -116,6 +118,20 @@ const ( ...@@ -116,6 +118,20 @@ const (
veryCachableCacheControl = "max-age=86400" veryCachableCacheControl = "max-age=86400"
) )
func redirect(w http.ResponseWriter, r *http.Request) bool {
if Redirect == "" || strings.EqualFold(r.Host, Redirect) {
return false
}
u := url.URL{
Scheme: "https",
Host: Redirect,
Path: r.URL.Path,
}
http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
return true
}
func makeCachable(w http.ResponseWriter, p string, fi os.FileInfo, cachable bool) { func makeCachable(w http.ResponseWriter, p string, fi os.FileInfo, cachable bool) {
etag := fmt.Sprintf("\"%v-%v\"", fi.Size(), fi.ModTime().UnixNano()) etag := fmt.Sprintf("\"%v-%v\"", fi.Size(), fi.ModTime().UnixNano())
w.Header().Set("ETag", etag) w.Header().Set("ETag", etag)
...@@ -140,6 +156,10 @@ type fileHandler struct { ...@@ -140,6 +156,10 @@ type fileHandler struct {
} }
func (fh *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (fh *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if redirect(w, r) {
return
}
mungeHeader(w) mungeHeader(w)
p := r.URL.Path p := r.URL.Path
// this ensures any leading .. are removed by path.Clean below // this ensures any leading .. are removed by path.Clean below
...@@ -236,6 +256,10 @@ func parseGroupName(path string) string { ...@@ -236,6 +256,10 @@ func parseGroupName(path string) string {
} }
func groupHandler(w http.ResponseWriter, r *http.Request) { func groupHandler(w http.ResponseWriter, r *http.Request) {
if redirect(w, r) {
return
}
mungeHeader(w) mungeHeader(w)
name := parseGroupName(r.URL.Path) name := parseGroupName(r.URL.Path)
if name == "" { if name == "" {
...@@ -420,6 +444,10 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { ...@@ -420,6 +444,10 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
} }
func recordingsHandler(w http.ResponseWriter, r *http.Request) { func recordingsHandler(w http.ResponseWriter, r *http.Request) {
if redirect(w, r) {
return
}
if len(r.URL.Path) < 12 || r.URL.Path[:12] != "/recordings/" { if len(r.URL.Path) < 12 || r.URL.Path[:12] != "/recordings/" {
http.Error(w, "server error", http.StatusInternalServerError) http.Error(w, "server error", http.StatusInternalServerError)
return return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment