Skip to content

Commit

Permalink
Merge pull request #2 from iotaledger/feat/percent-encoded-chars-in-p…
Browse files Browse the repository at this point in the history
…ath-frag-query

Add support for percent encoded characters in did url components
  • Loading branch information
UMR1352 authored Dec 23, 2024
2 parents cf0fddd + 3e1772a commit a3afd0b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ impl Core {
loop {
match input.peek() {
Some('?') | Some('#') | None => break,
Some('%') => self
.parse_pct_enc_char(input)
.map(|_| ())
.ok_or(Error::InvalidPath)?,
Some(ch) if char_path(ch) => {}
_ => return Err(Error::InvalidPath),
}
Expand All @@ -316,6 +320,10 @@ impl Core {
loop {
match input.peek() {
Some('#') | None => break,
Some('%') => self
.parse_pct_enc_char(input)
.map(|_| ())
.ok_or(Error::InvalidPath)?,
Some(ch) if char_query(ch) => {}
_ => return Err(Error::InvalidQuery),
}
Expand All @@ -342,6 +350,10 @@ impl Core {
loop {
match input.peek() {
None => break,
Some('%') => self
.parse_pct_enc_char(input)
.map(|_| ())
.ok_or(Error::InvalidPath)?,
Some(ch) if char_fragment(ch) => {}
_ => return Err(Error::InvalidFragment),
}
Expand Down
4 changes: 2 additions & 2 deletions src/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,13 @@ mod resolution {
#[repr(transparent)]
pub struct Path<'a>(Cow<'a, str>);

impl<'a> Default for Path<'a> {
impl Default for Path<'_> {
fn default() -> Self {
Self::new()
}
}

impl<'a> Path<'a> {
impl Path<'_> {
pub const fn new() -> Self {
Self(Cow::Borrowed(""))
}
Expand Down
11 changes: 11 additions & 0 deletions tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,14 @@ fn test_parse_simple_fragment() {
assert_eq!(did.query(), None);
assert_eq!(did.fragment(), Some("public-key-1"));
}

#[test]
#[rustfmt::skip]
fn test_parse_percent_encoded_components() {
let did: DID = DID::parse("did:example:123456/p%3Ath?qu%3Ary#fr%3Agment").unwrap();
assert_eq!(did.method(), "example");
assert_eq!(did.method_id(), "123456");
assert_eq!(did.path(), "/p%3Ath");
assert_eq!(did.query(), Some("qu%3Ary"));
assert_eq!(did.fragment(), Some("fr%3Agment"));
}

0 comments on commit a3afd0b

Please sign in to comment.