From 5d3499e036721c3cb495740513917dd59e09899b Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 5 Apr 2017 13:50:29 -0700 Subject: [PATCH] fix(uri): fix Uri to origin_form when path is '*' --- src/http/str.rs | 15 +-------------- src/uri.rs | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/http/str.rs b/src/http/str.rs index 6fd22364..4479a01b 100644 --- a/src/http/str.rs +++ b/src/http/str.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use std::str; -use bytes::{Bytes, BytesMut}; +use bytes::Bytes; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ByteStr(Bytes); @@ -38,19 +38,6 @@ impl Deref for ByteStr { } } - -impl From for Bytes { - fn from(s: ByteStr) -> Bytes { - s.0 - } -} - -impl From for BytesMut { - fn from(s: ByteStr) -> BytesMut { - s.0.into() - } -} - impl<'a> From<&'a str> for ByteStr { fn from(s: &'a str) -> ByteStr { ByteStr(Bytes::from(s)) diff --git a/src/uri.rs b/src/uri.rs index e2b57c9d..c1f72736 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -44,13 +44,7 @@ impl Uri { Err(UriError(ErrorKind::Empty)) } else if s.as_bytes() == b"*" { // asterisk-form - Ok(Uri { - source: ByteStr::from_static("*"), - scheme_end: None, - authority_end: None, - query_start: None, - fragment_start: None, - }) + Ok(asterisk_form()) } else if s.as_bytes() == b"/" { // shortcut for '/' Ok(Uri::default()) @@ -308,11 +302,24 @@ pub fn scheme_and_authority(uri: &Uri) -> Option { } } +#[inline] +fn asterisk_form() -> Uri { + Uri { + source: ByteStr::from_static("*"), + scheme_end: None, + authority_end: None, + query_start: None, + fragment_start: None, + } +} + pub fn origin_form(uri: &Uri) -> Uri { let range = Range(uri.path_start(), uri.origin_form_end()); let clone = if range.len() == 0 { ByteStr::from_static("/") + } else if uri.source.as_bytes()[range.0] == b'*' { + return asterisk_form(); } else if uri.source.as_bytes()[range.0] != b'/' { let mut new = BytesMut::with_capacity(range.1 - range.0 + 1); new.put_u8(b'/'); @@ -541,10 +548,11 @@ fn test_uri_to_origin_form() { ("http://hyper.rs/", "/"), ("http://hyper.rs/path", "/path"), ("http://hyper.rs?query", "/?query"), + ("*", "*"), ]; for case in cases { let uri = Uri::from_str(case.0).unwrap(); - assert_eq!(origin_form(&uri), case.1); + assert_eq!(origin_form(&uri), case.1, "{:?}", case); } }