@@ -47,9 +47,14 @@ class HTTP11Connection(object):
4747 port 443.
4848 :param ssl_context: (optional) A class with custom certificate settings.
4949 If not provided then hyper's default ``SSLContext`` is used instead.
50+ :param proxy_host: (optional) The proxy to connect to. This can be an IP
51+ address or a host name and may include a port.
52+ :param proxy_port: (optional) The proxy port to connect to. If not provided
53+ and one also isn't provided in the ``proxy`` parameter,
54+ defaults to 8080.
5055 """
51- def __init__ (self , host , port = None , secure = None , ssl_context = None ,
52- ** kwargs ):
56+ def __init__ (self , host , port = None , secure = None , ssl_context = None ,
57+ proxy_host = None , proxy_port = None , ** kwargs ):
5358 if port is None :
5459 try :
5560 self .host , self .port = host .split (':' )
@@ -75,6 +80,21 @@ def __init__(self, host, port=None, secure=None, ssl_context=None,
7580 self .ssl_context = ssl_context
7681 self ._sock = None
7782
83+ # Setup proxy details if applicable.
84+ if proxy_host :
85+ if proxy_port is None :
86+ try :
87+ self .proxy_host , self .proxy_port = proxy_host .split (':' )
88+ except ValueError :
89+ self .proxy_host , self .proxy_port = proxy_host , 8080
90+ else :
91+ self .proxy_port = int (self .proxy_port )
92+ else :
93+ self .proxy_host , self .proxy_port = proxy_host , proxy_port
94+ else :
95+ self .proxy_host = None
96+ self .proxy_port = None
97+
7898 #: The size of the in-memory buffer used to store data from the
7999 #: network. This is used as a performance optimisation. Increase buffer
80100 #: size to improve performance: decrease it to conserve memory.
@@ -93,11 +113,19 @@ def connect(self):
93113 :returns: Nothing.
94114 """
95115 if self ._sock is None :
96- sock = socket .create_connection ((self .host , self .port ), 5 )
116+ if not self .proxy_host :
117+ host = self .host
118+ port = self .port
119+ else :
120+ host = self .proxy_host
121+ port = self .proxy_port
122+
123+ sock = socket .create_connection ((host , port ), 5 )
97124 proto = None
98125
99126 if self .secure :
100- sock , proto = wrap_socket (sock , self .host , self .ssl_context )
127+ assert not self .proxy_host , "Using a proxy with HTTPS not yet supported."
128+ sock , proto = wrap_socket (sock , host , self .ssl_context )
101129
102130 log .debug ("Selected protocol: %s" , proto )
103131 sock = BufferedSocket (sock , self .network_buffer_size )
@@ -176,7 +204,8 @@ def get_response(self):
176204 self ._sock .advance_buffer (response .consumed )
177205
178206 if (response .status == 101 and
179- b'upgrade' in headers ['connection' ] and H2C_PROTOCOL .encode ('utf-8' ) in headers ['upgrade' ]):
207+ b'upgrade' in headers ['connection' ] and
208+ H2C_PROTOCOL .encode ('utf-8' ) in headers ['upgrade' ]):
180209 raise HTTPUpgrade (H2C_PROTOCOL , self ._sock )
181210
182211 return HTTP11Response (
0 commit comments