1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-17 13:46:36 +00:00

add custom Debug impl for HttpResponse

This commit is contained in:
Nikolay Kim 2017-11-08 19:31:25 -08:00
parent 1392b2b171
commit 02fb424659
7 changed files with 41 additions and 13 deletions

View file

@ -13,12 +13,15 @@ use actix_web::*;
/// somple handle /// somple handle
fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse { fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse {
println!("{:?}", req); println!("{:?}", req);
httpcodes::HTTPOk.with_body("Welcome!") httpcodes::HTTPOk
.builder()
.content_type("text/plain")
.body("Welcome!").unwrap()
} }
fn main() { fn main() {
if ::std::env::var("RUST_LOG").is_err() { 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 _ = env_logger::init();
let sys = actix::System::new("ws-example"); let sys = actix::System::new("ws-example");

View file

@ -83,8 +83,10 @@ impl<T, A, H> Http1<T, A, H>
// keep-alive timer // keep-alive timer
if let Some(ref mut timeout) = self.keepalive_timer { if let Some(ref mut timeout) = self.keepalive_timer {
match timeout.poll() { match timeout.poll() {
Ok(Async::Ready(_)) => Ok(Async::Ready(_)) => {
return Ok(Async::Ready(Http1Result::Done)), trace!("Keep-alive timeout, close connection");
return Ok(Async::Ready(Http1Result::Done))
}
Ok(Async::NotReady) => (), Ok(Async::NotReady) => (),
Err(_) => unreachable!(), Err(_) => unreachable!(),
} }

View file

@ -101,7 +101,7 @@ impl<T: AsyncWrite> Writer for H1Writer<T> {
fn start(&mut self, req: &mut HttpRequest, msg: &mut HttpResponse) fn start(&mut self, req: &mut HttpRequest, msg: &mut HttpResponse)
-> Result<WriterState, io::Error> -> Result<WriterState, io::Error>
{ {
trace!("Prepare message status={:?}", msg.status); trace!("Prepare response with status: {:?}", msg.status);
// prepare task // prepare task
self.started = true; self.started = true;
@ -164,6 +164,8 @@ impl<T: AsyncWrite> Writer for H1Writer<T> {
buffer.extend(b"\r\n"); buffer.extend(b"\r\n");
} }
trace!("Response: {:?}", msg);
if msg.body().is_binary() { if msg.body().is_binary() {
let body = msg.replace_body(Body::Empty); let body = msg.replace_body(Body::Empty);
if let Body::Binary(bytes) = body { if let Body::Binary(bytes) = body {

View file

@ -61,12 +61,13 @@ impl<T, A, H> Http2<T, A, H>
pub fn poll(&mut self) -> Poll<(), ()> { pub fn poll(&mut self) -> Poll<(), ()> {
// server // server
if let State::Server(ref mut server) = self.state { if let State::Server(ref mut server) = self.state {
// keep-alive timer // keep-alive timer
if let Some(ref mut timeout) = self.keepalive_timer { if let Some(ref mut timeout) = self.keepalive_timer {
match timeout.poll() { match timeout.poll() {
Ok(Async::Ready(_)) => Ok(Async::Ready(_)) => {
return Ok(Async::Ready(())), trace!("Keep-alive timeout, close connection");
return Ok(Async::Ready(()))
}
Ok(Async::NotReady) => (), Ok(Async::NotReady) => (),
Err(_) => unreachable!(), Err(_) => unreachable!(),
} }

View file

@ -101,7 +101,7 @@ impl Writer for H2Writer {
fn start(&mut self, req: &mut HttpRequest, msg: &mut HttpResponse) fn start(&mut self, req: &mut HttpRequest, msg: &mut HttpResponse)
-> Result<WriterState, io::Error> -> Result<WriterState, io::Error>
{ {
trace!("Prepare message status={:?}", msg); trace!("Prepare response with status: {:?}", msg.status);
// prepare response // prepare response
self.started = true; self.started = true;
@ -140,6 +140,8 @@ impl Writer for H2Writer {
return Err(io::Error::new(io::ErrorKind::Other, "err")), return Err(io::Error::new(io::ErrorKind::Other, "err")),
} }
trace!("Response: {:?}", msg);
if msg.body().is_binary() { if msg.body().is_binary() {
if let Body::Binary(bytes) = msg.replace_body(Body::Empty) { if let Body::Binary(bytes) = msg.replace_body(Body::Empty) {
self.eof = true; self.eof = true;

View file

@ -1,5 +1,5 @@
//! Pieces pertaining to the HTTP message protocol. //! 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::error::Error as Error;
use std::convert::Into; use std::convert::Into;
@ -23,7 +23,6 @@ pub enum ConnectionType {
Upgrade, Upgrade,
} }
#[derive(Debug)]
/// An HTTP Response /// An HTTP Response
pub struct HttpResponse { pub struct HttpResponse {
pub version: Option<Version>, pub version: Option<Version>,
@ -215,6 +214,27 @@ impl From<HttpResponse> 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)] #[derive(Debug)]
struct Parts { struct Parts {
version: Option<Version>, version: Option<Version>,

View file

@ -197,8 +197,6 @@ impl Task {
trace!("IO Frame: {:?}", frame); trace!("IO Frame: {:?}", frame);
let res = match frame { let res = match frame {
Frame::Message(response) => { Frame::Message(response) => {
trace!("Prepare message status={:?}", response.status);
// run middlewares // run middlewares
let mut response = let mut response =
if let Some(middlewares) = self.middlewares.take() { if let Some(middlewares) = self.middlewares.take() {