fix(uri): make absolute-form uris always have a path
This commit is contained in:
		| @@ -30,6 +30,13 @@ impl ByteStr { | |||||||
|         unsafe { str::from_utf8_unchecked(self.0.as_ref()) } |         unsafe { str::from_utf8_unchecked(self.0.as_ref()) } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     pub fn insert(&mut self, idx: usize, ch: char) { | ||||||
|  |         let mut s = self.as_str().to_owned(); | ||||||
|  |         s.insert(idx, ch); | ||||||
|  |         let bytes = Bytes::from(s); | ||||||
|  |         self.0 = bytes; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     #[cfg(feature = "compat")] |     #[cfg(feature = "compat")] | ||||||
|     pub fn into_bytes(self) -> Bytes { |     pub fn into_bytes(self) -> Bytes { | ||||||
|         self.0 |         self.0 | ||||||
|   | |||||||
							
								
								
									
										24
									
								
								src/uri.rs
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								src/uri.rs
									
									
									
									
									
								
							| @@ -42,7 +42,7 @@ pub struct Uri { | |||||||
|  |  | ||||||
| impl Uri { | impl Uri { | ||||||
|     /// Parse a string into a `Uri`. |     /// Parse a string into a `Uri`. | ||||||
|     fn new(s: ByteStr) -> Result<Uri, UriError> { |     fn new(mut s: ByteStr) -> Result<Uri, UriError> { | ||||||
|         if s.len() == 0 { |         if s.len() == 0 { | ||||||
|             Err(UriError(ErrorKind::Empty)) |             Err(UriError(ErrorKind::Empty)) | ||||||
|         } else if s.as_bytes() == b"*" { |         } else if s.as_bytes() == b"*" { | ||||||
| @@ -81,8 +81,19 @@ impl Uri { | |||||||
|                     return Err(UriError(ErrorKind::Malformed)); |                     return Err(UriError(ErrorKind::Malformed)); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |             // absolute-form must always have a path | ||||||
|  |             // if there isn't a '/', for consistency, add one. | ||||||
|  |             let slash = auth_end; | ||||||
|  |             if s.len() == slash { | ||||||
|  |                 s.insert(slash, '/'); | ||||||
|  |             } else if s.as_bytes()[slash] != b'/' { | ||||||
|  |                 s.insert(slash, '/'); | ||||||
|  |             } | ||||||
|  |  | ||||||
|             let query = parse_query(&s); |             let query = parse_query(&s); | ||||||
|             let fragment = parse_fragment(&s); |             let fragment = parse_fragment(&s); | ||||||
|  |  | ||||||
|             Ok(Uri { |             Ok(Uri { | ||||||
|                 source: s, |                 source: s, | ||||||
|                 scheme_end: scheme, |                 scheme_end: scheme, | ||||||
| @@ -443,7 +454,6 @@ macro_rules! test_parse { | |||||||
|         #[test] |         #[test] | ||||||
|         fn $test_name() { |         fn $test_name() { | ||||||
|             let uri = Uri::from_str($str).unwrap(); |             let uri = Uri::from_str($str).unwrap(); | ||||||
|             println!("{:?} = {:#?}", $str, uri); |  | ||||||
|             $( |             $( | ||||||
|             assert_eq!(uri.$method(), $value, stringify!($method)); |             assert_eq!(uri.$method(), $value, stringify!($method)); | ||||||
|             )+ |             )+ | ||||||
| @@ -486,6 +496,8 @@ test_parse! { | |||||||
|     query = None, |     query = None, | ||||||
|     fragment = None, |     fragment = None, | ||||||
|     port = Some(61761), |     port = Some(61761), | ||||||
|  |  | ||||||
|  |     to_string = "https://127.0.0.1:61761/", | ||||||
| } | } | ||||||
|  |  | ||||||
| test_parse! { | test_parse! { | ||||||
| @@ -497,6 +509,8 @@ test_parse! { | |||||||
|     path = "*", |     path = "*", | ||||||
|     query = None, |     query = None, | ||||||
|     fragment = None, |     fragment = None, | ||||||
|  |  | ||||||
|  |     to_string = "*", | ||||||
| } | } | ||||||
|  |  | ||||||
| test_parse! { | test_parse! { | ||||||
| @@ -510,6 +524,8 @@ test_parse! { | |||||||
|     query = None, |     query = None, | ||||||
|     fragment = None, |     fragment = None, | ||||||
|     port = None, |     port = None, | ||||||
|  |  | ||||||
|  |     to_string = "localhost", | ||||||
| } | } | ||||||
|  |  | ||||||
| test_parse! { | test_parse! { | ||||||
| @@ -627,6 +643,8 @@ test_parse! { | |||||||
|     query = Some("foo=bar"), |     query = Some("foo=bar"), | ||||||
|     fragment = None, |     fragment = None, | ||||||
|     port = None, |     port = None, | ||||||
|  |  | ||||||
|  |     to_string = "http://127.0.0.1/?foo=bar", | ||||||
| } | } | ||||||
|  |  | ||||||
| test_parse! { | test_parse! { | ||||||
| @@ -651,6 +669,8 @@ test_parse! { | |||||||
|     query = None, |     query = None, | ||||||
|     fragment = Some("foo?bar"), |     fragment = Some("foo?bar"), | ||||||
|     port = None, |     port = None, | ||||||
|  |  | ||||||
|  |     to_string = "http://127.0.0.1/#foo?bar", | ||||||
| } | } | ||||||
|  |  | ||||||
| #[test] | #[test] | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user