Skip to content

Commit

Permalink
Fix python 2.4 compat issues and fixed python 3.0 tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcgregorio committed Jan 5, 2012
1 parent 9804c42 commit ac69af8
Show file tree
Hide file tree
Showing 3 changed files with 822 additions and 13 deletions.
51 changes: 38 additions & 13 deletions python2/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,8 @@ class KeyCerts(Credentials):
name/password are mapped to key/cert."""
pass

class AllHosts(object): pass
class AllHosts(object):
pass

class ProxyInfo(object):
"""Collect information required to use a proxy."""
Expand Down Expand Up @@ -762,17 +763,22 @@ def from_environment(cls, method='http'):
"""
Read proxy info from the environment variables.
"""
if method not in ['http', 'https']: return
if method not in ['http', 'https']:
return

env_var = method+'_proxy'
env_var = method + '_proxy'
url = os.environ.get(env_var, os.environ.get(env_var.upper()))
if not url: return
if not url:
return
pi = cls.from_url(url, method)

no_proxy = os.environ.get('no_proxy', os.environ.get('NO_PROXY', ''))
bypass_hosts = no_proxy.split(',') if no_proxy else []
bypass_hosts = []
if no_proxy:
bypass_hosts = no_proxy.split(',')
# special case, no_proxy=* means all hosts bypassed
if no_proxy == '*': bypass_hosts = AllHosts
if no_proxy == '*':
bypass_hosts = AllHosts

pi.bypass_hosts = bypass_hosts
return pi
Expand All @@ -783,13 +789,27 @@ def from_url(cls, url, method='http'):
Construct a ProxyInfo from a URL (such as http_proxy env var)
"""
url = urlparse.urlparse(url)
ident, sep, host_port = url.netloc.rpartition('@')
username, sep, password = ident.partition(':')
host, sep, port = host_port.partition(':')
username = None
password = None
port = None
if '@' in url[1]:
ident, host_port = url[1].split('@', 1)
if ':' in ident:
username, password = ident.split(':', 1)
else:
password = ident
else:
host_port = url[1]
if ':' in host_port:
host, port = host_port.split(':', 1)
else:
host = host_port

if port:
port = int(port)
else:
port = dict(https=443, http=80)[method]

proxy_type = 3 # socks.PROXY_TYPE_HTTP
return cls(
proxy_type = proxy_type,
Expand All @@ -804,10 +824,15 @@ def applies_to(self, hostname):

def bypass_host(self, hostname):
"""Has this host been excluded from the proxy config"""
return self.bypass_hosts is AllHosts or any(
hostname.endswith(domain)
for domain in self.bypass_hosts
)
if self.bypass_hosts is AllHosts:
return True

bypass = False
for domain in self.bypass_hosts:
if hostname.endswith(domain):
bypass = True

return bypass


class HTTPConnectionWithTimeout(httplib.HTTPConnection):
Expand Down
Loading

0 comments on commit ac69af8

Please sign in to comment.