From be3a1ab7705cd26bfd7fd798db6a6357c1cba608 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 10 Nov 2017 13:26:12 -0800 Subject: [PATCH] use remote addr in logger if available --- CHANGES.md | 2 ++ Cargo.toml | 1 + src/h1.rs | 2 +- src/h2.rs | 2 +- src/httprequest.rs | 3 +++ src/lib.rs | 1 + src/middlewares/logger.rs | 13 +++++++++++-- src/task.rs | 5 ++--- 8 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index f4cec6a66..0d36f63f3 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 eb483647d..e25b983d2 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 ca13aa67d..3da5dc0ca 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 039a057b6..618cdca3a 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 e75131e5b..e1310fff0 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 32380a65a..95a8a53f6 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 f0d11c893..8131d72e3 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 494d22360..810e92ebd 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()))