Skip to content

proposal: net/url: add RelativeURLPath #22297

@rogpeppe

Description

@rogpeppe

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions