/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/urlutils.py

  • Committer: Vincent Ladeuil
  • Date: 2011-08-16 13:12:40 UTC
  • mfrom: (6071 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6076.
  • Revision ID: v.ladeuil+lp@free.fr-20110816131240-gcyn9cik86dxwgz3
Merge into trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
730
730
    return osutils.pathjoin(*segments)
731
731
 
732
732
 
 
733
class URL(object):
 
734
    """Parsed URL."""
 
735
 
 
736
    def __init__(self, scheme, quoted_user, quoted_password, quoted_host,
 
737
            port, quoted_path):
 
738
        self.scheme = scheme
 
739
        self.quoted_host = quoted_host
 
740
        self.host = urllib.unquote(self.quoted_host)
 
741
        self.quoted_user = quoted_user
 
742
        if self.quoted_user is not None:
 
743
            self.user = urllib.unquote(self.quoted_user)
 
744
        else:
 
745
            self.user = None
 
746
        self.quoted_password = quoted_password
 
747
        if self.quoted_password is not None:
 
748
            self.password = urllib.unquote(self.quoted_password)
 
749
        else:
 
750
            self.password = None
 
751
        self.port = port
 
752
        self.quoted_path = quoted_path
 
753
        self.path = urllib.unquote(self.quoted_path)
 
754
 
 
755
    def __eq__(self, other):
 
756
        return (isinstance(other, self.__class__) and
 
757
                self.scheme == other.scheme and
 
758
                self.host == other.host and
 
759
                self.user == other.user and
 
760
                self.password == other.password and
 
761
                self.path == other.path)
 
762
 
 
763
    def __repr__(self):
 
764
        return "<%s(%r, %r, %r, %r, %r, %r)>" % (
 
765
            self.__class__.__name__,
 
766
            self.scheme, self.quoted_user, self.quoted_password,
 
767
            self.quoted_host, self.port, self.quoted_path)
 
768
 
 
769
    @classmethod
 
770
    def from_string(cls, url):
 
771
        """Create a URL object from a string.
 
772
 
 
773
        :param url: URL as bytestring
 
774
        """
 
775
        if isinstance(url, unicode):
 
776
            raise errors.InvalidURL('should be ascii:\n%r' % url)
 
777
        url = url.encode('utf-8')
 
778
        (scheme, netloc, path, params,
 
779
         query, fragment) = urlparse.urlparse(url, allow_fragments=False)
 
780
        user = password = host = port = None
 
781
        if '@' in netloc:
 
782
            user, host = netloc.rsplit('@', 1)
 
783
            if ':' in user:
 
784
                user, password = user.split(':', 1)
 
785
        else:
 
786
            host = netloc
 
787
 
 
788
        if ':' in host and not (host[0] == '[' and host[-1] == ']'):
 
789
            # there *is* port
 
790
            host, port = host.rsplit(':',1)
 
791
            try:
 
792
                port = int(port)
 
793
            except ValueError:
 
794
                raise errors.InvalidURL('invalid port number %s in url:\n%s' %
 
795
                                        (port, url))
 
796
        if host != "" and host[0] == '[' and host[-1] == ']': #IPv6
 
797
            host = host[1:-1]
 
798
 
 
799
        return cls(scheme, user, password, host, port, path)
 
800
 
 
801
    def __str__(self):
 
802
        netloc = self.quoted_host
 
803
        if ":" in netloc:
 
804
            netloc = "[%s]" % netloc
 
805
        if self.quoted_user is not None:
 
806
            # Note that we don't put the password back even if we
 
807
            # have one so that it doesn't get accidentally
 
808
            # exposed.
 
809
            netloc = '%s@%s' % (self.quoted_user, netloc)
 
810
        if self.port is not None:
 
811
            netloc = '%s:%d' % (netloc, self.port)
 
812
        return urlparse.urlunparse(
 
813
            (self.scheme, netloc, self.quoted_path, None, None, None))
 
814
 
 
815
    @staticmethod
 
816
    def _combine_paths(base_path, relpath):
 
817
        """Transform a Transport-relative path to a remote absolute path.
 
818
 
 
819
        This does not handle substitution of ~ but does handle '..' and '.'
 
820
        components.
 
821
 
 
822
        Examples::
 
823
 
 
824
            t._combine_paths('/home/sarah', 'project/foo')
 
825
                => '/home/sarah/project/foo'
 
826
            t._combine_paths('/home/sarah', '../../etc')
 
827
                => '/etc'
 
828
            t._combine_paths('/home/sarah', '/etc')
 
829
                => '/etc'
 
830
 
 
831
        :param base_path: base path
 
832
        :param relpath: relative url string for relative part of remote path.
 
833
        :return: urlencoded string for final path.
 
834
        """
 
835
        if not isinstance(relpath, str):
 
836
            raise errors.InvalidURL(relpath)
 
837
        if relpath.startswith('/'):
 
838
            base_parts = []
 
839
        else:
 
840
            base_parts = base_path.split('/')
 
841
        if len(base_parts) > 0 and base_parts[-1] == '':
 
842
            base_parts = base_parts[:-1]
 
843
        for p in relpath.split('/'):
 
844
            if p == '..':
 
845
                if len(base_parts) == 0:
 
846
                    # In most filesystems, a request for the parent
 
847
                    # of root, just returns root.
 
848
                    continue
 
849
                base_parts.pop()
 
850
            elif p == '.':
 
851
                continue # No-op
 
852
            elif p != '':
 
853
                base_parts.append(p)
 
854
        path = '/'.join(base_parts)
 
855
        if not path.startswith('/'):
 
856
            path = '/' + path
 
857
        return path
 
858
 
 
859
    def clone(self, offset=None):
 
860
        """Return a new URL for a path relative to this URL.
 
861
 
 
862
        :param offset: A relative path, already urlencoded
 
863
        :return: `URL` instance
 
864
        """
 
865
        if offset is not None:
 
866
            relative = unescape(offset).encode('utf-8')
 
867
            path = self._combine_paths(self.path, relative)
 
868
            path = urllib.quote(path)
 
869
        else:
 
870
            path = self.quoted_path
 
871
        return self.__class__(self.scheme, self.quoted_user,
 
872
                self.quoted_password, self.quoted_host, self.port,
 
873
                path)
 
874
 
733
875
 
734
876
def parse_url(url):
735
877
    """Extract the server address, the credentials and the path from the url.
738
880
    chars.
739
881
 
740
882
    :param url: an quoted url
741
 
 
742
883
    :return: (scheme, user, password, host, port, path) tuple, all fields
743
884
        are unquoted.
744
885
    """
745
 
    if isinstance(url, unicode):
746
 
        raise errors.InvalidURL('should be ascii:\n%r' % url)
747
 
    url = url.encode('utf-8')
748
 
    (scheme, netloc, path, params,
749
 
     query, fragment) = urlparse.urlparse(url, allow_fragments=False)
750
 
    user = password = host = port = None
751
 
    if '@' in netloc:
752
 
        user, host = netloc.rsplit('@', 1)
753
 
        if ':' in user:
754
 
            user, password = user.split(':', 1)
755
 
            password = urllib.unquote(password)
756
 
        user = urllib.unquote(user)
757
 
    else:
758
 
        host = netloc
759
 
 
760
 
    if ':' in host and not (host[0] == '[' and host[-1] == ']'): #there *is* port
761
 
        host, port = host.rsplit(':',1)
762
 
        try:
763
 
            port = int(port)
764
 
        except ValueError:
765
 
            raise errors.InvalidURL('invalid port number %s in url:\n%s' %
766
 
                                    (port, url))
767
 
    if host != "" and host[0] == '[' and host[-1] == ']': #IPv6
768
 
        host = host[1:-1]
769
 
 
770
 
    host = urllib.unquote(host)
771
 
    path = urllib.unquote(path)
772
 
 
773
 
    return (scheme, user, password, host, port, path)
 
886
    parsed_url = URL.from_string(url)
 
887
    return (parsed_url.scheme, parsed_url.user, parsed_url.password,
 
888
        parsed_url.host, parsed_url.port, parsed_url.path)