1
0
Fork 0

Perform checks for / and * inside a newPath func.

This commit is contained in:
Alexander Andreev 2023-05-28 01:22:28 +04:00
parent 5d7d595df3
commit 99a7cebd0a
Signed by: Arav
GPG Key ID: D22A817D95815393
1 changed files with 11 additions and 3 deletions

View File

@ -9,13 +9,21 @@ import (
type path []string
// newPath splits a path and ensures that it starts with a slash (/).
// newPath splits a path and ensures that it starts with a slash (/) and doesn't
// have more than 1 catch-all parameter.
func newPath(path string) (path, error) {
parts := strings.Split(strings.TrimSuffix(path, "/"), "/")
if parts[0] != "" {
if path[0] != '/' {
return nil, errors.New("path should start with a slash (/) symbol")
}
if strings.Count(path, "*") > 1 {
return nil, errors.New("there can be only one catch-all (*) parameter in path")
}
parts := strings.Split(strings.TrimSuffix(path, "/"), "/")
parts[0] = "/"
return parts, nil
}