Added a newServePath() func that is a special variant of newPath that is used in ServeHTTP (it lacks an unnecessary check for * catch-all symbol).

This commit is contained in:
Alexander Andreev 2023-09-05 06:00:42 +04:00
parent cc2cd72df8
commit 5d613b34ee
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -36,9 +36,21 @@ func newPath(path string) (path, error) {
return parts, nil
}
// newServePath is a reduced version of newPath for ServeHTTP.
func newServePath(path string) (path, error) {
pathLen := len(path)
if path[0] != '/' {
return nil, errors.New("path should start with a slash symbol \"/\"")
}
path = strings.ReplaceAll(path, "//", "/")
parts := strings.Split(strings.TrimSuffix(path, "/"), "/")
if path[pathLen-1] == '/' {
path = path[:pathLen-1]
}
parts := strings.Split(path, "/")
parts[0] = "/"
@ -179,7 +191,7 @@ func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
path, err := newPath(r.URL.Path)
path, err := newServePath(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusNotAcceptable)
return