Skip to content
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

Relative URLs with unknown base #487

Closed
rictic opened this issue Feb 19, 2019 · 6 comments
Closed

Relative URLs with unknown base #487

rictic opened this issue Feb 19, 2019 · 6 comments

Comments

@rictic
Copy link

rictic commented Feb 19, 2019

I'm writing some code that transforms source code, specifically URL strings in HTML, CSS, and JS. These URLs may be relative (e.g. ./foo.js, /bar.css, ../search?query=cats) and my code won't have the context of the base URL that these URLs would be interpreted relative to at runtime.

I didn't see any public API that supports parsing or providing structured handling of this sort of URL. Is it a use case you'd be interested in supporting?

@SimonSapin
Copy link
Member

I think I would call this outside of the scope of this crate. The Url type always represents an absolute URL.

What transformation do you need to do on those URL strings?

@rictic
Copy link
Author

rictic commented Feb 25, 2019

The main operation I need right now is to resolve a url from a base. e.g. if we encountered the url ./foo.js inside ./public/docs.html I'd want to produce the URL ./public/foo.js. This is specifically for prototyping build-time tooling around the WIP import maps spec.

Correctly handling absolute URLs, absolute paths, query params and hashes, not to mention encoding makes for a decent amount of code and a lot of overlap with other url handling.

@SimonSapin
Copy link
Member

In terms of implementation I expect that dealing with .. path segments without a base URL is just different enough from with one that I expect that supporting either in the same code base would be non-trivial.

In terms of public API, I expect this new feature would be entirely separate from what’s already in the url crate. (The Url type would not be involved.)

For these reasons I think this would be better in a separate crate. Feel free to copy some of the code according to the license, though.

@KiChjang
Copy link
Contributor

KiChjang commented Feb 27, 2019

To me, this issue sounds like a confusion between URLs and URIs. My understanding is that the use case that @rictic described pertains to URIs, because they are relative paths without a base at compile-time. IIRC, URLs are explicitly required to have absolute paths.

Given the above, I'd suggest the author to look into using the http crate for URI parsing. See hyperium/http#73 for more information about the history behind http not including the url crate as one of its components, in particular this comment from @SimonSapin

@SimonSapin
Copy link
Member

I don’t believe that’s what the difference between URL and URI is. But I also don’t believe that distinction is useful at all, or relevant here.

@rictic
Copy link
Author

rictic commented Feb 27, 2019

Seems reasonable, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants