def _connect(self): """Connect to the host and port specified in __init__.""" if self.sock: return if self._proxy_host is not None: logger.info('Connecting to http proxy %s:%s', self._proxy_host, self._proxy_port) sock = socketutil.create_connection( (self._proxy_host, self._proxy_port)) if self.ssl: # TODO proxy header support data = self._buildheaders('CONNECT', '%s:%d' % (self.host, self.port), {}, HTTP_VER_1_0) sock.send(data) sock.setblocking(0) r = self.response_class(sock, self.timeout, 'CONNECT') timeout_exc = HTTPTimeoutException( 'Timed out waiting for CONNECT response from proxy') while not r.complete(): try: # We're a friend of the response class, so let # us use the private attribute. # pylint: disable=W0212 if not r._select(): if not r.complete(): raise timeout_exc except HTTPTimeoutException: # This raise/except pattern looks goofy, but # _select can raise the timeout as well as the # loop body. I wish it wasn't this convoluted, # but I don't have a better solution # immediately handy. raise timeout_exc if r.status != 200: raise HTTPProxyConnectFailedException( 'Proxy connection failed: %d %s' % (r.status, r.read())) logger.info('CONNECT (for SSL) to %s:%s via proxy succeeded.', self.host, self.port) else: sock = socketutil.create_connection((self.host, self.port)) if self.ssl: # This is the default, but in the case of proxied SSL # requests the proxy logic above will have cleared # blocking mode, so re-enable it just to be safe. sock.setblocking(1) logger.debug('wrapping socket for ssl with options %r', self.ssl_opts) sock = socketutil.wrap_socket(sock, **self.ssl_opts) if self._ssl_validator: self._ssl_validator(sock) sock.setblocking(0) self.sock = sock
def _connect(self): """Connect to the host and port specified in __init__.""" if self.sock: return if self._proxy_host is not None: logger.info('Connecting to http proxy %s:%s', self._proxy_host, self._proxy_port) sock = socketutil.create_connection((self._proxy_host, self._proxy_port)) if self.ssl: # TODO proxy header support data = self._buildheaders('CONNECT', '%s:%d' % (self.host, self.port), {}, HTTP_VER_1_0) sock.send(data) sock.setblocking(0) r = self.response_class(sock, self.timeout, 'CONNECT') timeout_exc = HTTPTimeoutException( 'Timed out waiting for CONNECT response from proxy') while not r.complete(): try: # We're a friend of the response class, so let # us use the private attribute. # pylint: disable=W0212 if not r._select(): if not r.complete(): raise timeout_exc except HTTPTimeoutException: # This raise/except pattern looks goofy, but # _select can raise the timeout as well as the # loop body. I wish it wasn't this convoluted, # but I don't have a better solution # immediately handy. raise timeout_exc if r.status != 200: raise HTTPProxyConnectFailedException( 'Proxy connection failed: %d %s' % (r.status, r.read())) logger.info('CONNECT (for SSL) to %s:%s via proxy succeeded.', self.host, self.port) else: sock = socketutil.create_connection((self.host, self.port)) if self.ssl: # This is the default, but in the case of proxied SSL # requests the proxy logic above will have cleared # blocking mode, so re-enable it just to be safe. sock.setblocking(1) logger.debug('wrapping socket for ssl with options %r', self.ssl_opts) sock = socketutil.wrap_socket(sock, **self.ssl_opts) if self._ssl_validator: self._ssl_validator(sock) sock.setblocking(0) self.sock = sock
def _connect(self): """Connect to the host and port specified in __init__.""" if self.sock: return if self._proxy_host is not None: logger.info('Connecting to http proxy %s:%s', self._proxy_host, self._proxy_port) sock = socketutil.create_connection((self._proxy_host, self._proxy_port)) if self.ssl: # TODO proxy header support data = self.buildheaders('CONNECT', '%s:%d' % (self.host, self.port), {}, HTTP_VER_1_0) sock.send(data) sock.setblocking(0) r = self.response_class(sock, self.timeout) timeout_exc = HTTPTimeoutException( 'Timed out waiting for CONNECT response from proxy') while not r.complete(): try: if not r._select(): raise timeout_exc except HTTPTimeoutException: # This raise/except pattern looks goofy, but # _select can raise the timeout as well as the # loop body. I wish it wasn't this convoluted, # but I don't have a better solution # immediately handy. raise timeout_exc if r.status != 200: raise HTTPProxyConnectFailedException( 'Proxy connection failed: %d %s' % (r.status, r.read())) logger.info('CONNECT (for SSL) to %s:%s via proxy succeeded.', self.host, self.port) else: sock = socketutil.create_connection((self.host, self.port)) if self.ssl: logger.debug('wrapping socket for ssl with options %r', self.ssl_opts) sock = socketutil.wrap_socket(sock, **self.ssl_opts) if self._ssl_validator: self._ssl_validator(sock) sock.setblocking(0) self.sock = sock