Adds sanitize_url and as_url to kombu.utils.url

Conflicts:
	kombu/utils/url.py
This commit is contained in:
Ask Solem 2014-05-20 18:41:44 +01:00
parent 3dad9e7aac
commit 85c0a15c68
2 changed files with 40 additions and 31 deletions

View File

@ -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.

View File

@ -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)