378
378
port = conn_class.default_port
379
379
self.proxied_host = '%s:%s' % (host, port)
380
380
urllib2.Request.set_proxy(self, proxy, type)
381
# When urllib2 makes a https request with our wrapper code and a proxy,
382
# it sets Host to the https proxy, not the host we want to talk to.
383
# I'm fairly sure this is our fault, but what is the cause is an open
384
# question. -- Robert Collins May 8 2010.
385
self.add_unredirected_header('Host', self.proxied_host)
383
388
class _ConnectRequest(Request):
711
716
connect = _ConnectRequest(request)
712
717
response = self.parent.open(connect)
713
718
if response.code != 200:
714
raise ConnectionError("Can't connect to %s via proxy %s" % (
719
raise errors.ConnectionError("Can't connect to %s via proxy %s" % (
715
720
connect.proxied_host, self.host))
717
722
connection.cleanup_pipe()
868
873
print 'Will unbind %s_open for %r' % (type, proxy)
869
874
delattr(self, '%s_open' % type)
876
def bind_scheme_request(proxy, scheme):
879
scheme_request = scheme + '_request'
880
if self._debuglevel >= 3:
881
print 'Will bind %s for %r' % (scheme_request, proxy)
882
setattr(self, scheme_request,
883
lambda request: self.set_proxy(request, scheme))
871
884
# We are interested only by the http[s] proxies
872
885
http_proxy = self.get_proxy_env_var('http')
886
bind_scheme_request(http_proxy, 'http')
873
887
https_proxy = self.get_proxy_env_var('https')
875
if http_proxy is not None:
876
if self._debuglevel >= 3:
877
print 'Will bind http_request for %r' % http_proxy
878
setattr(self, 'http_request',
879
lambda request: self.set_proxy(request, 'http'))
881
if https_proxy is not None:
882
if self._debuglevel >= 3:
883
print 'Will bind http_request for %r' % https_proxy
884
setattr(self, 'https_request',
885
lambda request: self.set_proxy(request, 'https'))
888
bind_scheme_request(https_proxy, 'https')
887
890
def get_proxy_env_var(self, name, default_to='all'):
888
891
"""Get a proxy env var.