New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: net/url: add RelativeURLPath #22297
Comments
I assume your existing implementation is at https://github.com/juju/httputil/blob/master/relativeurl.go? Also, since this func only takes paths, have you considered the |
@mvdan I'm fairly sure that this wouldn't be useful in any context other than HTTP. The logic is peculiar to the specific semantics used by relative URLs. For example, a trailing slash is significant. |
ResolveReference takes two *URL and returns a *URL. url.Relative seems OK to me. |
Or if it's two *url.URLs as arguments, it could be a method named "Sub" like https://golang.org/pkg/time/#Time.Sub |
We have
We could add
or
It's unclear to me which people would expect. Filepath has
|
Does anyone want to make a case for why this is important? We often want relative file paths, for example to display to users, but it's pretty rare to insist on relative URLs. Pretty much all URL uses are fine as absolute URLs. |
We needed this because we have some HTTP services that are behind an apache gateway that rewrites URL paths to add a prefix (so we have several hosted services under the same virtual host). The problem is that it's not possible for a service to form a link to an absolute path within that service, because that might be wrong (it's also possible to reach the service without going through the apache proxy). So we need to use relative links - hence this function (which was non-trivial to get right FWIW). |
Thanks for the context @rogpeppe. For now it seems like this would be best outside the standard library - the need seems very specific and rare. |
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:
The text was updated successfully, but these errors were encountered: