From 559f21a664275a4cebcbe294fc843ab8ca325fed Mon Sep 17 00:00:00 2001 From: Utkarsh Kukreti Date: Wed, 17 May 2017 17:09:20 +0530 Subject: [PATCH] fix(uri): fix parse for empty path and '/' or '?' in fragment Fixes #1176. --- src/uri.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/uri.rs b/src/uri.rs index d56ad74f..7e13f587 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -213,9 +213,8 @@ fn parse_scheme(s: &str) -> Option { fn parse_authority(s: &str) -> usize { let i = s.find("://").map(|p| p + 3).unwrap_or(0); - s[i..].find('/') - .or_else(|| s[i..].find('?')) - .or_else(|| s[i..].find('#')) + s[i..] + .find(|ch| ch == '/' || ch == '?' || ch == '#') .map(|end| end + i) .unwrap_or(s.len()) } @@ -537,6 +536,28 @@ test_parse! { port = None, } +test_parse! { + test_uri_parse_absolute_form_with_empty_path_and_fragment_with_slash, + "http://127.0.0.1#foo/bar", + scheme = Some("http"), + authority = Some("127.0.0.1"), + path = "/", + query = None, + fragment = Some("foo/bar"), + port = None, +} + +test_parse! { + test_uri_parse_absolute_form_with_empty_path_and_fragment_with_questionmark, + "http://127.0.0.1#foo?bar", + scheme = Some("http"), + authority = Some("127.0.0.1"), + path = "/", + query = None, + fragment = Some("foo?bar"), + port = None, +} + #[test] fn test_uri_parse_error() { fn err(s: &str) {