Skip to content

Commit

Permalink
Support par-encoded character
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 committed Feb 12, 2024
1 parent f1b5ce9 commit 985e617
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ include = ["src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
readme = "README.md"

[dependencies]
form_urlencoded = { version = "1.0", default-features = false }
form_urlencoded = { version = "1.0", default-features = false, features = ["alloc"] }
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"], optional = true }

[dev-dependencies]
Expand Down
15 changes: 15 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ impl Core {
loop {
match input.peek() {
Some('/') | Some('?') | Some('#') | None => break,
Some('%') => self
.parse_pct_enc_char(input)
.map(|_| ())
.ok_or(Error::InvalidMethodId)?,
Some(ch) if char_method_id(ch) => {}
_ => return Err(Error::InvalidMethodId),
}
Expand All @@ -268,6 +272,17 @@ impl Core {
Ok(())
}

fn parse_pct_enc_char(&mut self, input: &mut Input) -> Option<char> {
if input.next() != Some('%') {
return None;
}

let hex_value = input.take(2)?;
u8::from_str_radix(hex_value, 16)
.ok()
.map(|ascii| ascii as char)
}

fn parse_path(&mut self, input: &mut Input) -> Result<()> {
self.path = input.index();

Expand Down
2 changes: 2 additions & 0 deletions tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn test_parse_valid_method_id() {
assert!(DID::parse("did:method:identifier").is_ok());
assert!(DID::parse("did:method:IDENTIFIER").is_ok());
assert!(DID::parse("did:method:did:__:--:1231093213").is_ok());
assert!(DID::parse("did:web:user%40localhost%3A12345").is_ok());
}

#[test]
Expand All @@ -35,6 +36,7 @@ fn test_parse_invalid_method_id() {
assert!(DID::parse("did:method: - - -").is_err());
assert!(DID::parse("did:method:*****").is_err());
assert!(DID::parse("did:method:identifier-|$|").is_err());
assert!(DID::parse("did:web:user%localhost%12345").is_err());
}

#[test]
Expand Down

0 comments on commit 985e617

Please sign in to comment.