~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/http.py

  • Committer: Röbey Pointer
  • Date: 2006-01-09 07:46:33 UTC
  • mto: (1185.50.69 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1554.
  • Revision ID: robey@master-shake.local-20060109074633-85245a191c3b8ca7
change http url parsing to use urlparse, and use the ui_factory to ask for a password if necessary

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from bzrlib.errors import BzrError, BzrCheckError
28
28
from bzrlib.branch import Branch
29
29
from bzrlib.trace import mutter
 
30
from bzrlib.ui import ui_factory
30
31
 
31
32
 
32
33
def extract_auth(url, password_manager):
35
36
    password manager.  Return the url, minus those auth parameters (which
36
37
    confuse urllib2).
37
38
    """
38
 
    assert url.startswith('http://') or url.startswith('https://')
39
 
    scheme, host = url.split('//', 1)
40
 
    if '/' in host:
41
 
        host, path = host.split('/', 1)
42
 
        path = '/' + path
43
 
    else:
44
 
        path = ''
45
 
    port = ''
46
 
    if '@' in host:
47
 
        auth, host = host.split('@', 1)
 
39
    scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
 
40
    assert (scheme == 'http') or (scheme == 'https')
 
41
    
 
42
    if '@' in netloc:
 
43
        auth, netloc = netloc.split('@', 1)
48
44
        if ':' in auth:
49
45
            username, password = auth.split(':', 1)
50
46
        else:
51
47
            username, password = auth, None
52
 
        if ':' in host:
53
 
            host, port = host.split(':', 1)
54
 
            port = ':' + port
55
 
        # FIXME: if password isn't given, should we ask for it?
 
48
        if ':' in netloc:
 
49
            host = netloc.split(':', 1)[0]
 
50
        else:
 
51
            host = netloc
 
52
        username = urllib.unquote(username)
56
53
        if password is not None:
57
 
            username = urllib.unquote(username)
58
54
            password = urllib.unquote(password)
59
 
            password_manager.add_password(None, host, username, password)
60
 
    url = scheme + '//' + host + port + path
 
55
        else:
 
56
            password = ui_factory.get_password(prompt='HTTP %(user)@%(host) password',
 
57
                                               user=username, host=host)
 
58
        password_manager.add_password(None, host, username, password)
 
59
    url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
61
60
    return url
 
61
 
62
62
    
63
63
def get_url(url):
64
64
    import urllib2
65
 
    mutter("get_url %s" % url)
 
65
    mutter("get_url %s", url)
66
66
    manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
67
67
    url = extract_auth(url, manager)
68
68
    auth_handler = urllib2.HTTPBasicAuthHandler(manager)