2021-12-03 02:44:21 +00:00
|
|
|
package utils
|
|
|
|
|
2022-11-12 19:37:20 +00:00
|
|
|
import (
|
2023-08-27 08:13:15 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2022-11-12 19:37:20 +00:00
|
|
|
"strings"
|
|
|
|
)
|
2021-12-03 02:44:21 +00:00
|
|
|
|
2022-11-12 19:37:20 +00:00
|
|
|
func TrimHostPort(host string) string {
|
|
|
|
i := strings.IndexByte(host, ':')
|
2021-12-03 02:44:21 +00:00
|
|
|
if i >= 0 {
|
|
|
|
return host[:i]
|
|
|
|
}
|
|
|
|
return host
|
|
|
|
}
|
2023-08-27 08:13:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|