Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/purl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const ENCODE_SET: &AsciiSet = &percent_encoding::CONTROLS
.add(b'?')
.add(b'{')
.add(b'}')
// .add(b'/')
// .add(b':')
.add(b';')
.add(b'=')
.add(b'+')
Expand All @@ -38,6 +36,8 @@ const ENCODE_SET: &AsciiSet = &percent_encoding::CONTROLS
.add(b'^')
.add(b'|');

const NAME_ENCODE_SET: &AsciiSet = &ENCODE_SET.add(b'/');

/// A Package URL.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -301,13 +301,13 @@ impl Display for PackageUrl<'_> {

// Namespace: percent-encode each component
if let Some(ref ns) = self.namespace {
for component in ns.split('/').map(|s| s.encode(ENCODE_SET)) {
for component in ns.split('/').map(|s| s.encode(NAME_ENCODE_SET)) {
component.fmt(f).and(f.write_str("/"))?;
}
}

// Name: percent-encode the name
self.name.encode(ENCODE_SET).fmt(f)?;
self.name.encode(NAME_ENCODE_SET).fmt(f)?;

// Version: percent-encode the version
if let Some(ref v) = self.version {
Expand Down Expand Up @@ -393,11 +393,20 @@ mod tests {

#[test]
fn test_percent_encoding_idempotent() {
let orig = "pkg:brew/openssl%[email protected]";
let orig = "pkg:brew/open%2Fssl%[email protected]";
let round_trip = orig.parse::<PackageUrl>().unwrap().to_string();
assert_eq!(orig, round_trip);
}

#[test]
fn test_percent_encoded_name() {
let raw_purl = "pkg:type/name/space/first%2Fname";
let purl = PackageUrl::from_str(raw_purl).unwrap();
assert_eq!(purl.ty(), "type");
assert_eq!(purl.namespace(), Some("name/space"));
assert_eq!(purl.name(), "first/name");
}

#[test]
fn test_percent_encoding_qualifier() {
let mut purl = "pkg:deb/ubuntu/gnome-calculator@1:41.1-2ubuntu2"
Expand Down
Loading