Skip to content

Commit

Permalink
Fix IPv6 address parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkins committed Jan 20, 2017
1 parent 471a786 commit be4c7f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
13 changes: 7 additions & 6 deletions dj_database_url.py
Expand Up @@ -87,13 +87,14 @@ def parse(url, engine=None, conn_max_age=0):
path = ':memory:'

# Handle postgres percent-encoded paths.
netloc = url.netloc
if "@" in netloc:
netloc = netloc.rsplit("@", 1)[1]
if ":" in netloc:
netloc = netloc.split(":", 1)[0]
hostname = netloc or ''
hostname = url.hostname or ''
if '%2f' in hostname.lower():
# Switch to url.netloc to avoid lower cased paths
hostname = url.netloc
if "@" in hostname:
hostname = hostname.rsplit("@", 1)[1]
if ":" in hostname:
hostname = hostname.split(":", 1)[0]
hostname = hostname.replace('%2f', '/').replace('%2F', '/')

# Update with environment configuration.
Expand Down
11 changes: 11 additions & 0 deletions test_dj_database_url.py
Expand Up @@ -43,6 +43,17 @@ def test_postgres_unix_socket_parsing(self):
assert url['PASSWORD'] == ''
assert url['PORT'] == ''

def test_ipv6_parsing(self):
url = 'postgres://ieRaekei9wilaim7:wegauwhgeuioweg@[2001:db8:1234::1234:5678:90af]:5431/d8r82722r2kuvn'
url = dj_database_url.parse(url)

assert url['ENGINE'] == 'django.db.backends.postgresql_psycopg2'
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == '2001:db8:1234::1234:5678:90af'
assert url['USER'] == 'ieRaekei9wilaim7'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431

def test_postgres_search_path_parsing(self):
url = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?currentSchema=otherschema'
url = dj_database_url.parse(url)
Expand Down

0 comments on commit be4c7f2

Please sign in to comment.