Skip to content

Commit

Permalink
Revert "[WIP] start working on rwf2#813"
Browse files Browse the repository at this point in the history
This reverts commit 2af7c0b
  • Loading branch information
lovasoa committed Nov 5, 2018
1 parent 2af7c0b commit fbeb2b6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 89 deletions.
3 changes: 0 additions & 3 deletions core/lib/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ crate mod flash;

pub mod content;
pub mod status;
pub mod writable;

pub use self::response::{Response, ResponseBuilder, Body, DEFAULT_CHUNK_SIZE};
pub use self::responder::Responder;
pub use self::redirect::Redirect;
pub use self::flash::Flash;
pub use self::named_file::NamedFile;
pub use self::stream::Stream;
pub use self::writable::Writable;

#[doc(inline)] pub use self::content::Content;

/// Type alias for the `Result` of a `Responder::respond` call.
Expand Down
41 changes: 21 additions & 20 deletions core/lib/src/response/response.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use http::{ContentType, Cookie, Header, HeaderMap, Status};
use request::Request;
use response::Responder;
use std::{fmt, io, str};
use std::{io, fmt, str};
use std::borrow::Cow;
use super::writable::Writable;

use response::Responder;
use http::{Header, HeaderMap, Status, ContentType, Cookie};

/// The default size, in bytes, of a chunk for streamed responses.
pub const DEFAULT_CHUNK_SIZE: u64 = 4096;
Expand Down Expand Up @@ -60,13 +59,13 @@ impl<T> Body<T> {
}
}

