mirror of
https://github.com/actix/actix-web.git
synced 2024-12-17 05:36:36 +00:00
add custom Debug impl for HttpResponse
This commit is contained in:
parent
1392b2b171
commit
02fb424659
7 changed files with 41 additions and 13 deletions
|
@ -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");
|
||||
|
|
|
@ -83,8 +83,10 @@ impl<T, A, H> Http1<T, A, H>
|
|||
// 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!(),
|
||||
}
|
||||
|
|
|
@ -101,7 +101,7 @@ impl<T: AsyncWrite> Writer for H1Writer<T> {
|
|||
fn start(&mut self, req: &mut HttpRequest, msg: &mut HttpResponse)
|
||||
-> Result<WriterState, io::Error>
|
||||
{
|
||||
trace!("Prepare message status={:?}", msg.status);
|
||||
trace!("Prepare response with status: {:?}", msg.status);
|
||||
|
||||
// prepare task
|
||||
self.started = true;
|
||||
|
@ -164,6 +164,8 @@ impl<T: AsyncWrite> Writer for H1Writer<T> {
|
|||
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 {
|
||||
|
|
|
@ -61,12 +61,13 @@ impl<T, A, H> Http2<T, A, H>
|
|||
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!(),
|
||||
}
|
||||
|
|
|
@ -101,7 +101,7 @@ impl Writer for H2Writer {
|
|||
fn start(&mut self, req: &mut HttpRequest, msg: &mut HttpResponse)
|
||||
-> Result<WriterState, io::Error>
|
||||
{
|
||||
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;
|
||||
|
|
|
@ -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<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)]
|
||||
struct Parts {
|
||||
version: Option<Version>,
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue