-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Percent-decode URLs in canonical comparisons #11088
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,60 +1,92 @@ | ||
| use std::hash::Hash; | ||
|
|
||
| use uv_cache_key::CanonicalUrl; | ||
| use uv_normalize::PackageName; | ||
| use uv_pep440::Version; | ||
|
|
||
| use crate::cached::CachedDist; | ||
| use crate::installed::InstalledDist; | ||
| use crate::{InstalledMetadata, InstalledVersion, Name}; | ||
|
|
||
| /// A distribution which is either installable, is a wheel in our cache or is already installed. | ||
| /// | ||
| /// Note equality and hash operations are only based on the name and version, not the kind. | ||
| /// Note equality and hash operations are only based on the name and canonical version, not the | ||
| /// kind. | ||
| #[derive(Debug, Clone, Eq)] | ||
| #[allow(clippy::large_enum_variant)] | ||
| pub enum LocalDist { | ||
| Cached(CachedDist), | ||
| Installed(InstalledDist), | ||
| Cached(CachedDist, CanonicalVersion), | ||
| Installed(InstalledDist, CanonicalVersion), | ||
| } | ||
|
|
||
| impl LocalDist { | ||
| fn canonical_version(&self) -> &CanonicalVersion { | ||
| match self { | ||
| Self::Cached(_, version) => version, | ||
| Self::Installed(_, version) => version, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Name for LocalDist { | ||
| fn name(&self) -> &PackageName { | ||
| match self { | ||
| Self::Cached(dist) => dist.name(), | ||
| Self::Installed(dist) => dist.name(), | ||
| Self::Cached(dist, _) => dist.name(), | ||
| Self::Installed(dist, _) => dist.name(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl InstalledMetadata for LocalDist { | ||
| fn installed_version(&self) -> InstalledVersion { | ||
| match self { | ||
| Self::Cached(dist) => dist.installed_version(), | ||
| Self::Installed(dist) => dist.installed_version(), | ||
| Self::Cached(dist, _) => dist.installed_version(), | ||
| Self::Installed(dist, _) => dist.installed_version(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<CachedDist> for LocalDist { | ||
| fn from(dist: CachedDist) -> Self { | ||
| Self::Cached(dist) | ||
| let version = CanonicalVersion::from(dist.installed_version()); | ||
| Self::Cached(dist, version) | ||
| } | ||
| } | ||
|
|
||
| impl From<InstalledDist> for LocalDist { | ||
| fn from(dist: InstalledDist) -> Self { | ||
| Self::Installed(dist) | ||
| let version = CanonicalVersion::from(dist.installed_version()); | ||
| Self::Installed(dist, version) | ||
| } | ||
| } | ||
|
|
||
| impl Hash for LocalDist { | ||
| fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
| self.name().hash(state); | ||
| self.installed_version().hash(state); | ||
| self.canonical_version().hash(state); | ||
| } | ||
| } | ||
|
|
||
| impl PartialEq for LocalDist { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| self.name() == other.name() && self.installed_version() == other.installed_version() | ||
| self.name() == other.name() && self.canonical_version() == other.canonical_version() | ||
| } | ||
| } | ||
|
|
||
| /// Like [`InstalledVersion`], but with [`CanonicalUrl`] to ensure robust URL comparisons. | ||
| #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
| pub enum CanonicalVersion { | ||
| Version(Version), | ||
| Url(CanonicalUrl, Version), | ||
| } | ||
|
|
||
| impl From<InstalledVersion<'_>> for CanonicalVersion { | ||
| fn from(installed_version: InstalledVersion<'_>) -> Self { | ||
| match installed_version { | ||
| InstalledVersion::Version(version) => Self::Version(version.clone()), | ||
| InstalledVersion::Url(url, version) => { | ||
| Self::Url(CanonicalUrl::new(url), version.clone()) | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this actually might be wrong. What if a slash is percent-encoded? We'd be changing it to a path segment.
I guess we could go the other way -- always percent-encode the URL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But that's also not quite right... Because if the URL is already percent-encoded, we'd be re-encoding it. It's not idempotent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we want to percent-decode each path segment, but not slashes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'm actually not totally sure how to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried figuring this out with https://url.spec.whatwg.org but it's still unclear how to handle file urls vs. https urls. If we're eager there is a complete decoding algorithm there though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the segment contains a percent-encoded slash, though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, we could also consider just decoding
+, if it's "special" (or something like that).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
urlcrate helps you here: https://docs.rs/url/latest/url/struct.PathSegmentsMut.html#method.extendThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from the spec it also seems that file:// and https:// have different rules, so we should test that our solution works for both