1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

use remote addr in logger if available

This commit is contained in:
Nikolay Kim 2017-11-10 13:26:12 -08:00
parent f369d9af0e
commit be3a1ab770
8 changed files with 22 additions and 7 deletions

View File

@ -9,6 +9,8 @@
* Content compression/decompression (br, gzip, deflate) * Content compression/decompression (br, gzip, deflate)
* Refactor logger middleware
## 0.2.1 (2017-11-03) ## 0.2.1 (2017-11-03)

View File

@ -44,6 +44,7 @@ cookie = { version="0.10", features=["percent-encode", "secure"] }
regex = "0.2" regex = "0.2"
sha1 = "0.2" sha1 = "0.2"
url = "1.5" url = "1.5"
libc = "^0.2"
flate2 = "0.2" flate2 = "0.2"
brotli2 = "^0.3.2" brotli2 = "^0.3.2"
percent-encoding = "1.0" percent-encoding = "1.0"

View File

@ -173,7 +173,7 @@ impl<T, H> Http1<T, H>
not_ready = false; not_ready = false;
// set remote addr // set remote addr
req.set_remove_addr(self.addr.clone()); req.set_remove_addr(self.addr);
// stop keepalive timer // stop keepalive timer
self.keepalive_timer.take(); self.keepalive_timer.take();

View File

@ -140,7 +140,7 @@ impl<T, H> Http2<T, H>
self.keepalive_timer.take(); self.keepalive_timer.take();
self.tasks.push_back( 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) => { Ok(Async::NotReady) => {
// start keep-alive timer // start keep-alive timer

View File

@ -75,11 +75,13 @@ impl HttpRequest {
pub fn method(&self) -> &Method { &self.method } pub fn method(&self) -> &Method { &self.method }
/// Read the Request Version. /// Read the Request Version.
#[inline]
pub fn version(&self) -> Version { pub fn version(&self) -> Version {
self.version self.version
} }
/// Read the Request Headers. /// Read the Request Headers.
#[inline]
pub fn headers(&self) -> &HeaderMap { pub fn headers(&self) -> &HeaderMap {
&self.headers &self.headers
} }
@ -97,6 +99,7 @@ impl HttpRequest {
/// - Forwarded /// - Forwarded
/// - X-Forwarded-For /// - X-Forwarded-For
/// - peername of opened socket /// - peername of opened socket
#[inline]
pub fn remote(&self) -> Option<&SocketAddr> { pub fn remote(&self) -> Option<&SocketAddr> {
self.addr.as_ref() self.addr.as_ref()
} }

View File

@ -18,6 +18,7 @@ extern crate http_range;
extern crate mime; extern crate mime;
extern crate mime_guess; extern crate mime_guess;
extern crate url; extern crate url;
extern crate libc;
extern crate flate2; extern crate flate2;
extern crate brotli2; extern crate brotli2;
extern crate percent_encoding; extern crate percent_encoding;

View File

@ -3,6 +3,7 @@ use std::env;
use std::fmt; use std::fmt;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use libc;
use time; use time;
use regex::Regex; use regex::Regex;
@ -100,11 +101,18 @@ impl Logger {
}, },
FormatText::ResponseStatus => resp.status().as_u16().fmt(fmt), FormatText::ResponseStatus => resp.status().as_u16().fmt(fmt),
FormatText::ResponseSize => resp.response_size().fmt(fmt), FormatText::ResponseSize => resp.response_size().fmt(fmt),
FormatText::Pid => unsafe{libc::getpid().fmt(fmt)},
FormatText::Time => FormatText::Time =>
fmt.write_fmt(format_args!("{:.6}", response_time_ms/1000.0)), fmt.write_fmt(format_args!("{:.6}", response_time_ms/1000.0)),
FormatText::TimeMillis => FormatText::TimeMillis =>
fmt.write_fmt(format_args!("{:.6}", response_time_ms)), 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 => { FormatText::RequestTime => {
entry_time.strftime("[%d/%b/%Y:%H:%M:%S %z]") entry_time.strftime("[%d/%b/%Y:%H:%M:%S %z]")
.unwrap() .unwrap()
@ -205,7 +213,7 @@ impl Format {
"%" => FormatText::Percent, "%" => FormatText::Percent,
"a" => FormatText::RemoteAddr, "a" => FormatText::RemoteAddr,
"t" => FormatText::RequestTime, "t" => FormatText::RequestTime,
"P" => FormatText::Percent, "P" => FormatText::Pid,
"r" => FormatText::RequestLine, "r" => FormatText::RequestLine,
"s" => FormatText::ResponseStatus, "s" => FormatText::ResponseStatus,
"b" => FormatText::ResponseSize, "b" => FormatText::ResponseSize,
@ -251,6 +259,7 @@ impl<'a> ContextDisplay<'a> for Format {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum FormatText { pub enum FormatText {
Str(String), Str(String),
Pid,
Percent, Percent,
RequestLine, RequestLine,
RequestTime, RequestTime,

View File

@ -279,9 +279,8 @@ impl Task {
// finish middlewares // finish middlewares
if let Some(ref mut resp) = self.prepared { if let Some(ref mut resp) = self.prepared {
resp.set_response_size(io.written()); resp.set_response_size(io.written());
match self.middlewares.finishing(req, resp) { if let Ok(Async::NotReady) = self.middlewares.finishing(req, resp) {
Ok(Async::NotReady) => return Ok(Async::NotReady), return Ok(Async::NotReady)
_ => (),
} }
} }
Ok(Async::Ready(self.state.is_done())) Ok(Async::Ready(self.state.is_done()))