Skip to content
Permalink
Browse files

Upgrade to hyper 0.11 (WIP)

  • Loading branch information...
schrieveslaach committed May 4, 2019
1 parent 0a3960b commit e3d391f11a6f4fabbc7271446e53001283d6f9c0
@@ -21,7 +21,8 @@ private-cookies = ["cookie/secure"]
[dependencies]
smallvec = "0.6"
percent-encoding = "1"
hyper = { version = "0.10.13", default-features = false }
hyper = { version = "0.11.27", default-features = false }
http = "0.1.5"
time = "0.1"
indexmap = "1.0"
rustls = { version = "0.14", optional = true }
@@ -281,11 +281,11 @@ impl From<Mime> for ContentType {
#[inline]
fn from(mime: Mime) -> ContentType {
// soooo inefficient.
let params = mime.2.into_iter()
let params = mime.params().into_iter()
.map(|(attr, value)| (attr.to_string(), value.to_string()))
.collect::<Vec<_>>();

ContentType::with_params(mime.0.to_string(), mime.1.to_string(), params)
ContentType::with_params(mime.type_().to_string(), mime.subtype().to_string(), params)
}
}

@@ -5,25 +5,29 @@
//! while necessary.

extern crate hyper;
extern crate http;

#[doc(hidden)] pub use self::hyper::server::Request as Request;
#[doc(hidden)] pub use self::hyper::server::Response as Response;
#[doc(hidden)] pub use self::hyper::server::Server as Server;
#[doc(hidden)] pub use self::hyper::server::Handler as Handler;
#[doc(hidden)] pub use self::hyper::server::Http as Http;
#[doc(hidden)] pub use self::hyper::server::Service as Service;

#[doc(hidden)] pub use self::hyper::net;
// TODO #[doc(hidden)] pub use self::hyper::net;

#[doc(hidden)] pub use self::hyper::method::Method;
#[doc(hidden)] pub use self::hyper::status::StatusCode;
#[doc(hidden)] pub use self::http::method::Method;
#[doc(hidden)] pub use self::http::status::StatusCode;
#[doc(hidden)] pub use self::hyper::error::Error;
#[doc(hidden)] pub use self::hyper::uri::RequestUri;
#[doc(hidden)] pub use self::hyper::http::h1;
#[doc(hidden)] pub use self::hyper::buffer;
#[doc(hidden)] pub use self::hyper::Body;
#[doc(hidden)] pub use self::hyper::Chunk;
#[doc(hidden)] pub use self::http::uri::Uri;
// TODO #[doc(hidden)] pub use self::hyper::http::h1;
// TODO #[doc(hidden)] pub use self::hyper::buffer;

pub use self::hyper::mime;

/// Type alias to `self::hyper::Response<'a, self::hyper::net::Fresh>`.
#[doc(hidden)] pub type FreshResponse<'a> = self::Response<'a, self::net::Fresh>;
// TODO #[doc(hidden)] pub type FreshResponse<'a> = self::Response<'a, self::net::Fresh>;

