Skip to content

Commit 7a24556

Browse files
committed
git-bundle-web-server: handle starting & repeated '/' in route
Rather than step through the provided route character-by-character, split the route into non-empty tokens divided by '/'. This avoids the need for special handling of leading or trailing slashes, as well as treats multiple sequential slashes as a single divider. It's possible that the "multiple slashes" case is already handled by Go's internal route handling, but this implementation gives us a bit of extra safety & is generally a more concise implementation. Signed-off-by: Victoria Dye <[email protected]>
1 parent 1d26977 commit 7a24556

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

cmd/git-bundle-web-server/main.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,19 @@ import (
1515
)
1616

1717
func parseRoute(path string) (string, string, string, error) {
18-
if len(path) == 0 {
18+
elements := strings.FieldsFunc(path, func(char rune) bool { return char == '/' })
19+
switch len(elements) {
20+
case 0:
1921
return "", "", "", fmt.Errorf("empty route")
20-
}
21-
22-
if path[0] == '/' {
23-
path = path[1:]
24-
}
25-
26-
slash1 := strings.Index(path, "/")
27-
if slash1 < 0 {
22+
case 1:
2823
return "", "", "", fmt.Errorf("route has owner, but no repo")
29-
}
30-
slash2 := strings.Index(path[slash1+1:], "/")
31-
if slash2 < 0 {
32-
// No trailing slash.
33-
return path[:slash1], path[slash1+1:], "", nil
34-
}
35-
slash2 += slash1 + 1
36-
slash3 := strings.Index(path[slash2+1:], "/")
37-
if slash3 >= 0 {
24+
case 2:
25+
return elements[0], elements[1], "", nil
26+
case 3:
27+
return elements[0], elements[1], elements[2], nil
28+
default:
3829
return "", "", "", fmt.Errorf("path has depth exceeding three")
3930
}
40-
41-
return path[:slash1], path[slash1+1 : slash2], path[slash2+1:], nil
4231
}
4332

4433
func serve(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)