MCPcopy Index your code
hub / github.com/python/cpython / urljoin

Function urljoin

Lib/urllib/parse.py:697–766  ·  view source on GitHub ↗

Join a base URL and a possibly relative URL to form an absolute interpretation of the latter.

(base, url, allow_fragments=True)

Source from the content-addressed store, hash-verified

695 return url
696
697def urljoin(base, url, allow_fragments=True):
698 """Join a base URL and a possibly relative URL to form an absolute
699 interpretation of the latter."""
700 if not base:
701 return url
702 if not url:
703 return base
704
705 base, url, _coerce_result = _coerce_args(base, url)
706 bscheme, bnetloc, bpath, bquery, bfragment = \
707 _urlsplit(base, None, allow_fragments)
708 scheme, netloc, path, query, fragment = \
709 _urlsplit(url, None, allow_fragments)
710
711 if scheme is None:
712 scheme = bscheme
713 if scheme != bscheme or (scheme and scheme not in uses_relative):
714 return _coerce_result(url)
715 if not scheme or scheme in uses_netloc:
716 if netloc:
717 return _coerce_result(_urlunsplit(scheme, netloc, path,
718 query, fragment))
719 netloc = bnetloc
720
721 if not path:
722 path = bpath
723 if query is None:
724 query = bquery
725 if fragment is None:
726 fragment = bfragment
727 return _coerce_result(_urlunsplit(scheme, netloc, path,
728 query, fragment))
729
730 base_parts = bpath.split('/')
731 if base_parts[-1] != '':
732 # the last item is not a directory, so will not be taken into account
733 # in resolving the relative path
734 del base_parts[-1]
735
736 # for rfc3986, ignore all base path should the first character be root.
737 if path[:1] == '/':
738 segments = path.split('/')
739 else:
740 segments = base_parts + path.split('/')
741 # filter out elements that would cause redundant slashes on re-joining
742 # the resolved_path
743 segments[1:-1] = filter(None, segments[1:-1])
744
745 resolved_path = []
746
747 for seg in segments:
748 if seg == '..':
749 try:
750 resolved_path.pop()
751 except IndexError:
752 # ignore any .. segments that would otherwise cause an IndexError
753 # when popped from resolved_path if resolving for rfc3986
754 pass

Callers 2

_includeFunction · 0.90
http_error_302Method · 0.90

Calls 8

_coerce_argsFunction · 0.85
_urlsplitFunction · 0.85
_urlunsplitFunction · 0.85
filterFunction · 0.50
splitMethod · 0.45
popMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…