Make hashes optional (#910)

There is no guarantee that indexes provide hashes at all or the sha256
we support specifically. [PEP
503](https://peps.python.org/pep-0503/#specification):

> The URL SHOULD include a hash in the form of a URL fragment with the
following syntax: #<hashname>=<hashvalue>, where <hashname> is the
lowercase name of the hash function (such as sha256) and <hashvalue> is
the hex encoded digest.

We instead use the url as input to generate a hash when caching.
This commit is contained in:
konsti 2024-01-14 22:32:55 +01:00 committed by GitHub
parent 9ad19b7e54
commit 5ffbfadf66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 104 additions and 41 deletions

View file

@ -85,14 +85,18 @@ impl Yanked {
///
/// PEP 691 says multiple hashes can be included and the interpretation is left to the client, we
/// only support SHA 256 atm.
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Default, Serialize, Deserialize)]
pub struct Hashes {
// TODO(charlie): Hashes should be optional.
pub sha256: String,
pub sha256: Option<String>,
}
impl std::fmt::Display for Hashes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "sha256:{}", self.sha256)
impl Hashes {
/// Format as `<algorithm>:<hash>`.
///
/// Currently limited to SHA256.
pub fn to_string(&self) -> Option<String> {
self.sha256
.as_ref()
.map(|sha256| format!("sha256:{sha256}"))
}
}