mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-06 06:17:02 +00:00
27 lines
595 B
Go
27 lines
595 B
Go
package utils
|
|
|
|
import (
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
func TrimHostPort(host string) string {
|
|
i := strings.IndexByte(host, ':')
|
|
if i >= 0 {
|
|
return host[:i]
|
|
}
|
|
return host
|
|
}
|
|
|
|
func CleanPath(uriPath string) string {
|
|
unescapedPath, _ := url.PathUnescape(uriPath)
|
|
cleanedPath := path.Join("/", unescapedPath)
|
|
|
|
// If the path refers to a directory, add a trailing slash.
|
|
if !strings.HasSuffix(cleanedPath, "/") && (strings.HasSuffix(unescapedPath, "/") || strings.HasSuffix(unescapedPath, "/.") || strings.HasSuffix(unescapedPath, "/..")) {
|
|
cleanedPath += "/"
|
|
}
|
|
|
|
return cleanedPath
|
|
}
|