Remove all Hyper* types in favor of hyper::*.

This commit is contained in:
Sergio Benitez 2016-12-15 09:24:29 -08:00
parent 0cc379b82f
commit d3e2d829c7
12 changed files with 75 additions and 80 deletions

View File

@ -7,9 +7,14 @@ use std::mem::transmute;
use super::data_stream::{DataStream, StreamReader, kill_stream}; use super::data_stream::{DataStream, StreamReader, kill_stream};
use ext::ReadExt; use ext::ReadExt;
use http::hyper::{HyperBodyReader, HyperHttpStream};
use http::hyper::HyperNetworkStream; use http::hyper::h1::HttpReader;
use http::hyper::HyperHttpReader::*; use http::hyper::buffer;
use http::hyper::h1::HttpReader::*;
use http::hyper::net::{HttpStream, NetworkStream};
pub type BodyReader<'a, 'b> =
self::HttpReader<&'a mut self::buffer::BufReader<&'b mut NetworkStream>>;
/// Type representing the data in the body of an incoming request. /// Type representing the data in the body of an incoming request.
/// ///
@ -76,12 +81,12 @@ impl Data {
} }
#[doc(hidden)] #[doc(hidden)]
pub fn from_hyp(mut h_body: HyperBodyReader) -> Result<Data, &'static str> { pub fn from_hyp(mut h_body: BodyReader) -> Result<Data, &'static str> {
// FIXME: This is asolutely terrible, thanks to Hyper. // FIXME: This is asolutely terrible, thanks to Hyper.
// Retrieve the underlying HTTPStream from Hyper. // Retrieve the underlying HTTPStream from Hyper.
let mut stream = match h_body.get_ref().get_ref() let mut stream = match h_body.get_ref().get_ref()
.downcast_ref::<HyperHttpStream>() { .downcast_ref::<HttpStream>() {
Some(s) => { Some(s) => {
let owned_stream = s.clone(); let owned_stream = s.clone();
let buf_len = h_body.get_ref().get_buf().len() as u64; let buf_len = h_body.get_ref().get_buf().len() as u64;

View File

@ -1,15 +1,15 @@
use std::io::{self, BufRead, Read, Cursor, BufReader, Chain, Take}; use std::io::{self, BufRead, Read, Cursor, BufReader, Chain, Take};
use std::net::Shutdown; use std::net::Shutdown;
use http::hyper::{HyperHttpStream, HyperHttpReader}; use http::hyper::net::{HttpStream, NetworkStream};
use http::hyper::HyperNetworkStream; use http::hyper::h1::HttpReader;
pub type StreamReader = HyperHttpReader<HyperHttpStream>; pub type StreamReader = HttpReader<HttpStream>;
pub type InnerStream = Chain<Take<Cursor<Vec<u8>>>, BufReader<StreamReader>>; pub type InnerStream = Chain<Take<Cursor<Vec<u8>>>, BufReader<StreamReader>>;
pub struct DataStream { pub struct DataStream {
pub stream: InnerStream, pub stream: InnerStream,
pub network: HyperHttpStream, pub network: HttpStream,
} }
impl Read for DataStream { impl Read for DataStream {
@ -28,7 +28,7 @@ impl BufRead for DataStream {
} }
} }
pub fn kill_stream<S: Read, N: HyperNetworkStream>(stream: &mut S, network: &mut N) { pub fn kill_stream<S: Read, N: NetworkStream>(stream: &mut S, network: &mut N) {
io::copy(&mut stream.take(1024), &mut io::sink()).expect("sink"); io::copy(&mut stream.take(1024), &mut io::sink()).expect("sink");
// If there are any more bytes, kill it. // If there are any more bytes, kill it.

View File

@ -2,7 +2,12 @@ use std::io::{self, BufRead, Write, Cursor, BufReader};
use std::path::Path; use std::path::Path;
use std::fs::File; use std::fs::File;
use http::hyper::HyperBodyReader; use http::hyper::h1::HttpReader;
use http::hyper::net::NetworkStream;
use http::hyper::buffer;
pub type BodyReader<'a, 'b> =
self::HttpReader<&'a mut self::buffer::BufReader<&'b mut NetworkStream>>;
const PEEK_BYTES: usize = 4096; const PEEK_BYTES: usize = 4096;
@ -36,7 +41,7 @@ impl Data {
} }
#[doc(hidden)] #[doc(hidden)]
pub fn from_hyp(mut h_body: HyperBodyReader) -> Result<Data, &'static str> { pub fn from_hyp(mut h_body: BodyReader) -> Result<Data, &'static str> {
let mut vec = Vec::new(); let mut vec = Vec::new();
if let Err(_) = io::copy(&mut h_body, &mut vec) { if let Err(_) = io::copy(&mut h_body, &mut vec) {
return Err("Reading from body failed."); return Err("Reading from body failed.");

View File

@ -3,7 +3,7 @@ use std::str::FromStr;
use std::fmt; use std::fmt;
use http::Header; use http::Header;
use http::mime::Mime; use http::hyper::mime::Mime;
use router::Collider; use router::Collider;
/// Typed representation of HTTP Content-Types. /// Typed representation of HTTP Content-Types.
@ -236,7 +236,6 @@ impl FromStr for ContentType {
/// ```rust /// ```rust
/// use std::str::FromStr; /// use std::str::FromStr;
/// use rocket::http::ContentType; /// use rocket::http::ContentType;
/// use rocket::http::mime::{TopLevel, SubLevel};
/// ///
/// let custom = ContentType::from_str("application/x-custom").unwrap(); /// let custom = ContentType::from_str("application/x-custom").unwrap();
/// assert!(!custom.is_known()); /// assert!(!custom.is_known());

View File

@ -1,4 +1,5 @@
pub use http::hyper::HyperCookiePair as Cookie;
use http; use http;
pub type Cookies = http::hyper::HyperCookieJar<'static>;
pub use http::hyper::header::CookiePair as Cookie;
pub type Cookies = http::hyper::header::CookieJar<'static>;

View File

@ -7,32 +7,21 @@
//! while necessary. //! while necessary.
// TODO: Remove from Rocket in favor of a more flexible HTTP library. // TODO: Remove from Rocket in favor of a more flexible HTTP library.
pub use hyper::server::Request as HyperRequest; pub use hyper::server::Request as Request;
pub use hyper::server::Response as HyperResponse; pub use hyper::server::Response as Response;
pub use hyper::server::Server as HyperServer; pub use hyper::server::Server as Server;
pub use hyper::server::Handler as HyperHandler; pub use hyper::server::Handler as Handler;
pub use hyper::header::Headers as HyperHeaders;
pub use hyper::header::CookiePair as HyperCookiePair;
pub use hyper::header::CookieJar as HyperCookieJar;
pub use hyper::header::Cookie as HyperCookie;
pub use hyper::header::SetCookie as HyperSetCookie;
pub use hyper::method::Method as HyperMethod;
pub use hyper::uri::RequestUri as HyperRequestUri;
pub use hyper::net::Fresh as HyperFresh;
pub use hyper::net::HttpStream as HyperHttpStream;
pub use hyper::net::NetworkStream as HyperNetworkStream;
pub use hyper::http::h1::HttpReader as HyperHttpReader;
pub use hyper::header; pub use hyper::header;
pub use hyper::mime;
pub use hyper::net;
pub use hyper::method::Method;
pub use hyper::status::StatusCode; pub use hyper::status::StatusCode;
pub use hyper::uri::RequestUri;
pub use hyper::http::h1;
pub use hyper::buffer;
// TODO: Remove from Rocket in favor of a more flexible HTTP library. // TODO: Remove from Rocket in favor of a more flexible HTTP library.
pub type FreshHyperResponse<'a> = self::HyperResponse<'a, self::HyperFresh>; pub type FreshResponse<'a> = self::Response<'a, self::net::Fresh>;
// TODO: Remove from Rocket in favor of a more flexible HTTP library.
use hyper::buffer::BufReader;
pub type HyperBodyReader<'a, 'b> =
HyperHttpReader<&'a mut BufReader<&'b mut HyperNetworkStream>>;

View File

@ -2,7 +2,7 @@ use std::fmt;
use std::str::FromStr; use std::str::FromStr;
use error::Error; use error::Error;
use http::hyper::HyperMethod; use http::hyper;
use self::Method::*; use self::Method::*;
// TODO: Support non-standard methods, here and in codegen. // TODO: Support non-standard methods, here and in codegen.
@ -23,24 +23,24 @@ pub enum Method {
impl Method { impl Method {
#[doc(hidden)] #[doc(hidden)]
pub fn from_hyp(method: &HyperMethod) -> Option<Method> { pub fn from_hyp(method: &hyper::Method) -> Option<Method> {
match *method { match *method {
HyperMethod::Get => Some(Get), hyper::Method::Get => Some(Get),
HyperMethod::Put => Some(Put), hyper::Method::Put => Some(Put),
HyperMethod::Post => Some(Post), hyper::Method::Post => Some(Post),
HyperMethod::Delete => Some(Delete), hyper::Method::Delete => Some(Delete),
HyperMethod::Options => Some(Options), hyper::Method::Options => Some(Options),
HyperMethod::Head => Some(Head), hyper::Method::Head => Some(Head),
HyperMethod::Trace => Some(Trace), hyper::Method::Trace => Some(Trace),
HyperMethod::Connect => Some(Connect), hyper::Method::Connect => Some(Connect),
HyperMethod::Patch => Some(Patch), hyper::Method::Patch => Some(Patch),
HyperMethod::Extension(_) => None, hyper::Method::Extension(_) => None,
} }
} }
#[doc(hidden)] #[doc(hidden)]
#[inline(always)] #[inline(always)]
pub fn to_hyp(&self) -> HyperMethod { pub fn to_hyp(&self) -> hyper::Method {
self.to_string().as_str().parse().unwrap() self.to_string().as_str().parse().unwrap()
} }

View File

@ -14,9 +14,6 @@ mod content_type;
mod status; mod status;
mod header; mod header;
// TODO: Removed from Rocket in favor of a more flexible HTTP library.
pub use hyper::mime;
pub use self::method::Method; pub use self::method::Method;
pub use self::content_type::ContentType; pub use self::content_type::ContentType;
pub use self::status::Status; pub use self::status::Status;

View File

@ -9,7 +9,7 @@ use super::{FromParam, FromSegments};
use router::Route; use router::Route;
use http::uri::{URI, URIBuf, Segments}; use http::uri::{URI, URIBuf, Segments};
use http::hyper::{header, HyperCookie, HyperHeaders, HyperMethod, HyperRequestUri}; use http::hyper::{self, header};
use http::{Method, ContentType, Cookies}; use http::{Method, ContentType, Cookies};
/// The type of an incoming web request. /// The type of an incoming web request.
@ -25,7 +25,7 @@ pub struct Request {
uri: URIBuf, // FIXME: Should be URI (without hyper). uri: URIBuf, // FIXME: Should be URI (without hyper).
params: RefCell<Vec<&'static str>>, params: RefCell<Vec<&'static str>>,
cookies: Cookies, cookies: Cookies,
headers: HyperHeaders, // Don't use hyper's headers. headers: header::Headers, // Don't use hyper's headers.
} }
impl Request { impl Request {
@ -106,7 +106,7 @@ impl Request {
method: method, method: method,
cookies: Cookies::new(&[]), cookies: Cookies::new(&[]),
uri: URIBuf::from(uri), uri: URIBuf::from(uri),
headers: HyperHeaders::new(), headers: header::Headers::new(),
} }
} }
@ -120,7 +120,7 @@ impl Request {
/// ///
/// Returns the headers in this request. /// Returns the headers in this request.
#[inline(always)] #[inline(always)]
pub fn headers(&self) -> &HyperHeaders { pub fn headers(&self) -> &header::Headers {
// FIXME: Get rid of Hyper. // FIXME: Get rid of Hyper.
&self.headers &self.headers
} }
@ -188,8 +188,8 @@ impl Request {
#[doc(hidden)] #[doc(hidden)]
#[inline(always)] #[inline(always)]
pub fn set_headers(&mut self, h_headers: HyperHeaders) { pub fn set_headers(&mut self, h_headers: header::Headers) {
let cookies = match h_headers.get::<HyperCookie>() { let cookies = match h_headers.get::<header::Cookie>() {
// TODO: Retrieve key from config. // TODO: Retrieve key from config.
Some(cookie) => cookie.to_cookie_jar(&[]), Some(cookie) => cookie.to_cookie_jar(&[]),
None => Cookies::new(&[]), None => Cookies::new(&[]),
@ -200,12 +200,12 @@ impl Request {
} }
#[doc(hidden)] #[doc(hidden)]
pub fn new(h_method: HyperMethod, pub fn new(h_method: hyper::Method,
h_headers: HyperHeaders, h_headers: header::Headers,
h_uri: HyperRequestUri) h_uri: hyper::RequestUri)
-> Result<Request, String> { -> Result<Request, String> {
let uri = match h_uri { let uri = match h_uri {
HyperRequestUri::AbsolutePath(s) => URIBuf::from(s), hyper::RequestUri::AbsolutePath(s) => URIBuf::from(s),
_ => return Err(format!("Bad URI: {}", h_uri)), _ => return Err(format!("Bad URI: {}", h_uri)),
}; };
@ -214,7 +214,7 @@ impl Request {
_ => return Err(format!("Bad method: {}", h_method)), _ => return Err(format!("Bad method: {}", h_method)),
}; };
let cookies = match h_headers.get::<HyperCookie>() { let cookies = match h_headers.get::<header::Cookie>() {
// TODO: Retrieve key from config. // TODO: Retrieve key from config.
Some(cookie) => cookie.to_cookie_jar(&[]), Some(cookie) => cookie.to_cookie_jar(&[]),
None => Cookies::new(&[]), None => Cookies::new(&[]),

View File

@ -3,7 +3,7 @@ use std::convert::AsRef;
use outcome::IntoOutcome; use outcome::IntoOutcome;
use response::{Response, Responder}; use response::{Response, Responder};
use request::{self, Request, FromRequest}; use request::{self, Request, FromRequest};
use http::hyper::{HyperSetCookie, HyperCookiePair}; use http::hyper::header;
use http::Status; use http::Status;
// The name of the actual flash cookie. // The name of the actual flash cookie.
@ -161,9 +161,9 @@ impl<'r, R: Responder<'r>> Flash<R> {
Flash::new(responder, "error", msg) Flash::new(responder, "error", msg)
} }
fn cookie_pair(&self) -> HyperCookiePair { fn cookie_pair(&self) -> header::CookiePair {
let content = format!("{}{}{}", self.name.len(), self.name, self.message); let content = format!("{}{}{}", self.name.len(), self.name, self.message);
let mut pair = HyperCookiePair::new(FLASH_COOKIE_NAME.to_string(), content); let mut pair = header::CookiePair::new(FLASH_COOKIE_NAME.to_string(), content);
pair.path = Some("/".to_string()); pair.path = Some("/".to_string());
pair.max_age = Some(300); pair.max_age = Some(300);
pair pair
@ -179,7 +179,7 @@ impl<'r, R: Responder<'r>> Responder<'r> for Flash<R> {
trace_!("Flash: setting message: {}:{}", self.name, self.message); trace_!("Flash: setting message: {}:{}", self.name, self.message);
let cookie = vec![self.cookie_pair()]; let cookie = vec![self.cookie_pair()];
Response::build_from(self.responder.respond()?) Response::build_from(self.responder.respond()?)
.header_adjoin(HyperSetCookie(cookie)) .header_adjoin(header::SetCookie(cookie))
.ok() .ok()
} }
} }

View File

@ -19,8 +19,7 @@ use outcome::Outcome;
use error::Error; use error::Error;
use http::{Method, Status}; use http::{Method, Status};
use http::hyper::{self, HyperRequest, FreshHyperResponse}; use http::hyper::{self, header};
use http::hyper::{HyperServer, HyperHandler, HyperSetCookie, header};
/// The main `Rocket` type: used to mount routes and catchers and launch the /// The main `Rocket` type: used to mount routes and catchers and launch the
/// application. /// application.
@ -33,15 +32,15 @@ pub struct Rocket {
} }
#[doc(hidden)] #[doc(hidden)]
impl HyperHandler for Rocket { impl hyper::Handler for Rocket {
// This function tries to hide all of the Hyper-ness from Rocket. It // This function tries to hide all of the Hyper-ness from Rocket. It
// essentially converts Hyper types into Rocket types, then calls the // essentially converts Hyper types into Rocket types, then calls the
// `dispatch` function, which knows nothing about Hyper. Because responding // `dispatch` function, which knows nothing about Hyper. Because responding
// depends on the `HyperResponse` type, this function does the actual // depends on the `HyperResponse` type, this function does the actual
// response processing. // response processing.
fn handle<'h, 'k>(&self, fn handle<'h, 'k>(&self,
hyp_req: HyperRequest<'h, 'k>, hyp_req: hyper::Request<'h, 'k>,
res: FreshHyperResponse<'h>) { res: hyper::FreshResponse<'h>) {
// Get all of the information from Hyper. // Get all of the information from Hyper.
let (_, h_method, h_headers, h_uri, _, h_body) = hyp_req.deconstruct(); let (_, h_method, h_headers, h_uri, _, h_body) = hyp_req.deconstruct();
@ -87,7 +86,7 @@ impl HyperHandler for Rocket {
// We have a response from the user. Update the cookies in the header. // We have a response from the user. Update the cookies in the header.
let cookie_delta = request.cookies().delta(); let cookie_delta = request.cookies().delta();
if cookie_delta.len() > 0 { if cookie_delta.len() > 0 {
response.adjoin_header(HyperSetCookie(cookie_delta)); response.adjoin_header(header::SetCookie(cookie_delta));
} }
// Actually write out the response. // Actually write out the response.
@ -97,7 +96,7 @@ impl HyperHandler for Rocket {
impl Rocket { impl Rocket {
#[inline] #[inline]
fn issue_response(&self, mut response: Response, hyp_res: FreshHyperResponse) { fn issue_response(&self, mut response: Response, hyp_res: hyper::FreshResponse) {
// Add the 'rocket' server header, and write out the response. // Add the 'rocket' server header, and write out the response.
// TODO: If removing Hyper, write out `Data` header too. // TODO: If removing Hyper, write out `Data` header too.
response.set_header(header::Server("rocket".to_string())); response.set_header(header::Server("rocket".to_string()));
@ -109,7 +108,7 @@ impl Rocket {
} }
fn write_response(&self, mut response: Response, fn write_response(&self, mut response: Response,
mut hyp_res: FreshHyperResponse) -> io::Result<()> mut hyp_res: hyper::FreshResponse) -> io::Result<()>
{ {
*hyp_res.status_mut() = hyper::StatusCode::from_u16(response.status().code); *hyp_res.status_mut() = hyper::StatusCode::from_u16(response.status().code);
@ -429,7 +428,7 @@ impl Rocket {
} }
let full_addr = format!("{}:{}", self.address, self.port); let full_addr = format!("{}:{}", self.address, self.port);
let server = match HyperServer::http(full_addr.as_str()) { let server = match hyper::Server::http(full_addr.as_str()) {
Ok(hyper_server) => hyper_server, Ok(hyper_server) => hyper_server,
Err(e) => { Err(e) => {
error!("failed to start server."); error!("failed to start server.");

View File

@ -132,7 +132,7 @@ impl MockRequest {
/// ]); /// ]);
/// ``` /// ```
pub fn headers<'h, H: AsRef<[(&'h str, &'h str)]>>(mut self, headers: H) -> Self { pub fn headers<'h, H: AsRef<[(&'h str, &'h str)]>>(mut self, headers: H) -> Self {
let mut hyp_headers = hyper::HyperHeaders::new(); let mut hyp_headers = hyper::header::Headers::new();
for &(name, fields) in headers.as_ref() { for &(name, fields) in headers.as_ref() {
let mut vec_fields = vec![]; let mut vec_fields = vec![];