urlunparse(): Do not add a leading slash to the path if it is empty.

urljoin():  Make this conform to RFC 1808 for all examples given in that
            RFC (both "Normal" and "Abnormal"), so long as that RFC does
            not conflict the older RFC 1630, which also specified
            relative URL resolution.

This closes SF bug #110832 (Jitterbug PR#194).
This commit is contained in:
Fred Drake 2001-01-05 05:54:41 +00:00
parent b8584e0894
commit 867952f6e4

View file

@ -114,7 +114,7 @@ def urlunparse((scheme, netloc, url, params, query, fragment)):
originally had redundant delimiters, e.g. a ? with an empty query originally had redundant delimiters, e.g. a ? with an empty query
(the draft states that these are equivalent).""" (the draft states that these are equivalent)."""
if netloc or (scheme in uses_netloc and url[:2] == '//'): if netloc or (scheme in uses_netloc and url[:2] == '//'):
if url[:1] != '/': url = '/' + url if url and url[:1] != '/': url = '/' + url
url = '//' + (netloc or '') + url url = '//' + (netloc or '') + url
if scheme: if scheme:
url = scheme + ':' + url url = scheme + ':' + url
@ -131,13 +131,14 @@ def urljoin(base, url, allow_fragments = 1):
interpretation of the latter.""" interpretation of the latter."""
if not base: if not base:
return url return url
if not url:
return base
bscheme, bnetloc, bpath, bparams, bquery, bfragment = \ bscheme, bnetloc, bpath, bparams, bquery, bfragment = \
urlparse(base, '', allow_fragments) urlparse(base, '', allow_fragments)
scheme, netloc, path, params, query, fragment = \ scheme, netloc, path, params, query, fragment = \
urlparse(url, bscheme, allow_fragments) urlparse(url, bscheme, allow_fragments)
if scheme != bscheme or scheme not in uses_relative: if scheme != bscheme or scheme not in uses_relative:
return urlunparse((scheme, netloc, path, return url
params, query, fragment))
if scheme in uses_netloc: if scheme in uses_netloc:
if netloc: if netloc:
return urlunparse((scheme, netloc, path, return urlunparse((scheme, netloc, path,
@ -147,8 +148,12 @@ def urljoin(base, url, allow_fragments = 1):
return urlunparse((scheme, netloc, path, return urlunparse((scheme, netloc, path,
params, query, fragment)) params, query, fragment))
if not path: if not path:
if not params:
params = bparams
if not query:
query = bquery
return urlunparse((scheme, netloc, bpath, return urlunparse((scheme, netloc, bpath,
params, query or bquery, fragment)) params, query, fragment))
segments = bpath.split('/')[:-1] + path.split('/') segments = bpath.split('/')[:-1] + path.split('/')
# XXX The stuff below is bogus in various ways... # XXX The stuff below is bogus in various ways...
if segments[-1] == '.': if segments[-1] == '.':
@ -159,13 +164,14 @@ def urljoin(base, url, allow_fragments = 1):
i = 1 i = 1
n = len(segments) - 1 n = len(segments) - 1
while i < n: while i < n:
if segments[i] == '..' and segments[i-1]: if (segments[i] == '..'
and segments[i-1] not in ('', '..')):
del segments[i-1:i+1] del segments[i-1:i+1]
break break
i = i+1 i = i+1
else: else:
break break
if len(segments) == 2 and segments[1] == '..' and segments[0] == '': if segments == ['', '..']:
segments[-1] = '' segments[-1] = ''
elif len(segments) >= 2 and segments[-1] == '..': elif len(segments) >= 2 and segments[-1] == '..':
segments[-2:] = [''] segments[-2:] = ['']