Skip to content

Commit

Permalink
Add X-Forward-For option to Access Log aio-libs#2123
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-klein committed Jul 23, 2017
1 parent 240a0e2 commit 410a361
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ class AccessLogger:
Format:
%% The percent sign
%a Remote IP-address (IP-address of proxy if using reverse proxy)
%f X-Foward-For or Remote IP-address
%t Time when the request was started to process
%P The process ID of the child that serviced the request
%r First line of request
Expand All @@ -322,6 +323,7 @@ class AccessLogger:
"""
LOG_FORMAT_MAP = {
'a': 'remote_address',
'f': 'x_forwarded_for',
't': 'request_time',
'P': 'process_id',
'r': 'first_request_line',
Expand All @@ -336,7 +338,7 @@ class AccessLogger:
}

LOG_FORMAT = '%a %l %u %t "%r" %s %b "%{Referrer}i" "%{User-Agent}i"'
FORMAT_RE = re.compile(r'%(\{([A-Za-z0-9\-_]+)\}([ioe])|[atPrsbOD]|Tf?)')
FORMAT_RE = re.compile(r'%(\{([A-Za-z0-9\-_]+)\}([ioe])|[aftPrsbOD]|Tf?)')
CLEANUP_RE = re.compile(r'(%[^s])')
_FORMAT_CACHE = {}

Expand Down Expand Up @@ -429,6 +431,13 @@ def _format_a(args):
else:
return peername

@staticmethod
def _format_f(args):
result = args[0].headers.get("X-Forwarded-For")
if result is None:
return AccessLogger._format_a(args)
return result

@staticmethod
def _format_t(args):
return datetime.datetime.utcnow().strftime('[%d/%b/%Y:%H:%M:%S +0000]')
Expand Down

0 comments on commit 410a361

Please sign in to comment.