-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed
Labels
Milestone
Description
We have url.URL.ResolveReference, but nothing to perform the reverse calculation:
calculating a relative path that will transform one URL path into another.
The calculation is not hard but it's tricky to get right, and a function to
do this seems like it would sit well inside the net/url package.
In all the places we've needed this so far, both the base and target paths
are within the same host, so a path-only calculation seems to work well
and saves a bunch of potential edge cases.
So I propose this addition to net/url:
// RelativeURLPath returns a relative URL path that is lexically
// equivalent to targpath when interpreted by url.URL.ResolveReference.
// That is, for a path x returned by RelativeURLPath(basePath, targPath),
// and a URL u with path basePath, u.Parse(x) will return a URL
// with path targPath.
//
// It is assumed that both basePath and targPath are normalized
// (have no . or .. elements).
//
// An error is returned if basePath or targPath are not absolute paths.
func RelativeURLPath(basePath, targPath string) (string, error)