From 3021cd9dd880c19bb44941db9d1e1d51b6367609 Mon Sep 17 00:00:00 2001 From: Yazad Daruvala Date: Fri, 23 Jun 2017 21:52:56 -0700 Subject: [PATCH] refactor(uri): make ByteStr an implementation detail of uri --- src/common/mod.rs | 3 +++ src/{http => common}/str.rs | 0 src/http/h1/parse.rs | 6 +++--- src/http/mod.rs | 15 --------------- src/lib.rs | 1 + src/uri.rs | 8 ++++---- 6 files changed, 11 insertions(+), 22 deletions(-) create mode 100644 src/common/mod.rs rename src/{http => common}/str.rs (100%) diff --git a/src/common/mod.rs b/src/common/mod.rs new file mode 100644 index 00000000..79506177 --- /dev/null +++ b/src/common/mod.rs @@ -0,0 +1,3 @@ +pub use self::str::ByteStr; + +mod str; diff --git a/src/http/str.rs b/src/common/str.rs similarity index 100% rename from src/http/str.rs rename to src/common/str.rs diff --git a/src/http/h1/parse.rs b/src/http/h1/parse.rs index d2195c0f..8f1cae8d 100644 --- a/src/http/h1/parse.rs +++ b/src/http/h1/parse.rs @@ -5,7 +5,7 @@ use httparse; use bytes::{BytesMut, Bytes}; use header::{self, Headers, ContentLength, TransferEncoding}; -use http::{ByteStr, MessageHead, RawStatus, Http1Transaction, ParseResult, ServerTransaction, ClientTransaction, RequestLine}; +use http::{MessageHead, RawStatus, Http1Transaction, ParseResult, ServerTransaction, ClientTransaction, RequestLine}; use http::h1::{Encoder, Decoder, date}; use method::Method; use status::StatusCode; @@ -54,10 +54,10 @@ impl Http1Transaction for ServerTransaction { let slice = buf.split_to(len).freeze(); let path = slice.slice(path.0, path.1); // path was found to be utf8 by httparse - let path = unsafe { ByteStr::from_utf8_unchecked(path) }; + let path = try!(unsafe { ::uri::from_utf8_unchecked(path) }); let subject = RequestLine( method, - try!(::uri::from_byte_str(path)), + path, ); headers.extend(HeadersAsBytesIter { diff --git a/src/http/mod.rs b/src/http/mod.rs index 5e4568d1..6f8644ce 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -15,7 +15,6 @@ use version::HttpVersion::{Http10, Http11}; pub use self::conn::{Conn, KeepAlive, KA}; pub use self::body::{Body, TokioBody}; pub use self::chunk::Chunk; -pub use self::str::ByteStr; mod body; mod chunk; @@ -23,23 +22,9 @@ mod conn; mod io; mod h1; //mod h2; -mod str; pub mod request; pub mod response; -/* -macro_rules! nonblocking { - ($e:expr) => ({ - match $e { - Ok(n) => Ok(Some(n)), - Err(e) => match e.kind() { - stdio::ErrorKind::WouldBlock => Ok(None), - _ => Err(e) - } - } - }); -} -*/ /// An Incoming Message head. Includes request/status line, and headers. #[derive(Clone, Debug, Default, PartialEq)] diff --git a/src/lib.rs b/src/lib.rs index 172d531d..02d2c48d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,6 +37,7 @@ extern crate unicase; extern crate test; +mod common; pub use uri::Uri; pub use client::Client; pub use error::{Result, Error}; diff --git a/src/uri.rs b/src/uri.rs index 89b3811c..0ac3ec16 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -2,8 +2,8 @@ use std::error::Error as StdError; use std::fmt::{Display, self}; use std::str::{self, FromStr}; -use http::ByteStr; -use bytes::{BufMut, BytesMut}; +use ::common::ByteStr; +use bytes::{BufMut, Bytes, BytesMut}; /// The Request-URI of a Request's StartLine. /// @@ -297,8 +297,8 @@ impl Display for Uri { } } -pub fn from_byte_str(s: ByteStr) -> Result { - Uri::new(s) +pub unsafe fn from_utf8_unchecked(slice: Bytes) -> Result { + Uri::new(ByteStr::from_utf8_unchecked(slice)) } pub fn scheme_and_authority(uri: &Uri) -> Option {