diff --git a/src/header/name.rs b/src/header/name.rs index ec2c4434..b9cfb106 100644 --- a/src/header/name.rs +++ b/src/header/name.rs @@ -122,14 +122,14 @@ macro_rules! standard_headers { // Test lower case let name_bytes = name.as_bytes(); let bytes: Bytes = - HeaderName::from_bytes(name_bytes).unwrap().into(); + HeaderName::from_bytes(name_bytes).unwrap().inner.into(); assert_eq!(bytes, name_bytes); assert_eq!(HeaderName::from_bytes(name_bytes).unwrap(), std); // Test upper case let upper = name.to_uppercase().to_string(); let bytes: Bytes = - HeaderName::from_bytes(upper.as_bytes()).unwrap().into(); + HeaderName::from_bytes(upper.as_bytes()).unwrap().inner.into(); assert_eq!(bytes, name.as_bytes()); assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(), std); @@ -1809,6 +1809,10 @@ impl HeaderName { Repr::Custom(ref v) => &*v.0, } } + + pub(super) fn into_bytes(self) -> Bytes { + self.inner.into() + } } impl FromStr for HeaderName { @@ -1881,13 +1885,6 @@ impl From for Bytes { } } -impl From for Bytes { - #[inline] - fn from(name: HeaderName) -> Bytes { - name.inner.into() - } -} - impl<'a> TryFrom<&'a str> for HeaderName { type Error = InvalidHeaderName; #[inline] @@ -1912,14 +1909,6 @@ impl<'a> TryFrom<&'a [u8]> for HeaderName { } } -impl TryFrom for HeaderName { - type Error = InvalidHeaderNameBytes; - #[inline] - fn try_from(bytes: Bytes) -> Result { - Self::from_bytes(bytes.as_ref()).map_err(InvalidHeaderNameBytes) - } -} - #[doc(hidden)] impl From for HeaderName { fn from(src: StandardHeader) -> HeaderName { diff --git a/src/header/value.rs b/src/header/value.rs index 7379e468..1a0276ed 100644 --- a/src/header/value.rs +++ b/src/header/value.rs @@ -161,10 +161,11 @@ impl HeaderValue { /// This function is intended to be replaced in the future by a `TryFrom` /// implementation once the trait is stabilized in std. #[inline] - pub fn from_shared(src: Bytes) -> Result { + fn from_shared(src: Bytes) -> Result { HeaderValue::try_from_generic(src, std::convert::identity).map_err(InvalidHeaderValueBytes) } + /* /// Convert a `Bytes` directly into a `HeaderValue` without validating. /// /// This function does NOT validate that illegal bytes are not contained @@ -187,6 +188,7 @@ impl HeaderValue { } } } + */ fn try_from_generic, F: FnOnce(T) -> Bytes>(src: T, into: F) -> Result { for &b in src.as_ref() { @@ -357,7 +359,7 @@ impl From for HeaderValue { #[inline] fn from(h: HeaderName) -> HeaderValue { HeaderValue { - inner: h.into(), + inner: h.into_bytes(), is_sensitive: false, } } @@ -475,13 +477,6 @@ impl FromStr for HeaderValue { } } -impl From for Bytes { - #[inline] - fn from(value: HeaderValue) -> Bytes { - value.inner - } -} - impl<'a> From<&'a HeaderValue> for HeaderValue { #[inline] fn from(t: &'a HeaderValue) -> Self { @@ -533,15 +528,6 @@ impl TryFrom> for HeaderValue { } } -impl TryFrom for HeaderValue { - type Error = InvalidHeaderValueBytes; - - #[inline] - fn try_from(bytes: Bytes) -> Result { - HeaderValue::from_shared(bytes) - } -} - #[cfg(test)] mod try_from_header_name_tests { use super::*; diff --git a/src/uri/authority.rs b/src/uri/authority.rs index 66087b2a..9d33d626 100644 --- a/src/uri/authority.rs +++ b/src/uri/authority.rs @@ -41,8 +41,16 @@ impl Authority { /// assert_eq!(authority.host(), "example.com"); /// # } /// ``` - pub fn from_shared(s: Bytes) -> Result { - TryFrom::try_from(s) + pub(super) fn from_shared(s: Bytes) -> Result { + let authority_end = Authority::parse_non_empty(&s[..]).map_err(InvalidUriBytes)?; + + if authority_end != s.len() { + return Err(ErrorKind::InvalidUriChar.into()); + } + + Ok(Authority { + data: unsafe { ByteStr::from_utf8_unchecked(s) }, + }) } /// Attempt to convert an `Authority` from a static string. @@ -257,14 +265,9 @@ impl Authority { pub fn as_str(&self) -> &str { &self.data[..] } - - /// Converts this `Authority` back to a sequence of bytes - #[inline] - pub fn into_bytes(self) -> Bytes { - self.into() - } } +/* impl TryFrom for Authority { type Error = InvalidUriBytes; /// Attempt to convert an `Authority` from `Bytes`. @@ -298,6 +301,7 @@ impl TryFrom for Authority { }) } } +*/ impl AsRef for Authority { fn as_ref(&self) -> &str { @@ -494,13 +498,6 @@ impl FromStr for Authority { } } -impl From for Bytes { - #[inline] - fn from(src: Authority) -> Bytes { - src.data.into() - } -} - impl fmt::Debug for Authority { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.as_str()) diff --git a/src/uri/mod.rs b/src/uri/mod.rs index 0d022c48..6d281d19 100644 --- a/src/uri/mod.rs +++ b/src/uri/mod.rs @@ -260,8 +260,54 @@ impl Uri { /// assert_eq!(uri.path(), "/foo"); /// # } /// ``` - pub fn from_shared(s: Bytes) -> Result { - TryFrom::try_from(s) + fn from_shared(s: Bytes) -> Result { + use self::ErrorKind::*; + + if s.len() > MAX_LEN { + return Err(TooLong.into()); + } + + match s.len() { + 0 => { + return Err(Empty.into()); + } + 1 => match s[0] { + b'/' => { + return Ok(Uri { + scheme: Scheme::empty(), + authority: Authority::empty(), + path_and_query: PathAndQuery::slash(), + }); + } + b'*' => { + return Ok(Uri { + scheme: Scheme::empty(), + authority: Authority::empty(), + path_and_query: PathAndQuery::star(), + }); + } + _ => { + let authority = Authority::from_shared(s)?; + + return Ok(Uri { + scheme: Scheme::empty(), + authority: authority, + path_and_query: PathAndQuery::empty(), + }); + } + }, + _ => {} + } + + if s[0] == b'/' { + return Ok(Uri { + scheme: Scheme::empty(), + authority: Authority::empty(), + path_and_query: PathAndQuery::from_shared(s)?, + }); + } + + parse_full(s) } /// Convert a `Uri` from a static string. @@ -632,80 +678,6 @@ impl Uri { } } -impl TryFrom for Uri { - type Error = InvalidUriBytes; - - /// Attempt to convert a `Uri` from `Bytes` - /// - /// # Examples - /// - /// ``` - /// # extern crate http; - /// # use http::uri::*; - /// extern crate bytes; - /// - /// use std::convert::TryFrom; - /// use bytes::Bytes; - /// - /// # pub fn main() { - /// let bytes = Bytes::from("http://example.com/foo"); - /// let uri = Uri::try_from(bytes).unwrap(); - /// - /// assert_eq!(uri.host().unwrap(), "example.com"); - /// assert_eq!(uri.path(), "/foo"); - /// # } - /// ``` - fn try_from(s: Bytes) -> Result { - use self::ErrorKind::*; - - if s.len() > MAX_LEN { - return Err(TooLong.into()); - } - - match s.len() { - 0 => { - return Err(Empty.into()); - } - 1 => match s[0] { - b'/' => { - return Ok(Uri { - scheme: Scheme::empty(), - authority: Authority::empty(), - path_and_query: PathAndQuery::slash(), - }); - } - b'*' => { - return Ok(Uri { - scheme: Scheme::empty(), - authority: Authority::empty(), - path_and_query: PathAndQuery::star(), - }); - } - _ => { - let authority = Authority::from_shared(s)?; - - return Ok(Uri { - scheme: Scheme::empty(), - authority: authority, - path_and_query: PathAndQuery::empty(), - }); - } - }, - _ => {} - } - - if s[0] == b'/' { - return Ok(Uri { - scheme: Scheme::empty(), - authority: Authority::empty(), - path_and_query: PathAndQuery::from_shared(s)?, - }); - } - - parse_full(s) - } -} - impl<'a> TryFrom<&'a str> for Uri { type Error = InvalidUri; diff --git a/src/uri/path.rs b/src/uri/path.rs index 851db5b7..dfefc113 100644 --- a/src/uri/path.rs +++ b/src/uri/path.rs @@ -39,7 +39,7 @@ impl PathAndQuery { /// assert_eq!(path_and_query.query(), Some("world")); /// # } /// ``` - pub fn from_shared(mut src: Bytes) -> Result { + pub(super) fn from_shared(mut src: Bytes) -> Result { let mut query = NONE; let mut fragment = None; @@ -275,20 +275,6 @@ impl PathAndQuery { } ret } - - /// Converts this `PathAndQuery` back to a sequence of bytes - #[inline] - pub fn into_bytes(self) -> Bytes { - self.into() - } -} - -impl TryFrom for PathAndQuery { - type Error = InvalidUriBytes; - #[inline] - fn try_from(bytes: Bytes) -> Result { - PathAndQuery::from_shared(bytes) - } } impl<'a> TryFrom<&'a [u8]> for PathAndQuery { @@ -315,12 +301,6 @@ impl FromStr for PathAndQuery { } } -impl From for Bytes { - fn from(src: PathAndQuery) -> Bytes { - src.data.into() - } -} - impl fmt::Debug for PathAndQuery { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(self, f) diff --git a/src/uri/scheme.rs b/src/uri/scheme.rs index f8f32ae0..5bbab11e 100644 --- a/src/uri/scheme.rs +++ b/src/uri/scheme.rs @@ -5,7 +5,7 @@ use std::str::FromStr; use bytes::Bytes; -use super::{ErrorKind, InvalidUri, InvalidUriBytes}; +use super::{ErrorKind, InvalidUri}; use crate::byte_str::ByteStr; /// Represents the scheme component of a URI @@ -38,30 +38,6 @@ impl Scheme { inner: Scheme2::Standard(Protocol::Https), }; - /// Attempt to convert a `Scheme` from `Bytes` - /// - /// This function has been replaced by `TryFrom` implementation - /// - /// # Examples - /// - /// ``` - /// # extern crate http; - /// # use http::uri::*; - /// extern crate bytes; - /// - /// use bytes::Bytes; - /// - /// # pub fn main() { - /// let bytes = Bytes::from("http"); - /// let scheme = Scheme::from_shared(bytes).unwrap(); - /// - /// assert_eq!(scheme.as_str(), "http"); - /// # } - /// ``` - pub fn from_shared(s: Bytes) -> Result { - TryFrom::try_from(s) - } - pub(super) fn empty() -> Self { Scheme { inner: Scheme2::None, @@ -89,48 +65,6 @@ impl Scheme { None => unreachable!(), } } - - /// Converts this `Scheme` back to a sequence of bytes - #[inline] - pub fn into_bytes(self) -> Bytes { - self.into() - } -} - -impl TryFrom for Scheme { - type Error = InvalidUriBytes; - - /// Attempt to convert a `Scheme` from `Bytes` - /// - /// # Examples - /// - /// ``` - /// # extern crate http; - /// # use http::uri::*; - /// extern crate bytes; - /// - /// use std::convert::TryFrom; - /// use bytes::Bytes; - /// - /// # pub fn main() { - /// let bytes = Bytes::from("http"); - /// let scheme = Scheme::try_from(bytes).unwrap(); - /// - /// assert_eq!(scheme.as_str(), "http"); - /// # } - /// ``` - fn try_from(s: Bytes) -> Result { - use self::Scheme2::*; - - match Scheme2::parse_exact(&s[..]).map_err(InvalidUriBytes)? { - None => Err(ErrorKind::InvalidScheme.into()), - Standard(p) => Ok(Standard(p).into()), - Other(_) => { - let b = unsafe { ByteStr::from_utf8_unchecked(s) }; - Ok(Other(Box::new(b)).into()) - } - } - } } impl<'a> TryFrom<&'a [u8]> for Scheme { @@ -168,21 +102,6 @@ impl FromStr for Scheme { } } -impl From for Bytes { - #[inline] - fn from(src: Scheme) -> Self { - use self::Protocol::*; - use self::Scheme2::*; - - match src.inner { - None => Bytes::new(), - Standard(Http) => Bytes::from_static(b"http"), - Standard(Https) => Bytes::from_static(b"https"), - Other(v) => (*v).into(), - } - } -} - impl fmt::Debug for Scheme { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(self.as_str(), f)