diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index edc075b92..e677c3fa6 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -1,5 +1,15 @@ # Changes +### Added + +* Debug impl for ResponseBuilder + +* From SizedStream and BodyStream for Body + +### Changed + +* SizedStream accepts u64 + ## [0.2.2] - 2019-05-29 ### Fixed diff --git a/actix-http/src/body.rs b/actix-http/src/body.rs index 0652dd274..e728cdb98 100644 --- a/actix-http/src/body.rs +++ b/actix-http/src/body.rs @@ -234,6 +234,25 @@ impl From for Body { } } +impl From> for Body +where + S: Stream + 'static, +{ + fn from(s: SizedStream) -> Body { + Body::from_message(s) + } +} + +impl From> for Body +where + S: Stream + 'static, + E: Into + 'static, +{ + fn from(s: BodyStream) -> Body { + Body::from_message(s) + } +} + impl MessageBody for Bytes { fn size(&self) -> BodySize { BodySize::Sized(self.len()) @@ -366,7 +385,7 @@ where /// Type represent streaming body. This body implementation should be used /// if total size of stream is known. Data get sent as is without using transfer encoding. pub struct SizedStream { - size: usize, + size: u64, stream: S, } @@ -374,7 +393,7 @@ impl SizedStream where S: Stream, { - pub fn new(size: usize, stream: S) -> Self { + pub fn new(size: u64, stream: S) -> Self { SizedStream { size, stream } } } @@ -384,7 +403,7 @@ where S: Stream, { fn size(&self) -> BodySize { - BodySize::Sized(self.size) + BodySize::Sized64(self.size) } fn poll_next(&mut self) -> Poll, Error> { diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index fd51e54c7..ce986a472 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -764,6 +764,25 @@ impl IntoFuture for ResponseBuilder { } } +impl fmt::Debug for ResponseBuilder { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let head = self.head.as_ref().unwrap(); + + let res = writeln!( + f, + "\nResponseBuilder {:?} {}{}", + head.version, + head.status, + head.reason.unwrap_or(""), + ); + let _ = writeln!(f, " headers:"); + for (key, val) in head.headers.iter() { + let _ = writeln!(f, " {:?}: {:?}", key, val); + } + res + } +} + /// Helper converters impl, E: Into> From> for Response { fn from(res: Result) -> Self { diff --git a/actix-http/tests/test_server.rs b/actix-http/tests/test_server.rs index a299f58d7..4a679f4b9 100644 --- a/actix-http/tests/test_server.rs +++ b/actix-http/tests/test_server.rs @@ -12,7 +12,6 @@ use futures::stream::{once, Stream}; use regex::Regex; use tokio_timer::sleep; -use actix_http::body::Body; use actix_http::error::PayloadError; use actix_http::{ body, error, http, http::header, Error, HttpService, KeepAlive, Request, Response, @@ -218,30 +217,28 @@ fn test_expect_continue_h1() { #[test] fn test_chunked_payload() { - let chunk_sizes = vec![ 32768, 32, 32768 ]; + let chunk_sizes = vec![32768, 32, 32768]; let total_size: usize = chunk_sizes.iter().sum(); let srv = TestServer::new(|| { - HttpService::build() - .h1(|mut request: Request| { - request.take_payload() - .map_err(|e| panic!(format!("Error reading payload: {}", e))) - .fold(0usize, |acc, chunk| { - future::ok::<_, ()>(acc + chunk.len()) - }) - .map(|req_size| { - Response::Ok().body(format!("size={}", req_size)) - }) - }) + HttpService::build().h1(|mut request: Request| { + request + .take_payload() + .map_err(|e| panic!(format!("Error reading payload: {}", e))) + .fold(0usize, |acc, chunk| future::ok::<_, ()>(acc + chunk.len())) + .map(|req_size| Response::Ok().body(format!("size={}", req_size))) + }) }); let returned_size = { let mut stream = net::TcpStream::connect(srv.addr()).unwrap(); - let _ = stream.write_all(b"POST /test HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n"); + let _ = stream + .write_all(b"POST /test HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n"); for chunk_size in chunk_sizes.iter() { let mut bytes = Vec::new(); - let random_bytes: Vec = (0..*chunk_size).map(|_| rand::random::()).collect(); + let random_bytes: Vec = + (0..*chunk_size).map(|_| rand::random::()).collect(); bytes.extend(format!("{:X}\r\n", chunk_size).as_bytes()); bytes.extend(&random_bytes[..]); @@ -826,8 +823,7 @@ fn test_h1_body_length() { HttpService::build().h1(|_| { let body = once(Ok(Bytes::from_static(STR.as_ref()))); ok::<_, ()>( - Response::Ok() - .body(Body::from_message(body::SizedStream::new(STR.len(), body))), + Response::Ok().body(body::SizedStream::new(STR.len() as u64, body)), ) }) }); @@ -852,9 +848,10 @@ fn test_h2_body_length() { HttpService::build() .h2(|_| { let body = once(Ok(Bytes::from_static(STR.as_ref()))); - ok::<_, ()>(Response::Ok().body(Body::from_message( - body::SizedStream::new(STR.len(), body), - ))) + ok::<_, ()>( + Response::Ok() + .body(body::SizedStream::new(STR.len() as u64, body)), + ) }) .map_err(|_| ()), )