1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-12-22 15:25:48 +01:00

Implement redirection to canonical host.

This commit is contained in:
Juliusz Chroboczek 2020-12-07 02:43:51 +01:00
parent ef1c211b7f
commit f8e2f755d6
2 changed files with 30 additions and 0 deletions

View file

@ -27,6 +27,8 @@ func main() {
flag.StringVar(&httpAddr, "http", ":8443", "web server `address`")
flag.StringVar(&webserver.StaticRoot, "static", "./static/",
"web server root `directory`")
flag.StringVar(&webserver.Redirect, "redirect", "",
"redirect to canonical `host`")
flag.StringVar(&dataDir, "data", "./data/",
"data `directory`")
flag.StringVar(&group.Directory, "groups", "./groups/",

View file

@ -31,6 +31,8 @@ var server atomic.Value
var StaticRoot string
var Redirect string
func Serve(address string, dataDir string) error {
http.Handle("/", &fileHandler{http.Dir(StaticRoot)})
http.HandleFunc("/group/", groupHandler)
@ -116,6 +118,20 @@ const (
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) {
etag := fmt.Sprintf("\"%v-%v\"", fi.Size(), fi.ModTime().UnixNano())
w.Header().Set("ETag", etag)
@ -140,6 +156,10 @@ type fileHandler struct {
}
func (fh *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if redirect(w, r) {
return
}
mungeHeader(w)
p := r.URL.Path
// this ensures any leading .. are removed by path.Clean below
@ -236,6 +256,10 @@ func parseGroupName(path string) string {
}
func groupHandler(w http.ResponseWriter, r *http.Request) {
if redirect(w, r) {
return
}
mungeHeader(w)
name := parseGroupName(r.URL.Path)
if name == "" {
@ -420,6 +444,10 @@ func wsHandler(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/" {
http.Error(w, "server error", http.StatusInternalServerError)
return