impl<W: Writable> Body<Writable> {
impl<T: io::Read> Body<T> {
/// Attempts to read `self` into a `Vec` and returns it. If reading fails,
/// returns `None`.
pub fn into_bytes(self) -> Option<Vec<u8>> {
let mut vec = Vec::new();
let mut body = self.into_inner();
if let Err(e) = body.write(Box::new(&mut vec)) {
if let Err(e) = body.read_to_end(&mut vec) {
error_!("Error reading body: {:?}", e);
return None;
}
Expand Down Expand Up @@ -351,7 +350,7 @@ impl<'r> ResponseBuilder<'r> {
/// ```
#[inline(always)]
pub fn sized_body<B>(&mut self, body: B) -> &mut ResponseBuilder<'r>
where B: Writable + io::Seek + 'r
where B: io::Read + io::Seek + 'r
{
self.response.set_sized_body(body);
self
Expand All @@ -377,7 +376,7 @@ impl<'r> ResponseBuilder<'r> {
/// ```
#[inline(always)]
pub fn streamed_body<B>(&mut self, body: B) -> &mut ResponseBuilder<'r>
where B: Writable + 'r
where B: io::Read + 'r
{
self.response.set_streamed_body(body);
self
Expand All @@ -403,8 +402,8 @@ impl<'r> ResponseBuilder<'r> {
/// # }
/// ```
#[inline(always)]
pub fn chunked_body<B: Writable + 'r>(&mut self, body: B, chunk_size: u64)
-> &mut ResponseBuilder<'r>
pub fn chunked_body<B: io::Read + 'r>(&mut self, body: B, chunk_size: u64)
-> &mut ResponseBuilder<'r>
{
self.response.set_chunked_body(body, chunk_size);
self
Expand All @@ -426,8 +425,8 @@ impl<'r> ResponseBuilder<'r> {
/// .finalize();
/// ```
#[inline(always)]
pub fn raw_body<T: Writable + 'r>(&mut self, body: Body<T>)
-> &mut ResponseBuilder<'r>
pub fn raw_body<T: io::Read + 'r>(&mut self, body: Body<T>)
-> &mut ResponseBuilder<'r>
{
self.response.set_raw_body(body);
self
Expand Down Expand Up @@ -561,7 +560,7 @@ impl<'r> ResponseBuilder<'r> {
pub struct Response<'r> {
status: Option<Status>,
headers: HeaderMap<'r>,
body: Option<Body<Box<Writable + 'r>>>,
body: Option<Body<Box<io::Read + 'r>>>,
}

impl<'r> Response<'r> {
Expand Down Expand Up @@ -890,7 +889,7 @@ impl<'r> Response<'r> {
/// assert_eq!(response.body_string(), Some("Hello, world!".to_string()));
/// ```
#[inline(always)]
pub fn body(&mut self) -> Option<Body<&mut Writable>> {
pub fn body(&mut self) -> Option<Body<&mut io::Read>> {
// Looks crazy, right? Needed so Rust infers lifetime correctly. Weird.
match self.body.as_mut() {
Some(body) => Some(match body.as_mut() {
Expand Down Expand Up @@ -967,7 +966,7 @@ impl<'r> Response<'r> {
/// assert!(response.body().is_none());
/// ```
#[inline(always)]
pub fn take_body(&mut self) -> Option<Body<Box<Writable + 'r>>> {
pub fn take_body(&mut self) -> Option<Body<Box<io::Read + 'r>>> {
self.body.take()
}

Expand Down Expand Up @@ -1005,7 +1004,7 @@ impl<'r> Response<'r> {
/// ```
#[inline]
pub fn set_sized_body<B>(&mut self, mut body: B)
where B: Writable + io::Seek + 'r
where B: io::Read + io::Seek + 'r
{
let size = body.seek(io::SeekFrom::End(0))
.expect("Attempted to retrieve size by seeking, but failed.");
Expand All @@ -1030,7 +1029,7 @@ impl<'r> Response<'r> {
/// assert_eq!(response.body_string(), Some("aaaaa".to_string()));
/// ```
#[inline(always)]
pub fn set_streamed_body<B>(&mut self, body: B) where B: Writable + 'r {
pub fn set_streamed_body<B>(&mut self, body: B) where B: io::Read + 'r {
self.set_chunked_body(body, DEFAULT_CHUNK_SIZE);
}

Expand All @@ -1049,7 +1048,7 @@ impl<'r> Response<'r> {
/// ```
#[inline(always)]
pub fn set_chunked_body<B>(&mut self, body: B, chunk_size: u64)
where B: Writable + 'r {
where B: io::Read + 'r {
self.body = Some(Body::Chunked(Box::new(body), chunk_size));
}

Expand All @@ -1071,7 +1070,7 @@ impl<'r> Response<'r> {
/// assert_eq!(response.body_string(), Some("Hello!".to_string()));
/// ```
#[inline(always)]
pub fn set_raw_body<T: Writable + 'r>(&mut self, body: Body<T>) {
pub fn set_raw_body<T: io::Read + 'r>(&mut self, body: Body<T>) {
self.body = Some(match body {
Body::Sized(b, n) => Body::Sized(Box::new(b.take(n)), n),
Body::Chunked(b, n) => Body::Chunked(Box::new(b), n),
Expand Down Expand Up @@ -1192,6 +1191,8 @@ impl<'r> fmt::Debug for Response<'r> {
}
}

use request::Request;

impl<'r> Responder<'r> for Response<'r> {
/// This is the identity implementation. It simply returns `Ok(self)`.
fn respond_to(self, _: &Request) -> Result<Response<'r>, Status> {
Expand Down
12 changes: 6 additions & 6 deletions core/lib/src/response/stream.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::io::Read;
use std::fmt::{self, Debug};

use request::Request;
use response::{Response, Responder, DEFAULT_CHUNK_SIZE};
use response::Writable;
use http::Status;

/// Streams a response to a client from an arbitrary `Read`er type.
Expand All @@ -11,9 +11,9 @@ use http::Status;
/// 4KiB. This means that at most 4KiB are stored in memory while the response
/// is being sent. This type should be used when sending responses that are
/// arbitrarily large in size, such as when streaming from a local socket.
pub struct Stream<T: Writable>(T, u64);
pub struct Stream<T: Read>(T, u64);

impl<T: Writable> Stream<T> {
impl<T: Read> Stream<T> {
/// Create a new stream from the given `reader` and sets the chunk size for
/// each streamed chunk to `chunk_size` bytes.
///
Expand All @@ -34,7 +34,7 @@ impl<T: Writable> Stream<T> {
}
}

impl<T: Writable + Debug> Debug for Stream<T> {
impl<T: Read + Debug> Debug for Stream<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Stream({:?})", self.0)
}
Expand All @@ -54,7 +54,7 @@ impl<T: Writable + Debug> Debug for Stream<T> {
/// # #[allow(unused_variables)]
/// let response = Stream::from(io::stdin());
/// ```
impl<T: Writable> From<T> for Stream<T> {
impl<T: Read> From<T> for Stream<T> {
fn from(reader: T) -> Self {
Stream(reader, DEFAULT_CHUNK_SIZE)
}
Expand All @@ -68,7 +68,7 @@ impl<T: Writable> From<T> for Stream<T> {
/// If reading from the input stream fails at any point during the response, the
/// response is abandoned, and the response ends abruptly. An error is printed
/// to the console with an indication of what went wrong.
impl<'r, T: Writable + 'r> Responder<'r> for Stream<T> {
impl<'r, T: Read + 'r> Responder<'r> for Stream<T> {
fn respond_to(self, _: &Request) -> Result<Response<'r>, Status> {
Response::build().chunked_body(self.0, self.1).ok()
}
Expand Down
57 changes: 0 additions & 57 deletions core/lib/src/response/writable.rs

This file was deleted.

12 changes: 9 additions & 3 deletions core/lib/src/rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Rocket {
Some(Body::Sized(body, size)) => {
hyp_res.headers_mut().set(header::ContentLength(size));
let mut stream = hyp_res.start()?;
body.write(Box::new(&mut stream))?;
io::copy(body, &mut stream)?;
stream.end()
}
Some(Body::Chunked(mut body, chunk_size)) => {
Expand All @@ -158,7 +158,13 @@ impl Rocket {
// The buffer stores the current chunk being written out.
let mut buffer = vec![0; chunk_size as usize];
let mut stream = hyp_res.start()?;
body.write_chunked(Box::new(&mut stream), chunk_size as usize);
loop {
match body.read_max(&mut buffer)? {
0 => break,
n => stream.write_all(&buffer[..n])?,
}
}

stream.end()
}
}
Expand Down Expand Up @@ -801,4 +807,4 @@ impl Rocket {
pub fn config(&self) -> &Config {
&self.config
}
}
}

0 comments on commit fbeb2b6

Please sign in to comment.