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

Use scheme parsing to determine absolute vs. relative URLs #2904

Merged
merged 1 commit into from
Apr 8, 2024

Conversation

charliermarsh
Copy link
Member

Summary

We have a heuristic in File that attempts to detect whether a URL is absolute or relative. However, contains("://") is prone to false positive. In the linked issues, the URLs look like:

/packages/5a/d8/4d75d1e4287ad9d051aab793c68f902c9c55c4397636b5ee540ebd15aedf/pytz-2005k.tar.bz2?hash=597b596dc1c2c130cd0a57a043459c3bd6477c640c07ac34ca3ce8eed7e6f30c&remote=https://files.pythonhosted.org/packages/5a/d8/4d75d1e4287ad9d051aab793c68f902c9c55c4397636b5ee540ebd15aedf/pytz-2005k.tar.bz2#sha256=597b596dc1c2c130cd0a57a043459c3bd6477c640c07ac34ca3ce8eed7e6f30c

Which is relative, but includes ://.

Instead, we should determine whether the URL has a scheme which matches the Url crate internally.

Closes #2899.

@charliermarsh charliermarsh marked this pull request as ready for review April 8, 2024 17:28
@charliermarsh charliermarsh added the bug Something isn't working label Apr 8, 2024
///
/// This is useful for parsing URLs that may be absolute or relative, with a
/// known base URL, and that doesn't require having already parsed a `BaseUrl`.
pub fn base_url_join_relative(base: &str, maybe_relative: &str) -> Result<Url, JoinRelativeError> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the try behavior is necessary here. url::ParseError::RelativeUrlWithoutBase occurs if and only if the URL has no scheme... And we already verify that FileLocation::RelativeUrl is created iff the URL has no scheme.

@charliermarsh charliermarsh merged commit cc3c570 into main Apr 8, 2024
35 checks passed
@charliermarsh charliermarsh deleted the charlie/scheme branch April 8, 2024 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

failing to install a package from private pypi repo - Caused by: relative URL without a base
2 participants