fix(uri): fix Uri to origin_form when path is '*'

This commit is contained in:
Sean McArthur
2017-04-05 13:50:29 -07:00
parent cb1927553e
commit 5d3499e036
2 changed files with 17 additions and 22 deletions

View File

@@ -1,7 +1,7 @@
use std::ops::Deref; use std::ops::Deref;
use std::str; use std::str;
use bytes::{Bytes, BytesMut}; use bytes::Bytes;
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ByteStr(Bytes); pub struct ByteStr(Bytes);
@@ -38,19 +38,6 @@ impl Deref for ByteStr {
} }
} }
impl From<ByteStr> for Bytes {
fn from(s: ByteStr) -> Bytes {
s.0
}
}
impl From<ByteStr> for BytesMut {
fn from(s: ByteStr) -> BytesMut {
s.0.into()
}
}
impl<'a> From<&'a str> for ByteStr { impl<'a> From<&'a str> for ByteStr {
fn from(s: &'a str) -> ByteStr { fn from(s: &'a str) -> ByteStr {
ByteStr(Bytes::from(s)) ByteStr(Bytes::from(s))

View File

@@ -44,13 +44,7 @@ impl Uri {
Err(UriError(ErrorKind::Empty)) Err(UriError(ErrorKind::Empty))
} else if s.as_bytes() == b"*" { } else if s.as_bytes() == b"*" {
// asterisk-form // asterisk-form
Ok(Uri { Ok(asterisk_form())
source: ByteStr::from_static("*"),
scheme_end: None,
authority_end: None,
query_start: None,
fragment_start: None,
})
} else if s.as_bytes() == b"/" { } else if s.as_bytes() == b"/" {
// shortcut for '/' // shortcut for '/'
Ok(Uri::default()) Ok(Uri::default())
@@ -308,11 +302,24 @@ pub fn scheme_and_authority(uri: &Uri) -> Option<Uri> {
} }
} }
#[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 { pub fn origin_form(uri: &Uri) -> Uri {
let range = Range(uri.path_start(), uri.origin_form_end()); let range = Range(uri.path_start(), uri.origin_form_end());
let clone = if range.len() == 0 { let clone = if range.len() == 0 {
ByteStr::from_static("/") ByteStr::from_static("/")
} else if uri.source.as_bytes()[range.0] == b'*' {
return asterisk_form();
} else if uri.source.as_bytes()[range.0] != b'/' { } else if uri.source.as_bytes()[range.0] != b'/' {
let mut new = BytesMut::with_capacity(range.1 - range.0 + 1); let mut new = BytesMut::with_capacity(range.1 - range.0 + 1);
new.put_u8(b'/'); new.put_u8(b'/');
@@ -541,10 +548,11 @@ fn test_uri_to_origin_form() {
("http://hyper.rs/", "/"), ("http://hyper.rs/", "/"),
("http://hyper.rs/path", "/path"), ("http://hyper.rs/path", "/path"),
("http://hyper.rs?query", "/?query"), ("http://hyper.rs?query", "/?query"),
("*", "*"),
]; ];
for case in cases { for case in cases {
let uri = Uri::from_str(case.0).unwrap(); let uri = Uri::from_str(case.0).unwrap();
assert_eq!(origin_form(&uri), case.1); assert_eq!(origin_form(&uri), case.1, "{:?}", case);
} }
} }