From 85c0a15c687b5b5ccd44681dae356a718041b757 Mon Sep 17 00:00:00 2001 From: Ask Solem Date: Tue, 20 May 2014 18:41:44 +0100 Subject: [PATCH] Adds sanitize_url and as_url to kombu.utils.url Conflicts: kombu/utils/url.py --- kombu/connection.py | 40 +++++++++++----------------------------- kombu/utils/url.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/kombu/connection.py b/kombu/connection.py index 85b8f5e9..c49c5b4b 100644 --- a/kombu/connection.py +++ b/kombu/connection.py @@ -11,13 +11,8 @@ import os import socket from contextlib import contextmanager -from functools import partial from itertools import count, cycle from operator import itemgetter -try: - from urllib.parse import quote -except ImportError: # Py2 - from urllib import quote # noqa # jython breaks on relative import for .exceptions for some reason # (Issue #112) @@ -28,7 +23,7 @@ from .transport import get_transport_cls, supports_librabbitmq from .utils import cached_property, retry_over_time, shufflecycle, HashedSeq from .utils.compat import OrderedDict from .utils.functional import lazy -from .utils.url import parse_url, urlparse +from .utils.url import as_url, parse_url, quote, urlparse __all__ = ['Connection', 'ConnectionPool', 'ChannelPool'] @@ -569,36 +564,23 @@ class Connection(object): self.password, self.virtual_host, self.port, repr(self.transport_options)) - def as_uri(self, include_password=False, mask=''): + def as_uri(self, include_password=False, mask='**', + getfields=itemgetter('port', 'userid', 'password', + 'virtual_host', 'transport')): """Convert connection parameters to URL form.""" hostname = self.hostname or 'localhost' if self.transport.can_parse_url: if self.uri_prefix: return '%s+%s' % (self.uri_prefix, hostname) return self.hostname - quoteS = partial(quote, safe='') # strict quote fields = self.info() - port, userid, password, transport = itemgetter( - 'port', 'userid', 'password', 'transport' - )(fields) - url = '%s://' % transport - if userid or password: - if userid: - url += quoteS(userid) - if password: - if include_password: - url += ':' + quoteS(password) - else: - url += ':' + mask if mask else '' - url += '@' - url += quoteS(fields['hostname']) - if port: - url += ':%s' % (port, ) - - url += '/' + quote(fields['virtual_host']) - if self.uri_prefix: - return '%s+%s' % (self.uri_prefix, url) - return url + port, userid, password, vhost, transport = getfields(fields) + scheme = ('{0}+{1}'.format(self.uri_prefix, transport) + if self.uri_prefix else transport) + return as_url( + scheme, hostname, port, userid, password, quote(vhost), + sanitize=not include_password, mask=mask, + ) def Pool(self, limit=None, preload=None): """Pool of connections. diff --git a/kombu/utils/url.py b/kombu/utils/url.py index 3eebe345..adc03004 100644 --- a/kombu/utils/url.py +++ b/kombu/utils/url.py @@ -1,13 +1,17 @@ from __future__ import absolute_import +from functools import partial + try: - from urllib.parse import unquote, urlparse, parse_qsl + from urllib.parse import parse_qsl, quote, unquote, urlparse except ImportError: - from urllib import unquote # noqa + from urllib import quote, unquote # noqa from urlparse import urlparse, parse_qsl # noqa from . import kwdict +safequote = partial(quote, safe='') + def _parse_url(url): scheme = urlparse(url).scheme @@ -28,3 +32,26 @@ def parse_url(url): return dict(transport=scheme, hostname=host, port=port, userid=user, password=password, virtual_host=path, **query) + + +def as_url(scheme, host=None, port=None, user=None, password=None, + path=None, query=None, sanitize=False, mask='**'): + parts = ['{0}://'.format(scheme)] + if user or password: + if user: + parts.append(safequote(user)) + if password: + if sanitize: + parts.extend([':', mask] if mask else [':']) + else: + parts.extend([':', safequote(password)]) + parts.append('@') + parts.append(safequote(host)) + if port: + parts.extend([':', port]) + parts.extend(['/', path]) + return ''.join(str(part) for part in parts if part) + + +def sanitize_url(url, mask='**'): + return as_url(*_parse_url(url), sanitize=True, mask=mask)