From c2b1723bd4ff6c655fc4423da9016dac8d3c4822 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Thu, 10 Sep 2020 13:39:38 +0200 Subject: [PATCH] Fail early when a group doesn't exist. --- group.go | 6 ------ webclient.go | 3 +++ webserver.go | 45 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/group.go b/group.go index 50fd4d0..d81ffea 100644 --- a/group.go +++ b/group.go @@ -346,9 +346,6 @@ type groupDescription struct { func descriptionChanged(name string, old *groupDescription) (bool, error) { fi, err := os.Stat(filepath.Join(groupsDir, name+".json")) if err != nil { - if os.IsNotExist(err) { - err = userError("group does not exist") - } return false, err } if fi.Size() != old.fileSize || fi.ModTime() != old.modTime { @@ -360,9 +357,6 @@ func descriptionChanged(name string, old *groupDescription) (bool, error) { func getDescription(name string) (*groupDescription, error) { r, err := os.Open(filepath.Join(groupsDir, name+".json")) if err != nil { - if os.IsNotExist(err) { - err = userError("group does not exist") - } return nil, err } defer r.Close() diff --git a/webclient.go b/webclient.go index f76cc9a..ea2cb7b 100644 --- a/webclient.go +++ b/webclient.go @@ -704,6 +704,9 @@ func startClient(conn *websocket.Conn) (err error) { g, err := addClient(m.Group, c) if err != nil { + if os.IsNotExist(err) { + err = userError("group does not exist") + } return } c.group = g diff --git a/webserver.go b/webserver.go index 7926121..95c0dec 100644 --- a/webserver.go +++ b/webserver.go @@ -21,12 +21,7 @@ import ( func webserver() { http.Handle("/", mungeHandler{http.FileServer(http.Dir(staticRoot))}) - http.HandleFunc("/group/", - func(w http.ResponseWriter, r *http.Request) { - mungeHeader(w) - http.ServeFile(w, r, - filepath.Join(staticRoot, "sfu.html")) - }) + http.HandleFunc("/group/", groupHandler) http.HandleFunc("/recordings", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, @@ -74,6 +69,44 @@ func (h mungeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.h.ServeHTTP(w, r) } +func parseGroupName(path string) string { + if !strings.HasPrefix(path, "/group/") { + return "" + } + + name := path[len("/group/"):] + if name == "" { + return "" + } + + if name[len(name)-1] == '/' { + name = name[:len(name)-1] + } + return name +} + +func groupHandler(w http.ResponseWriter, r *http.Request) { + mungeHeader(w) + name := parseGroupName(r.URL.Path) + if name == "" { + http.NotFound(w, r) + return + } + + _, err := addGroup(name, nil) + if err != nil { + if os.IsNotExist(err) { + http.NotFound(w, r) + } else { + log.Println("addGroup: %v", err) + http.Error(w, "Internal server error", + http.StatusInternalServerError) + } + return + } + http.ServeFile(w, r, filepath.Join(staticRoot, "sfu.html")) +} + func publicHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "application/json") w.Header().Set("cache-control", "no-cache")