Made colons quote-safe in logs; mainly for ipv6

Previous logging made a mess of ipv6 addresses. This just makes the
colon quote-safe so it passes through unscathed. The logs will still
be backward compatible because unquote don't care:

    $ python
    >>> from urllib import quote, unquote
    >>> quote('2001:db8:85a3:8d3:1319:8a2e:370:7348')
    '2001%3Adb8%3A85a3%3A8d3%3A1319%3A8a2e%3A370%3A7348'
    >>> unquote(quote('2001:db8:85a3:8d3:1319:8a2e:370:7348'))
    '2001:db8:85a3:8d3:1319:8a2e:370:7348'
    >>> quote('2001:db8:85a3:8d3:1319:8a2e:370:7348', '/:')
    '2001:db8:85a3:8d3:1319:8a2e:370:7348'
    >>> unquote(quote('2001:db8:85a3:8d3:1319:8a2e:370:7348', '/:'))
    '2001:db8:85a3:8d3:1319:8a2e:370:7348'

Change-Id: Ia13a9bc8a320cde5c56034a7f0b645913428bf21
This commit is contained in:
gholt 2013-04-20 04:16:57 +00:00
parent f63dc07b9d
commit cd8af3f8f1
2 changed files with 22 additions and 2 deletions

View File

@ -66,6 +66,8 @@ from swift.common.utils import (get_logger, get_remote_client,
get_valid_utf8_str, config_true_value,
InputProxy)
QUOTE_SAFE = '/:'
class ProxyLoggingMiddleware(object):
"""
@ -122,7 +124,7 @@ class ProxyLoggingMiddleware(object):
if self.req_already_logged(req):
return
req_path = get_valid_utf8_str(req.path)
the_request = quote(unquote(req_path))
the_request = quote(unquote(req_path), QUOTE_SAFE)
if req.query_string:
the_request = the_request + '?' + req.query_string
logged_headers = None
@ -131,7 +133,7 @@ class ProxyLoggingMiddleware(object):
for k, v in req.headers.items())
method = self.method_from_req(req)
self.access_logger.info(' '.join(
quote(str(x) if x else '-')
quote(str(x) if x else '-', QUOTE_SAFE)
for x in (
get_remote_client(req),
req.remote_addr,

View File

@ -519,6 +519,24 @@ class TestProxyLogging(unittest.TestCase):
self.assertEquals(resp_body, 'FAKE APP')
self.assertEquals(log_parts[11], str(len(resp_body)))
def test_ipv6(self):
ipv6addr = '2001:db8:85a3:8d3:1319:8a2e:370:7348'
app = proxy_logging.ProxyLoggingMiddleware(FakeApp(), {})
app.access_logger = FakeLogger()
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
req.remote_addr = ipv6addr
resp = app(req.environ, start_response)
resp_body = ''.join(resp)
log_parts = self._log_parts(app)
self.assertEquals(log_parts[0], ipv6addr)
self.assertEquals(log_parts[1], ipv6addr)
self.assertEquals(log_parts[3], 'GET')
self.assertEquals(log_parts[4], '/')
self.assertEquals(log_parts[5], 'HTTP/1.0')
self.assertEquals(log_parts[6], '200')
self.assertEquals(resp_body, 'FAKE APP')
self.assertEquals(log_parts[11], str(len(resp_body)))
if __name__ == '__main__':
unittest.main()