From 02fb424659bee6179dfabb02352b776f63a50893 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 8 Nov 2017 19:31:25 -0800 Subject: [PATCH] add custom Debug impl for HttpResponse --- examples/tls/src/main.rs | 7 +++++-- src/h1.rs | 6 ++++-- src/h1writer.rs | 4 +++- src/h2.rs | 7 ++++--- src/h2writer.rs | 4 +++- src/httpresponse.rs | 24 ++++++++++++++++++++++-- src/task.rs | 2 -- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/examples/tls/src/main.rs b/examples/tls/src/main.rs index 49d8acbb2..6d1b856d7 100644 --- a/examples/tls/src/main.rs +++ b/examples/tls/src/main.rs @@ -13,12 +13,15 @@ use actix_web::*; /// somple handle fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse { println!("{:?}", req); - httpcodes::HTTPOk.with_body("Welcome!") + httpcodes::HTTPOk + .builder() + .content_type("text/plain") + .body("Welcome!").unwrap() } fn main() { if ::std::env::var("RUST_LOG").is_err() { - ::std::env::set_var("RUST_LOG", "actix_web=info"); + ::std::env::set_var("RUST_LOG", "actix_web=trace"); } let _ = env_logger::init(); let sys = actix::System::new("ws-example"); diff --git a/src/h1.rs b/src/h1.rs index 6ade25b76..16d250585 100644 --- a/src/h1.rs +++ b/src/h1.rs @@ -83,8 +83,10 @@ impl Http1 // keep-alive timer if let Some(ref mut timeout) = self.keepalive_timer { match timeout.poll() { - Ok(Async::Ready(_)) => - return Ok(Async::Ready(Http1Result::Done)), + Ok(Async::Ready(_)) => { + trace!("Keep-alive timeout, close connection"); + return Ok(Async::Ready(Http1Result::Done)) + } Ok(Async::NotReady) => (), Err(_) => unreachable!(), } diff --git a/src/h1writer.rs b/src/h1writer.rs index dccf07033..838229a0d 100644 --- a/src/h1writer.rs +++ b/src/h1writer.rs @@ -101,7 +101,7 @@ impl Writer for H1Writer { fn start(&mut self, req: &mut HttpRequest, msg: &mut HttpResponse) -> Result { - trace!("Prepare message status={:?}", msg.status); + trace!("Prepare response with status: {:?}", msg.status); // prepare task self.started = true; @@ -164,6 +164,8 @@ impl Writer for H1Writer { buffer.extend(b"\r\n"); } + trace!("Response: {:?}", msg); + if msg.body().is_binary() { let body = msg.replace_body(Body::Empty); if let Body::Binary(bytes) = body { diff --git a/src/h2.rs b/src/h2.rs index 4d16f70fc..22ae0fe73 100644 --- a/src/h2.rs +++ b/src/h2.rs @@ -61,12 +61,13 @@ impl Http2 pub fn poll(&mut self) -> Poll<(), ()> { // server if let State::Server(ref mut server) = self.state { - // keep-alive timer if let Some(ref mut timeout) = self.keepalive_timer { match timeout.poll() { - Ok(Async::Ready(_)) => - return Ok(Async::Ready(())), + Ok(Async::Ready(_)) => { + trace!("Keep-alive timeout, close connection"); + return Ok(Async::Ready(())) + } Ok(Async::NotReady) => (), Err(_) => unreachable!(), } diff --git a/src/h2writer.rs b/src/h2writer.rs index a4adba227..88f1830cf 100644 --- a/src/h2writer.rs +++ b/src/h2writer.rs @@ -101,7 +101,7 @@ impl Writer for H2Writer { fn start(&mut self, req: &mut HttpRequest, msg: &mut HttpResponse) -> Result { - trace!("Prepare message status={:?}", msg); + trace!("Prepare response with status: {:?}", msg.status); // prepare response self.started = true; @@ -140,6 +140,8 @@ impl Writer for H2Writer { return Err(io::Error::new(io::ErrorKind::Other, "err")), } + trace!("Response: {:?}", msg); + if msg.body().is_binary() { if let Body::Binary(bytes) = msg.replace_body(Body::Empty) { self.eof = true; diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 3bc7a089e..72a909ef0 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -1,5 +1,5 @@ //! Pieces pertaining to the HTTP message protocol. -use std::{io, mem, str}; +use std::{io, mem, str, fmt}; use std::error::Error as Error; use std::convert::Into; @@ -23,7 +23,6 @@ pub enum ConnectionType { Upgrade, } -#[derive(Debug)] /// An HTTP Response pub struct HttpResponse { pub version: Option, @@ -215,6 +214,27 @@ impl From for Frame { } } +impl fmt::Debug for HttpResponse { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let res = write!(f, "\nHttpResponse {:?} {}{}\n", + self.version, self.status, self.reason.unwrap_or("")); + let _ = write!(f, " encoding: {:?}\n", self.encoding); + let _ = write!(f, " headers:\n"); + for key in self.headers.keys() { + let vals: Vec<_> = self.headers.get_all(key).iter().collect(); + if vals.len() > 1 { + let _ = write!(f, " {:?}: {:?}\n", key, vals); + } else { + let _ = write!(f, " {:?}: {:?}\n", key, vals[0]); + } + } + if let Some(ref err) = self.error { + let _ = write!(f, " error: {}\n", err); + } + res + } +} + #[derive(Debug)] struct Parts { version: Option, diff --git a/src/task.rs b/src/task.rs index 74c2c4156..a9c9c6276 100644 --- a/src/task.rs +++ b/src/task.rs @@ -197,8 +197,6 @@ impl Task { trace!("IO Frame: {:?}", frame); let res = match frame { Frame::Message(response) => { - trace!("Prepare message status={:?}", response.status); - // run middlewares let mut response = if let Some(middlewares) = self.middlewares.take() {