Skip to content

Commit

Permalink
fix: parse ipath-abempty from RFC 3987
Browse files Browse the repository at this point in the history
Merge pull request #80 from deltachat/link2xt/ipath-abempty
  • Loading branch information
farooqkz authored Jul 24, 2024
2 parents a991527 + 96f78f2 commit 13efc9b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/parser/link_url/parse_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,32 @@ fn get_correct_link(link: &str) -> Option<usize> {
None
}

fn parse_ipath_abempty(input: &str) -> IResult<&str, &str, CustomError<&str>> {
recognize(many0(tuple((char('/'), opt(take_while_ipchar1)))))(input)
}

#[test]
fn test_ipath_abempty() {
assert_eq!(parse_ipath_abempty("///foo/bar"), Ok(("", "///foo/bar")));
}

fn parse_ipath_absolute(input: &str) -> IResult<&str, &str, CustomError<&str>> {
recognize(tuple((
char('/'),
opt(tuple((
take_while_ipchar1,
many0(tuple((char('/'), opt(take_while_ipchar1)))),
))),
)))(input)
}

#[test]
fn test_ipath_absolute() {
assert_eq!(parse_ipath_absolute("/foo"), Ok(("", "/foo")));
assert_eq!(parse_ipath_absolute("/foo/bar"), Ok(("", "/foo/bar")));
assert_eq!(parse_ipath_absolute("/foo//bar"), Ok(("", "/foo//bar")));
}

// IRI links per RFC3987 and RFC3986
fn parse_iri(input: &str) -> IResult<&str, LinkDestination, CustomError<&str>> {
let input_ = <&str>::clone(&input);
Expand All @@ -345,13 +371,8 @@ fn parse_iri(input: &str) -> IResult<&str, LinkDestination, CustomError<&str>> {
// host is actually part of authority but we need it separately
// see iauthority function description for more information
let (input, path) = opt(alt((
recognize(tuple((
char('/'),
opt(tuple((
take_while_ipchar1,
many0(tuple((char('/'), opt(take_while_ipchar1)))),
))),
))), // ipath-absolute
parse_ipath_abempty,
parse_ipath_absolute,
recognize(tuple((
take_while_ipchar,
many0(tuple((char('/'), opt(take_while_ipchar1)))),
Expand Down
2 changes: 2 additions & 0 deletions tests/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ fn basic_parsing() {
"mailto:[email protected]?subject=hi&body=hello%20world",
"mailto:foö@ü.chat",
"ftp://test-test",
"https://www.openmandriva.org/en/news/article/openmandriva-rome-24-07-released",
"https://www.openmandriva.org///en/news/article/openmandriva-rome-24-07-released",
];

let test_cases_with_puny = vec!["https://ü.app#help", "http://münchen.de"];
Expand Down

0 comments on commit 13efc9b

Please sign in to comment.