Skip to content

Commit

Permalink
potential fix for #189
Browse files Browse the repository at this point in the history
  • Loading branch information
tobixen committed May 25, 2022
1 parent 0ce851d commit e5b5fa2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
28 changes: 20 additions & 8 deletions caldav/lib/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from six import PY3
from caldav.lib.python_utilities import to_unicode, to_normal_str
if PY3:
from urllib.parse import ParseResult, SplitResult, urlparse
from urllib.parse import ParseResult, SplitResult, urlparse, unquote, quote, urlunparse
else:
from urlparse import ParseResult, SplitResult
from urlparse import urlparse
from urlparse import urlparse, urlunparse
from urllib import unquote, quote


def uc2utf8(input):
Expand Down Expand Up @@ -133,21 +134,32 @@ def canonical(self):
"""
a canonical URL ... remove authentication details, make sure there
are no double slashes, and to make sure the URL is always the same,
run it through the urlparser
run it through the urlparser, and make sure path is properly quoted
"""
url = self.unauth()

# this is actually already done in the unauth method ...
if '//' in url.path:
raise NotImplementedError("remove the double slashes")

# This looks like a noop - but it may have the side effect
# that urlparser be run (actually not - unauth ensures we
# have an urlParseResult object)
url.scheme
arr = list(self.url_parsed)
## quoting path
arr[2] = quote(unquote(url.path))
## sensible defaults
if not arr[0]:
arr[0] = 'https'
if arr[1] and not ':' in arr[1]:
if arr[0] == 'https':
portpart = ':443'
elif arr[0] == 'http':
portpart = ':80'
else:
portpart = ''
arr[1] += portpart

# make sure to delete the string version
url.url_raw = None
url.url_raw = urlunparse(arr)
url.url_parsed = None

return url

Expand Down
4 changes: 2 additions & 2 deletions caldav/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def children(self, type=None):
# And why is the strip_trailing_slash-method needed?
# The collection URL should always end with a slash according
# to RFC 2518, section 5.2.
if (self.url.strip_trailing_slash() !=
self.url.join(path).strip_trailing_slash()):
if (self.url.canonical().strip_trailing_slash() !=
self.url.join(path).canonical().strip_trailing_slash()):
c.append((self.url.join(path), resource_types,
resource_name))

Expand Down
3 changes: 3 additions & 0 deletions tests/test_caldav_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,9 @@ def testURL(self):
assert_equal(URL('http://www.example.com:8080/bar/').strip_trailing_slash(), URL('http://www.example.com:8080/bar'))
assert_equal(URL('http://www.example.com:8080/bar/').strip_trailing_slash(), URL('http://www.example.com:8080/bar').strip_trailing_slash())

# 9) canonical
assert_equal(URL('https://www.example.com:443/b%61r/').canonical(), URL('//www.example.com/bar/').canonical())

def testFilters(self):
filter = \
cdav.Filter().append(
Expand Down

0 comments on commit e5b5fa2

Please sign in to comment.