mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-23 06:18:59 +00:00
28 lines
595 B
Go
28 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
|
|
}
|