1
Fork 0

Implement splitPath.

Use it for parsing special paths instead of ad hoc code.
This commit is contained in:
Juliusz Chroboczek 2024-01-17 22:12:22 +01:00
parent 6455ae3a4c
commit b92cf0480a
3 changed files with 59 additions and 68 deletions

View File

@ -300,24 +300,38 @@ func parseGroupName(prefix string, p string) string {
return name[1:] return name[1:]
} }
func splitPath(pth string) (string, string, string) {
index := strings.Index(pth, "/.")
if index < 0 {
return pth, "", ""
}
index2 := strings.Index(pth[index+1:], "/")
if index2 < 0 {
return pth[:index], pth[index+1:], ""
}
return pth[:index], pth[index+1 : index+1+index2], pth[index+1+index2:]
}
func groupHandler(w http.ResponseWriter, r *http.Request) { func groupHandler(w http.ResponseWriter, r *http.Request) {
if redirect(w, r) { if redirect(w, r) {
return return
} }
if strings.HasSuffix(r.URL.Path, "/.status.json") { _, kind, rest := splitPath(r.URL.Path)
if kind == ".status.json" && rest == "" {
groupStatusHandler(w, r) groupStatusHandler(w, r)
return return
} } else if kind == ".whip" {
if rest == "" {
dir, id := parseWhip(r.URL.Path)
if dir != "" {
if id == "" {
whipEndpointHandler(w, r) whipEndpointHandler(w, r)
} else { } else {
whipResourceHandler(w, r) whipResourceHandler(w, r)
} }
return return
} else if kind != "" {
notFound(w)
return
} }
name := parseGroupName("/group/", r.URL.Path) name := parseGroupName("/group/", r.URL.Path)
@ -369,7 +383,11 @@ func groupBase(r *http.Request) (string, error) {
} }
func groupStatusHandler(w http.ResponseWriter, r *http.Request) { func groupStatusHandler(w http.ResponseWriter, r *http.Request) {
pth := path.Dir(r.URL.Path) pth, kind, rest := splitPath(r.URL.Path)
if kind != ".status.json" || rest != "" {
http.Error(w, "Internal server error",
http.StatusInternalServerError)
}
name := parseGroupName("/group/", pth) name := parseGroupName("/group/", pth)
if name == "" { if name == "" {
notFound(w) notFound(w)

View File

@ -22,42 +22,34 @@ func TestParseGroupName(t *testing.T) {
} }
for _, pg := range a { for _, pg := range a {
t.Run(pg.p, func(t *testing.T) {
g := parseGroupName("/group/", pg.p) g := parseGroupName("/group/", pg.p)
if g != pg.g { if g != pg.g {
t.Errorf("Path %v, got %v, expected %v", t.Errorf("Path %v, got %v, expected %v",
pg.p, g, pg.g) pg.p, g, pg.g)
} }
})
} }
} }
func TestParseWhip(t *testing.T) { func TestParseSplit(t *testing.T) {
a := []struct{ p, d, b string }{ a := []struct{ p, a, b, c string }{
{"", "", ""}, {"", "", "", ""},
{"/", "", ""}, {"/a", "/a", "", ""},
{"/foo", "", ""}, {"/a/.b", "/a", ".b", ""},
{"/foo/", "", ""}, {"/a/.b/", "/a", ".b", "/"},
{"/foo/bar", "", ""}, {"/a/.b/c", "/a", ".b", "/c"},
{"/foo/bar/", "", ""}, {"/a/.b/c/", "/a", ".b", "/c/"},
{"/foo/bar/baz", "", ""}, {"/a/.b/c/d", "/a", ".b", "/c/d"},
{"/foo/bar/baz/", "", ""}, {"/a/.b/c/d/", "/a", ".b", "/c/d/"},
{"/foo/.whip", "/foo/", ""}, {"/a/.b/c/d./", "/a", ".b", "/c/d./"},
{"/foo/.whip/", "/foo/", ""},
{"/foo/.whip/bar", "/foo/", "bar"},
{"/foo/.whip/bar/", "/foo/", "bar"},
{"/foo/.whip/bar/baz", "", ""},
{"/foo/.whip/bar/baz/", "", ""},
} }
for _, pdb := range a { for _, pabc := range(a) {
t.Run(pdb.p, func(t *testing.T) { a, b, c := splitPath(pabc.p)
d, b := parseWhip(pdb.p) if pabc.a != a || pabc.b != b || pabc.c != c {
if d != pdb.d || b != pdb.b { t.Errorf("Path %v, got %v, %v, %v, expected %v, %v, %v",
t.Errorf("Path %v, got %v %v, expected %v %v", pabc.p, a, b, c, pabc.a, pabc.b, pabc.c,
pdb.p, d, b, pdb.d, pdb.b) )
} }
})
} }
} }
@ -79,14 +71,12 @@ func TestParseBearerToken(t *testing.T) {
} }
for _, ab := range a { for _, ab := range a {
t.Run(ab.a, func(t *testing.T) {
b := parseBearerToken(ab.a) b := parseBearerToken(ab.a)
if b != ab.b { if b != ab.b {
t.Errorf("Bearer token %v, got %v, expected %v", t.Errorf("Bearer token %v, got %v, expected %v",
ab.a, b, ab.b, ab.a, b, ab.b,
) )
} }
})
} }
} }

View File

@ -23,23 +23,6 @@ import (
"github.com/jech/galene/rtpconn" "github.com/jech/galene/rtpconn"
) )
func parseWhip(pth string) (string, string) {
if pth != "/" {
pth = strings.TrimSuffix(pth, "/")
}
dir := path.Dir(pth)
base := path.Base(pth)
if base == ".whip" {
return dir + "/", ""
}
if path.Base(dir) == ".whip" {
return path.Dir(dir) + "/", base
}
return "", ""
}
var idSecret []byte var idSecret []byte
var idCipher cipher.Block var idCipher cipher.Block
@ -163,8 +146,8 @@ func whipEndpointHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
pth, pthid := parseWhip(r.URL.Path) pth, kind, pthid := splitPath(r.URL.Path)
if pthid != "" { if kind != ".whip" || pthid != "/" {
http.Error(w, "Internal server error", http.Error(w, "Internal server error",
http.StatusInternalServerError) http.StatusInternalServerError)
return return
@ -282,13 +265,13 @@ func whipEndpointHandler(w http.ResponseWriter, r *http.Request) {
} }
func whipResourceHandler(w http.ResponseWriter, r *http.Request) { func whipResourceHandler(w http.ResponseWriter, r *http.Request) {
pth, obfuscated := parseWhip(r.URL.Path) pth, kind, rest := splitPath(r.URL.Path)
if pth == "" || obfuscated == "" { if kind != ".whip" || rest == "" {
http.Error(w, "Internal server error", http.Error(w, "Internal server error",
http.StatusInternalServerError) http.StatusInternalServerError)
return return
} }
id, err := deobfuscate(obfuscated) id, err := deobfuscate(rest[1:])
if err != nil { if err != nil {
http.Error(w, "Internal server error", http.Error(w, "Internal server error",
http.StatusInternalServerError) http.StatusInternalServerError)