Skip to content

Commit

Permalink
[#553] Catch exceptions by urlparse with horrible URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Mar 5, 2013
1 parent 6e990b3 commit d13220c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions ckan/lib/datapreview.py
Expand Up @@ -22,10 +22,14 @@ def compare_domains(urls):
for url in urls:
# all urls are interpreted as absolute urls,
# except for urls that start with a /
if not urlparse.urlparse(url).scheme and not url.startswith('/'):
url = '//' + url
parsed = urlparse.urlparse(url.lower(), 'http')
domain = (parsed.scheme, parsed.hostname, parsed.port)
try:
if not urlparse.urlparse(url).scheme and not url.startswith('/'):
url = '//' + url
parsed = urlparse.urlparse(url.lower(), 'http')
domain = (parsed.scheme, parsed.hostname, parsed.port)
except ValueError:
# URL is so messed up that even urlparse can't stand it
return False

if not first_domain:
first_domain = domain
Expand Down
3 changes: 3 additions & 0 deletions ckan/tests/lib/test_datapreview.py
Expand Up @@ -18,3 +18,6 @@ def test_compare_domains(self):
assert comp(['http://de.okfn.org', 'http://www.okfn.org']) == False

assert comp(['http://de.okfn.org', 'http:www.foo.com']) == False

# Wrong URL. Makes urlparse choke
assert comp(['http://Server=cda3; Service=sde:sqlserver:cda3; Database=NationalDatasets; User=sde; Version=sde.DEFAULT', 'http://www.okf.org']) == False

0 comments on commit d13220c

Please sign in to comment.