Tarun Koyalwar a5b39dcaa5
automerge url parameters from input and templates (#3010)
* fix automerge url parameters

* fix url encoding & refactor raw request

* handle trailing slash edgecases

* minor code refactoring
2022-12-13 12:09:31 +05:30

37 lines
863 B
Go

package utils
import (
"fmt"
"path"
"strings"
)
// Joins two relative paths and handles trailing slash edgecase
func JoinURLPath(elem1 string, elem2 string) string {
/*
Trailing Slash EdgeCase
Path.Join converts /test/ to /test
this should be handled manually
*/
if elem2 == "" {
return elem1
}
if elem2 == "/" || elem2 == "/?" {
// check for extra slash
if strings.HasSuffix(elem1, "/") && strings.HasPrefix(elem2, "/") {
elem1 = strings.TrimRight(elem1, "/")
}
// merge and return
return fmt.Sprintf("%v%v", elem1, elem2)
} else {
if strings.HasPrefix(elem2, "?") {
// path2 is parameter and not a url append and return
return fmt.Sprintf("%v%v", elem1, elem2)
}
// Note:
// path.Join implicitly calls path.Clean so any relative paths are filtered
// if not encoded properly
return path.Join(elem1, elem2)
}
}