@ -7,6 +7,7 @@ package util
import (
import (
"net/url"
"net/url"
"path"
"path"
"strings"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/log"
)
)
@ -56,16 +57,25 @@ func Max(a, b int) int {
// URLJoin joins url components, like path.Join, but preserving contents
// URLJoin joins url components, like path.Join, but preserving contents
func URLJoin ( base string , elems ... string ) string {
func URLJoin ( base string , elems ... string ) string {
u , err := url . Parse ( base )
if ! strings . HasSuffix ( base , "/" ) {
base += "/"
}
baseURL , err := url . Parse ( base )
if err != nil {
if err != nil {
log . Error ( 4 , "URLJoin: Invalid base URL %s" , base )
log . Error ( 4 , "URLJoin: Invalid base URL %s" , base )
return ""
return ""
}
}
joinArgs := make ( [ ] string , 0 , len ( elems ) + 1 )
joinedPath := path . Join ( elems ... )
joinArgs = append ( joinArgs , u . Path )
argURL , err := url . Parse ( joinedPath )
joinArgs = append ( joinArgs , elems ... )
if err != nil {
u . Path = path . Join ( joinArgs ... )
log . Error ( 4 , "URLJoin: Invalid arg %s" , joinedPath )
return u . String ( )
return ""
}
joinedURL := baseURL . ResolveReference ( argURL ) . String ( )
if ! baseURL . IsAbs ( ) {
return joinedURL [ 1 : ] // Removing leading '/'
}
return joinedURL
}
}
// Min min of two ints
// Min min of two ints