diff --git a/CHANGES.md b/CHANGES.md index f4cec6a6..0d36f63f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,8 @@ * Content compression/decompression (br, gzip, deflate) +* Refactor logger middleware + ## 0.2.1 (2017-11-03) diff --git a/Cargo.toml b/Cargo.toml index eb483647..e25b983d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ cookie = { version="0.10", features=["percent-encode", "secure"] } regex = "0.2" sha1 = "0.2" url = "1.5" +libc = "^0.2" flate2 = "0.2" brotli2 = "^0.3.2" percent-encoding = "1.0" diff --git a/src/h1.rs b/src/h1.rs index ca13aa67..3da5dc0c 100644 --- a/src/h1.rs +++ b/src/h1.rs @@ -173,7 +173,7 @@ impl Http1 not_ready = false; // set remote addr - req.set_remove_addr(self.addr.clone()); + req.set_remove_addr(self.addr); // stop keepalive timer self.keepalive_timer.take(); diff --git a/src/h2.rs b/src/h2.rs index 039a057b..618cdca3 100644 --- a/src/h2.rs +++ b/src/h2.rs @@ -140,7 +140,7 @@ impl Http2 self.keepalive_timer.take(); self.tasks.push_back( - Entry::new(parts, body, resp, self.addr.clone(), &self.router)); + Entry::new(parts, body, resp, self.addr, &self.router)); } Ok(Async::NotReady) => { // start keep-alive timer diff --git a/src/httprequest.rs b/src/httprequest.rs index e75131e5..e1310fff 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -75,11 +75,13 @@ impl HttpRequest { pub fn method(&self) -> &Method { &self.method } /// Read the Request Version. + #[inline] pub fn version(&self) -> Version { self.version } /// Read the Request Headers. + #[inline] pub fn headers(&self) -> &HeaderMap { &self.headers } @@ -97,6 +99,7 @@ impl HttpRequest { /// - Forwarded /// - X-Forwarded-For /// - peername of opened socket + #[inline] pub fn remote(&self) -> Option<&SocketAddr> { self.addr.as_ref() } diff --git a/src/lib.rs b/src/lib.rs index 32380a65..95a8a53f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ extern crate http_range; extern crate mime; extern crate mime_guess; extern crate url; +extern crate libc; extern crate flate2; extern crate brotli2; extern crate percent_encoding; diff --git a/src/middlewares/logger.rs b/src/middlewares/logger.rs index f0d11c89..8131d72e 100644 --- a/src/middlewares/logger.rs +++ b/src/middlewares/logger.rs @@ -3,6 +3,7 @@ use std::env; use std::fmt; use std::fmt::{Display, Formatter}; +use libc; use time; use regex::Regex; @@ -100,11 +101,18 @@ impl Logger { }, FormatText::ResponseStatus => resp.status().as_u16().fmt(fmt), FormatText::ResponseSize => resp.response_size().fmt(fmt), + FormatText::Pid => unsafe{libc::getpid().fmt(fmt)}, FormatText::Time => fmt.write_fmt(format_args!("{:.6}", response_time_ms/1000.0)), FormatText::TimeMillis => fmt.write_fmt(format_args!("{:.6}", response_time_ms)), - FormatText::RemoteAddr => Ok(()), //req.remote_addr.fmt(fmt), + FormatText::RemoteAddr => { + if let Some(addr) = req.remote() { + addr.fmt(fmt) + } else { + "-".fmt(fmt) + } + } FormatText::RequestTime => { entry_time.strftime("[%d/%b/%Y:%H:%M:%S %z]") .unwrap() @@ -205,7 +213,7 @@ impl Format { "%" => FormatText::Percent, "a" => FormatText::RemoteAddr, "t" => FormatText::RequestTime, - "P" => FormatText::Percent, + "P" => FormatText::Pid, "r" => FormatText::RequestLine, "s" => FormatText::ResponseStatus, "b" => FormatText::ResponseSize, @@ -251,6 +259,7 @@ impl<'a> ContextDisplay<'a> for Format { #[derive(Debug, Clone)] pub enum FormatText { Str(String), + Pid, Percent, RequestLine, RequestTime, diff --git a/src/task.rs b/src/task.rs index 494d2236..810e92eb 100644 --- a/src/task.rs +++ b/src/task.rs @@ -279,9 +279,8 @@ impl Task { // finish middlewares if let Some(ref mut resp) = self.prepared { resp.set_response_size(io.written()); - match self.middlewares.finishing(req, resp) { - Ok(Async::NotReady) => return Ok(Async::NotReady), - _ => (), + if let Ok(Async::NotReady) = self.middlewares.finishing(req, resp) { + return Ok(Async::NotReady) } } Ok(Async::Ready(self.state.is_done()))