2022-07-05 08:32:10 +07:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
// URLPathFullClean returns a URL path with the query parameters appended (full path) with the path portion parsed
|
|
|
|
// through path.Clean given a *url.URL.
|
2022-07-18 11:59:13 +07:00
|
|
|
func URLPathFullClean(u *url.URL) (output string) {
|
|
|
|
lengthPath := len(u.Path)
|
|
|
|
lengthQuery := len(u.RawQuery)
|
|
|
|
appendForwardSlash := lengthPath > 1 && u.Path[lengthPath-1] == '/'
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case lengthPath == 1 && lengthQuery == 0:
|
|
|
|
return u.Path
|
|
|
|
case lengthPath == 1:
|
2022-07-05 08:32:10 +07:00
|
|
|
return path.Clean(u.Path) + "?" + u.RawQuery
|
2022-07-18 11:59:13 +07:00
|
|
|
case lengthQuery != 0 && appendForwardSlash:
|
|
|
|
return path.Clean(u.Path) + "/?" + u.RawQuery
|
|
|
|
case lengthQuery != 0:
|
|
|
|
return path.Clean(u.Path) + "?" + u.RawQuery
|
|
|
|
case appendForwardSlash:
|
|
|
|
return path.Clean(u.Path) + "/"
|
|
|
|
default:
|
|
|
|
return path.Clean(u.Path)
|
2022-07-05 08:32:10 +07:00
|
|
|
}
|
|
|
|
}
|