2017-10-03 00:37:36 +08:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2021-11-04 18:12:57 +08:00
|
|
|
"fmt"
|
2017-10-03 00:37:36 +08:00
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
// URLJoin joins a URL and a path returning a new URL
|
|
|
|
//
|
|
|
|
// path should be URL escaped
|
|
|
|
func URLJoin(base *url.URL, path string) (*url.URL, error) {
|
|
|
|
rel, err := url.Parse(path)
|
|
|
|
if err != nil {
|
2022-06-09 04:54:39 +08:00
|
|
|
return nil, fmt.Errorf("error parsing %q as URL: %w", path, err)
|
2017-10-03 00:37:36 +08:00
|
|
|
}
|
|
|
|
return base.ResolveReference(rel), nil
|
|
|
|
}
|
|
|
|
|
2018-01-05 23:55:43 +08:00
|
|
|
// URLPathEscape escapes URL path the in string using URL escaping rules
|
2017-10-03 00:37:36 +08:00
|
|
|
//
|
|
|
|
// This mimics url.PathEscape which only available from go 1.8
|
2018-01-05 23:55:43 +08:00
|
|
|
func URLPathEscape(in string) string {
|
2017-10-03 00:37:36 +08:00
|
|
|
var u url.URL
|
|
|
|
u.Path = in
|
|
|
|
return u.String()
|
|
|
|
}
|