~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/urlutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-09 17:04:46 UTC
  • mfrom: (6055.1.3 822571-bzr-home-unicode)
  • Revision ID: pqm@pqm.ubuntu.com-20110809170446-f1wc1a8fhgnxi4cn
(vila) Decode BZR_HOME with fs encoding to allow unicode homes. (Vincent
 Ladeuil)

Show diffs side-by-side

added added

removed removed

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