Skip to content

Commit

Permalink
url.flatten_path: Handle '.' & '.'-related paths.
Browse files Browse the repository at this point in the history
Closes #241
  • Loading branch information
chfoo committed Feb 24, 2015
1 parent e48af67 commit 4f35c51
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ What's New
* Updated certificate bundle.
* Fixed TypeError crash on bad Meta Refresh HTML element.
* Fixed unable to fetch FTP files with spaces and other special characters.
* Fixed AssertionError fetching URLs with trailing dot not properly removed.
* Added ``--no-cache``.
* Added ``--report-speed``.

Expand Down
8 changes: 6 additions & 2 deletions wpull/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,11 @@ def flatten_path(path, flatten_slashes=False):
'''
# Based on posixpath.normpath

# Fast path
if not path or path == '/':
return '/'

# Take off leading slash
if path[0] == '/':
path = path[1:]

Expand All @@ -682,10 +684,12 @@ def flatten_path(path, flatten_slashes=False):
new_parts.append(part)
elif new_parts:
new_parts.pop()

if flatten_slashes and path.endswith('/'):

# If the filename is empty string
if flatten_slashes and path.endswith('/') or not len(new_parts):
new_parts.append('')

# Put back leading slash
new_parts.appendleft('')

return '/'.join(new_parts)
10 changes: 10 additions & 0 deletions wpull/url_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ def test_url_info_invalids(self):
ValueError, URLInfo.parse, 'http://[.]/python.xml%22')

def test_url_info_path_folding(self):
self.assertEqual(
'http://example.com/',
URLInfo.parse('http://example.com/.').url
)
self.assertEqual(
'http://example.com/',
URLInfo.parse('http://example.com/../').url
Expand Down Expand Up @@ -654,6 +658,12 @@ def test_flatten_path(self):
flatten_slashes=True))
self.assertEqual('/a', flatten_path('a'))
self.assertEqual('/a/', flatten_path('a/'))
self.assertEqual('/', flatten_path('.'))
self.assertEqual('/', flatten_path('./'))
self.assertEqual('/', flatten_path('/.'))
self.assertEqual('/', flatten_path('/..'))
self.assertEqual('/', flatten_path('../'))
self.assertEqual('/', flatten_path('./.'))
self.assertEqual('/', flatten_path('/'))
self.assertEqual('/', flatten_path('/../../../'))
self.assertEqual('/', flatten_path('/.././'))
Expand Down

0 comments on commit 4f35c51

Please sign in to comment.