Commit d69c758a authored by Vincent Pelletier's avatar Vincent Pelletier

http: Make logging closer to apache default format

Escape all quoted strings.
Add referrer.
Add user-agent.
parent de10e327
......@@ -93,6 +93,25 @@ class ThreadingWSGIServer(ThreadingMixIn, WSGIServer):
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
WSGIServer.server_bind(self)
def _buildQuoteCharList():
# All chars considered guilty
result = ['\\x%02x' % x for x in xrange(256)]
# Exception for non-printable whitespaces.
result[ord('\b')] = '\\b'
result[ord('\n')] = '\\n'
result[ord('\r')] = '\\r'
result[ord('\t')] = '\\t'
result[ord('\v')] = '\\v'
# Exception for printable chars
for x in xrange(0x20, 0x7f):
result[x] = chr(x)
# Exception in the exception: chars which need escapement
result[ord('\\')] = '\\\\'
result[ord('"')] = '\\"'
return result
_QUOTE_LOG_LIST = _buildQuoteCharList()
del _buildQuoteCharList
class CaucaseWSGIRequestHandler(WSGIRequestHandler):
"""
Make WSGIRequestHandler logging more apache-like.
......@@ -114,6 +133,25 @@ class CaucaseWSGIRequestHandler(WSGIRequestHandler):
'%d/' + self.monthname[now.month] + '/%Y:%H:%M:%S +0000',
)
def log_request(self, code='-', size='-'):
"""
Log request. Happens after response was sent.
Compared to python(s default (from BaseHTTPServer):
- log referrer
- log user agent
- escaping
"""
headers = getattr(self, 'headers', {})
self.log_message(
'"%s" %s %s "%s" "%s"',
''.join(_QUOTE_LOG_LIST[ord(x)] for x in self.requestline),
code,
size,
''.join(_QUOTE_LOG_LIST[ord(x)] for x in headers.get('Referrer', '-')),
''.join(_QUOTE_LOG_LIST[ord(x)] for x in headers.get('User-Agent', '-')),
)
class CaucaseSSLWSGIRequestHandler(CaucaseWSGIRequestHandler):
"""
Add SSL-specific entries to environ:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment