Join a base URL and a possibly relative URL to form an absolute interpretation of the latter.
(base, url, allow_fragments=True)
| 695 | return url |
| 696 | |
| 697 | def 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 |
no test coverage detected
searching dependent graphs…