| OLD | NEW |
| (Empty) | |
| 1 """HTTP Authentication and Proxy support. |
| 2 |
| 3 |
| 4 Copyright 2006 John J. Lee <jjl@pobox.com> |
| 5 |
| 6 This code is free software; you can redistribute it and/or modify it under |
| 7 the terms of the BSD or ZPL 2.1 licenses (see the file COPYING.txt |
| 8 included with the distribution). |
| 9 |
| 10 """ |
| 11 |
| 12 from _urllib2_fork import HTTPPasswordMgr |
| 13 |
| 14 |
| 15 # TODO: stop deriving from HTTPPasswordMgr |
| 16 class HTTPProxyPasswordMgr(HTTPPasswordMgr): |
| 17 # has default realm and host/port |
| 18 def add_password(self, realm, uri, user, passwd): |
| 19 # uri could be a single URI or a sequence |
| 20 if uri is None or isinstance(uri, basestring): |
| 21 uris = [uri] |
| 22 else: |
| 23 uris = uri |
| 24 passwd_by_domain = self.passwd.setdefault(realm, {}) |
| 25 for uri in uris: |
| 26 for default_port in True, False: |
| 27 reduced_uri = self.reduce_uri(uri, default_port) |
| 28 passwd_by_domain[reduced_uri] = (user, passwd) |
| 29 |
| 30 def find_user_password(self, realm, authuri): |
| 31 attempts = [(realm, authuri), (None, authuri)] |
| 32 # bleh, want default realm to take precedence over default |
| 33 # URI/authority, hence this outer loop |
| 34 for default_uri in False, True: |
| 35 for realm, authuri in attempts: |
| 36 authinfo_by_domain = self.passwd.get(realm, {}) |
| 37 for default_port in True, False: |
| 38 reduced_authuri = self.reduce_uri(authuri, default_port) |
| 39 for uri, authinfo in authinfo_by_domain.iteritems(): |
| 40 if uri is None and not default_uri: |
| 41 continue |
| 42 if self.is_suburi(uri, reduced_authuri): |
| 43 return authinfo |
| 44 user, password = None, None |
| 45 |
| 46 if user is not None: |
| 47 break |
| 48 return user, password |
| 49 |
| 50 def reduce_uri(self, uri, default_port=True): |
| 51 if uri is None: |
| 52 return None |
| 53 return HTTPPasswordMgr.reduce_uri(self, uri, default_port) |
| 54 |
| 55 def is_suburi(self, base, test): |
| 56 if base is None: |
| 57 # default to the proxy's host/port |
| 58 hostport, path = test |
| 59 base = (hostport, "/") |
| 60 return HTTPPasswordMgr.is_suburi(self, base, test) |
| 61 |
| 62 |
| 63 class HTTPSClientCertMgr(HTTPPasswordMgr): |
| 64 # implementation inheritance: this is not a proper subclass |
| 65 def add_key_cert(self, uri, key_file, cert_file): |
| 66 self.add_password(None, uri, key_file, cert_file) |
| 67 def find_key_cert(self, authuri): |
| 68 return HTTPPasswordMgr.find_user_password(self, None, authuri) |
| OLD | NEW |