/// Reexported Hyper header types.
pub mod header {
@@ -1,3 +1,5 @@
extern crate http;

use std::fmt;
use std::str::FromStr;

@@ -24,18 +26,18 @@ pub enum Method {
impl Method {
/// WARNING: This is unstable! Do not use this method outside of Rocket!
#[doc(hidden)]
pub fn from_hyp(method: &hyper::Method) -> Option<Method> {
pub fn from_hyp(method: &http::method::Method) -> Option<Method> {
match *method {
hyper::Method::Get => Some(Get),
hyper::Method::Put => Some(Put),
hyper::Method::Post => Some(Post),
hyper::Method::Delete => Some(Delete),
hyper::Method::Options => Some(Options),
hyper::Method::Head => Some(Head),
hyper::Method::Trace => Some(Trace),
hyper::Method::Connect => Some(Connect),
hyper::Method::Patch => Some(Patch),
hyper::Method::Extension(_) => None,
http::method::Method::GET => Some(Get),
http::method::Method::PUT => Some(Put),
http::method::Method::POST => Some(Post),
http::method::Method::DELETE => Some(Delete),
http::method::Method::OPTIONS => Some(Options),
http::method::Method::HEAD => Some(Head),
http::method::Method::TRACE => Some(Trace),
http::method::Method::CONNECT => Some(Connect),
http::method::Method::PATCH => Some(Patch),
_ => None,
}
}

@@ -6,7 +6,7 @@ use std::convert::TryFrom;

use ext::IntoOwned;
use parse::Indexed;
use uri::{Origin, Authority, Absolute, Error};
use uri::{Origin, Authority, Absolute, Error, Host};
use uri::encoding::{percent_encode, DEFAULT_ENCODE_SET};

/// An `enum` encapsulating any of the possible URI variants.
@@ -94,6 +94,20 @@ impl<'a> Uri<'a> {
::parse::uri::from_str(string)
}

// pub fn from_hyp(uri: &'a hyper::Uri) -> Uri<'a> {
// match uri.is_absolute() {
// true => Uri::Absolute(Absolute::new(
// uri.scheme().unwrap(),
// match uri.host() {
// Some(host) => Some(Authority::new(None, Host::Raw(host), uri.port())),
// None => None
// },
// None
// )),
// false => Uri::Asterisk
// }
// }

/// Returns the internal instance of `Origin` if `self` is a `Uri::Origin`.
/// Otherwise, returns `None`.
///
@@ -23,6 +23,7 @@ tls = ["rocket_http/tls"]
private-cookies = ["rocket_http/private-cookies"]

[dependencies]
futures = "0.1"
rocket_codegen = { version = "0.4.0", path = "../codegen" }
rocket_http = { version = "0.4.0", path = "../http" }
yansi = "0.5"
@@ -5,20 +5,18 @@ use std::time::Duration;

#[cfg(feature = "tls")] use super::net_stream::HttpsStream;

use super::data_stream::{DataStream, kill_stream};
use super::data_stream::{DataStream, /* TODO kill_stream */};
use super::net_stream::NetStream;
use ext::ReadExt;

use http::hyper;
use http::hyper::h1::HttpReader;
use http::hyper::h1::HttpReader::*;
use http::hyper::net::{HttpStream, NetworkStream};
// TODO use http::hyper::h1::HttpReader;
// TODO use http::hyper::h1::HttpReader::*;
// TODO use http::hyper::net::{HttpStream, NetworkStream};

pub type HyperBodyReader<'a, 'b> =
self::HttpReader<&'a mut hyper::buffer::BufReader<&'b mut NetworkStream>>;
// TODO pub type HyperBodyReader<'a, 'b> =
// self::HttpReader<&'a mut hyper::buffer::BufReader<&'b mut NetworkStream>>;

// |---- from hyper ----|
pub type BodyReader = HttpReader<Chain<Cursor<Vec<u8>>, NetStream>>;

/// The number of bytes to read into the "peek" buffer.
const PEEK_BYTES: usize = 512;
@@ -58,7 +56,7 @@ const PEEK_BYTES: usize = 512;
pub struct Data {
buffer: Vec<u8>,
is_complete: bool,
stream: BodyReader,
stream: hyper::Body,
}

impl Data {
@@ -85,56 +83,15 @@ impl Data {
// FIXME: Insert a `BufReader` in front of the `NetStream` with capacity
// 4096. We need the new `Chain` methods to get the inner reader to
// actually do this, however.
let empty_http_stream = HttpReader::SizedReader(empty_stream, 0);
let stream = ::std::mem::replace(&mut self.stream, empty_http_stream);
DataStream(Cursor::new(buffer).chain(stream))
// TODO let empty_http_stream = HttpReader::SizedReader(empty_stream, 0);
// TODO let stream = ::std::mem::replace(&mut self.stream, empty_http_stream);
DataStream(/* TODO Cursor::new(buffer).chain(stream)*/)
}

// FIXME: This is absolutely terrible (downcasting!), thanks to Hyper.
crate fn from_hyp(mut body: HyperBodyReader) -> Result<Data, &'static str> {
#[inline(always)]
#[cfg(feature = "tls")]
fn concrete_stream(stream: &mut NetworkStream) -> Option<NetStream> {
stream.downcast_ref::<HttpsStream>()
.map(|s| NetStream::Https(s.clone()))
.or_else(|| {
stream.downcast_ref::<HttpStream>()
.map(|s| NetStream::Http(s.clone()))
})
}

#[inline(always)]
#[cfg(not(feature = "tls"))]
fn concrete_stream(stream: &mut NetworkStream) -> Option<NetStream> {
stream.downcast_ref::<HttpStream>()
.map(|s| NetStream::Http(s.clone()))
}

// Retrieve the underlying Http(s)Stream from Hyper.
let net_stream = match concrete_stream(*body.get_mut().get_mut()) {
Some(net_stream) => net_stream,
None => return Err("Stream is not an HTTP(s) stream!")
};

// Set the read timeout to 5 seconds.
let _ = net_stream.set_read_timeout(Some(Duration::from_secs(5)));

// Steal the internal, undecoded data buffer from Hyper.
let (mut hyper_buf, pos, cap) = body.get_mut().take_buf();
hyper_buf.truncate(cap); // slow, but safe
let mut cursor = Cursor::new(hyper_buf);
cursor.set_position(pos as u64);
crate fn from_hyp(mut body: hyper::Body) -> Result<Data, &'static str> {

// Create an HTTP reader from the buffer + stream.
let inner_data = cursor.chain(net_stream);
let http_stream = match body {
SizedReader(_, n) => SizedReader(inner_data, n),
EofReader(_) => EofReader(inner_data),
EmptyReader(_) => EmptyReader(inner_data),
ChunkedReader(_, n) => ChunkedReader(inner_data, n)
};

Ok(Data::new(http_stream))
Ok(Data::new(body))
}

/// Retrieve the `peek` buffer.
@@ -230,49 +187,25 @@ impl Data {
// bytes `vec[pos..cap]` are buffered and unread. The remainder of the data
// bytes can be read from `stream`.
#[inline(always)]
crate fn new(mut stream: BodyReader) -> Data {
trace_!("Data::new({:?})", stream);
crate fn new(body: hyper::Body) -> Data {
let mut peek_buf: Vec<u8> = vec![0; PEEK_BYTES];

// Fill the buffer with as many bytes as possible. If we read less than
// that buffer's length, we know we reached the EOF. Otherwise, it's
// unclear, so we just say we didn't reach EOF.
let eof = match stream.read_max(&mut peek_buf[..]) {
Ok(n) => {
trace_!("Filled peek buf with {} bytes.", n);
// We can use `set_len` here instead of `truncate`, but we'll
// take the performance hit to avoid `unsafe`. All of this code
// should go away when we migrate away from hyper 0.10.x.
peek_buf.truncate(n);
n < PEEK_BYTES
}
Err(e) => {
error_!("Failed to read into peek buffer: {:?}.", e);
// Likewise here as above.
peek_buf.truncate(0);
false
},
};

trace_!("Peek bytes: {}/{} bytes.", peek_buf.len(), PEEK_BYTES);
Data { buffer: peek_buf, stream, is_complete: eof }
Data { buffer: peek_buf, stream: body, is_complete: false }
}

/// This creates a `data` object from a local data source `data`.
#[inline]
crate fn local(data: Vec<u8>) -> Data {
let empty_stream = Cursor::new(vec![]).chain(NetStream::Empty);

Data {
buffer: data,
stream: HttpReader::SizedReader(empty_stream, 0),
buffer: data.clone(),
stream: hyper::Body::from(data),
is_complete: true,
}
}
}

impl Drop for Data {
fn drop(&mut self) {
kill_stream(&mut self.stream);
// TODO kill_stream(&mut self.stream);
}
}
@@ -1,34 +1,35 @@
use std::io::{self, Read, Cursor, Chain};
use std::net::Shutdown;

use super::data::BodyReader;
use http::hyper::net::NetworkStream;
use http::hyper::h1::HttpReader;
// TODO use super::data::BodyReader;
// TODO use http::hyper::net::NetworkStream;
// TODO use http::hyper::h1::HttpReader;

// |-- peek buf --|
pub type InnerStream = Chain<Cursor<Vec<u8>>, BodyReader>;
// TODO pub type InnerStream = Chain<Cursor<Vec<u8>>, BodyReader>;

/// Raw data stream of a request body.
///
/// This stream can only be obtained by calling
/// [`Data::open()`](::data::Data::open()). The stream contains all of the data
/// in the body of the request. It exposes no methods directly. Instead, it must
/// be used as an opaque [`Read`] structure.
pub struct DataStream(crate InnerStream);
pub struct DataStream();

// TODO: Have a `BufRead` impl for `DataStream`. At the moment, this isn't
// possible since Hyper's `HttpReader` doesn't implement `BufRead`.
impl Read for DataStream {
#[inline(always)]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
trace_!("DataStream::read()");
self.0.read(buf)
// TODO self.0.read(buf)
unimplemented!()
}
}

pub fn kill_stream(stream: &mut BodyReader) {
/* pub fn kill_stream(stream: &mut BodyReader) {
// Only do the expensive reading if we're not sure we're done.
use self::HttpReader::*;
// TODO use self::HttpReader::*;
match *stream {
SizedReader(_, n) | ChunkedReader(_, Some(n)) if n > 0 => { /* continue */ },
_ => return
@@ -46,10 +47,10 @@ pub fn kill_stream(stream: &mut BodyReader) {
}
Ok(n) => debug!("flushed {} unread bytes", n)
}
}
}*/

impl Drop for DataStream {
fn drop(&mut self) {
kill_stream(&mut self.0.get_mut().1);
// TODO kill_stream(&mut self.0.get_mut().1);
}
}
Oops, something went wrong.

0 comments on commit e3d391f

Please sign in to comment.
You can’t perform that action at this time.