def __init__(self, host, port=None, key_file=None, cert_file=None, cacert=None, timeout=None, insecure=False, ssl_compression=True): # List of exceptions reported by Python3 instead of # SSLConfigurationError if six.PY3: excp_lst = (TypeError, IOError, ssl.SSLError) # https.py:250:36: F821 undefined name 'FileNotFoundError' else: # NOTE(jamespage) # Accomodate changes in behaviour for pep-0467, introduced # in python 2.7.9. # https://github.com/python/peps/blob/master/pep-0476.txt excp_lst = (TypeError, IOError, ssl.SSLError) try: HTTPSConnection.__init__(self, host, port, key_file=key_file, cert_file=cert_file) self.key_file = key_file self.cert_file = cert_file self.timeout = timeout self.insecure = insecure # NOTE(flaper87): `is_verified` is needed for # requests' urllib3. If insecure is True then # the request is not `verified`, hence `not insecure` self.is_verified = not insecure self.ssl_compression = ssl_compression self.cacert = None if cacert is None else str(cacert) self.set_context() # ssl exceptions are reported in various form in Python 3 # so to be compatible, we report the same kind as under # Python2 except excp_lst as e: raise exc.SSLConfigurationError(str(e))
def http_connect(ipaddr, port, device, partition, method, path, headers=None, query_string=None, ssl=False): """ Helper function to create a HTTPConnection object that is buffered for backend Swift services. :param ipaddr: IPv4 address to connect to :param port: port to connect to :param device: device of the node to query :param partition: partition on the device :param method: HTTP method to request ('GET', 'PUT', 'POST', etc.) :param path: request path :param headers: dictionary of headers :param query_string: request query string :returns: HTTPConnection object """ if not port: port = 443 if ssl else 80 if ssl: conn = HTTPSConnection('%s:%s' % (ipaddr, port)) else: conn = BufferedHTTPConnection('%s:%s' % (ipaddr, port)) path = quote('/' + device + '/' + str(partition) + path) if query_string: path += '?' + query_string conn.path = path conn.putrequest(method, path) if headers: for header, value in headers.iteritems(): conn.putheader(header, value) conn.endheaders() return conn
def http_connect_raw(ipaddr, port, method, path, headers=None, query_string=None, ssl=False): """ Helper function to create a HTTPConnection object that is buffered. :param ipaddr: IPv4 address to connect to :param port: port to connect to :param method: HTTP method to request ('GET', 'PUT', 'POST', etc.) :param path: request path :param headers: dictionary of headers :param query_string: request query string :returns: HTTPConnection object """ if not port: port = 443 if ssl else 80 if ssl: conn = HTTPSConnection('%s:%s' % (ipaddr, port)) else: conn = BufferedHTTPConnection('%s:%s' % (ipaddr, port)) if query_string: path += '?' + query_string conn.path = path conn.putrequest(method, path) if headers: for header, value in headers.iteritems(): conn.putheader(header, value) conn.endheaders() return conn
def __init__(self, host, port=None, key_file=None, cert_file=None, cacert=None, timeout=None, insecure=False, ssl_compression=True): # List of exceptions reported by Python3 instead of # SSLConfigurationError if six.PY3: excp_lst = (TypeError, FileNotFoundError, ssl.SSLError) else: # NOTE(jamespage) # Accommodate changes in behaviour for pep-0467, introduced # in python 2.7.9. # https://github.com/python/peps/blob/master/pep-0476.txt excp_lst = (TypeError, IOError, ssl.SSLError) try: HTTPSConnection.__init__(self, host, port, key_file=key_file, cert_file=cert_file) self.key_file = key_file self.cert_file = cert_file self.timeout = timeout self.insecure = insecure # NOTE(flaper87): `is_verified` is needed for # requests' urllib3. If insecure is True then # the request is not `verified`, hence `not insecure` self.is_verified = not insecure self.ssl_compression = ssl_compression self.cacert = None if cacert is None else str(cacert) self.set_context() # ssl exceptions are reported in various form in Python 3 # so to be compatible, we report the same kind as under # Python2 except excp_lst as e: raise exc.SSLConfigurationError(str(e))
def __init__(self, host, port=None, key_file=None, cert_file=None, cacert=None, timeout=None, insecure=False, ssl_compression=True): # List of exceptions reported by Python3 instead of # SSLConfigurationError if six.PY3: excp_lst = (TypeError, FileNotFoundError, ssl.SSLError) else: excp_lst = () try: HTTPSConnection.__init__(self, host, port, key_file=key_file, cert_file=cert_file) self.key_file = key_file self.cert_file = cert_file self.timeout = timeout self.insecure = insecure self.ssl_compression = ssl_compression self.cacert = None if cacert is None else str(cacert) self.set_context() # ssl exceptions are reported in various form in Python 3 # so to be compatible, we report the same kind as under # Python2 except excp_lst as e: raise exc.SSLConfigurationError(str(e))
def http_connect(ipaddr, port, device, partition, method, path, headers=None, query_string=None, ssl=False): """ Helper function to create an HTTPConnection object. If ssl is set True, HTTPSConnection will be used. However, if ssl=False, BufferedHTTPConnection will be used, which is buffered for backend Swift services. :param ipaddr: IPv4 address to connect to :param port: port to connect to :param device: device of the node to query :param partition: partition on the device :param method: HTTP method to request ('GET', 'PUT', 'POST', etc.) :param path: request path :param headers: dictionary of headers :param query_string: request query string :param ssl: set True if SSL should be used (default: False) :returns: HTTPConnection object """ if not port: port = 443 if ssl else 80 if ssl: conn = HTTPSConnection('%s:%s' % (ipaddr, port)) else: conn = BufferedHTTPConnection('%s:%s' % (ipaddr, port)) path = quote('/' + device + '/' + str(partition) + path) if query_string: path += '?' + query_string conn.path = path conn.putrequest(method, path, skip_host=(headers and 'Host' in headers)) if headers: for header, value in headers.iteritems(): conn.putheader(header, str(value)) conn.endheaders() return conn
def __init__( self, host, port=None, key_file=None, cert_file=None, cacert=None, timeout=None, insecure=False, ssl_compression=True, ): # List of exceptions reported by Python3 instead of # SSLConfigurationError if six.PY3: excp_lst = (TypeError, FileNotFoundError, ssl.SSLError) else: excp_lst = () try: HTTPSConnection.__init__(self, host, port, key_file=key_file, cert_file=cert_file) self.key_file = key_file self.cert_file = cert_file self.timeout = timeout self.insecure = insecure self.ssl_compression = ssl_compression self.cacert = cacert self.setcontext() # ssl exceptions are reported in various form in Python 3 # so to be compatible, we report the same kind as under # Python2 except excp_lst as e: raise exc.SSLConfigurationError(str(e))
def __init__( self, host, port=None, key_file=None, cert_file=None, cacert=None, timeout=None, insecure=False, ssl_compression=True, ): # List of exceptions reported by Python3 instead of # SSLConfigurationError if six.PY3: excp_lst = (TypeError, FileNotFoundError, ssl.SSLError) else: excp_lst = () try: HTTPSConnection.__init__(self, host, port, key_file=key_file, cert_file=cert_file) self.key_file = key_file self.cert_file = cert_file self.timeout = timeout self.insecure = insecure # NOTE(flaper87): `is_verified` is needed for # requests' urllib3. If insecure is True then # the request is not `verified`, hence `not insecure` self.is_verified = not insecure self.ssl_compression = ssl_compression self.cacert = None if cacert is None else str(cacert) self.set_context() # ssl exceptions are reported in various form in Python 3 # so to be compatible, we report the same kind as under # Python2 except excp_lst as e: raise exc.SSLConfigurationError(str(e))
def __init__(self, host, port=None, key_file=None, cert_file=None, cacert=None, timeout=None, insecure=False, ssl_compression=True): # List of exceptions reported by Python3 instead of # SSLConfigurationError excp_lst = (TypeError, FileNotFoundError, ssl.SSLError) try: HTTPSConnection.__init__(self, host, port, key_file=key_file, cert_file=cert_file) self.key_file = key_file self.cert_file = cert_file self.timeout = timeout self.insecure = insecure # NOTE(flaper87): `is_verified` is needed for # requests' urllib3. If insecure is True then # the request is not `verified`, hence `not insecure` self.is_verified = not insecure self.ssl_compression = ssl_compression self.cacert = None if cacert is None else str(cacert) self.set_context() # ssl exceptions are reported in various form in Python 3 # so to be compatible, we report the same kind as under # Python2 except excp_lst as e: raise exc.SSLConfigurationError(str(e))
def http_connect_raw(ipaddr, port, method, path, headers=None, query_string=None, ssl=False): """ Helper function to create an HTTPConnection object. If ssl is set True, HTTPSConnection will be used. However, if ssl=False, BufferedHTTPConnection will be used, which is buffered for backend Swift services. :param ipaddr: IPv4 address to connect to :param port: port to connect to :param method: HTTP method to request ('GET', 'PUT', 'POST', etc.) :param path: request path :param headers: dictionary of headers :param query_string: request query string :param ssl: set True if SSL should be used (default: False) :returns: HTTPConnection object """ if ssl: conn = HTTPSConnection('%s:%s' % (ipaddr, port)) else: conn = BufferedHTTPConnection('%s:%s' % (ipaddr, port)) if query_string: path += '?' + query_string conn.path = path conn.putrequest(method, path) if headers: for header, value in headers.iteritems(): conn.putheader(header, value) conn.endheaders() return conn
def close(self): if self.sock: # Removing reference to socket but don't close it yet. # Response close will close both socket and associated # file. Closing socket too soon will cause response # reads to fail with socket IO error 'Bad file descriptor'. self.sock = None # Calling close on HTTPConnection to continue doing that cleanup. HTTPSConnection.close(self)
def __init__(self, host, port=None, key_file=None, cert_file=None, cacert=None, timeout=None, insecure=False, ssl_compression=True): HTTPSConnection.__init__(self, host, port, key_file=key_file, cert_file=cert_file) self.key_file = key_file self.cert_file = cert_file self.timeout = timeout self.insecure = insecure self.ssl_compression = ssl_compression self.cacert = cacert self.setcontext()
def http_connect_raw(ipaddr, port, method, path, headers=None, query_string=None, ssl=False, key_file=None, cert_file=None, timeout=None): """ Helper function to create an HTTPConnection object. If ssl is set True, HTTPSConnection will be used. However, if ssl=False, BufferedHTTPConnection will be used, which is buffered for backend Swift services. :param ipaddr: IPv4 address to connect to :param port: port to connect to :param method: HTTP method to request ('GET', 'PUT', 'POST', etc.) :param path: request path :param headers: dictionary of headers :param query_string: request query string :param ssl: set True if SSL should be used (default: False) :param key_file: Private key file (not needed if cert_file has private key) :param cert_file: Certificate file (Keystore) :returns: HTTPConnection object """ if timeout is None: timeout = DEFAULT_TIMEOUT if ssl: conn = HTTPSConnection('%s:%s' % (ipaddr, port), key_file=key_file, cert_file=cert_file, timeout=timeout) else: conn = BufferedHTTPConnection('%s:%s' % (ipaddr, port), timeout=timeout) if query_string: path += '?' + query_string conn.path = path conn.putrequest(method, path) if headers: # pylint: disable=E1103 for header, value in headers.iteritems(): conn.putheader(header, value) # pylint: disable=E1103 conn.endheaders() return conn
def http_connect_raw(ipaddr, port, method, path, headers=None, query_string=None, ssl=False): """ Helper function to create an HTTPConnection object. If ssl is set True, HTTPSConnection will be used. However, if ssl=False, BufferedHTTPConnection will be used, which is buffered for backend Swift services. :param ipaddr: IPv4 address to connect to :param port: port to connect to :param method: HTTP method to request ('GET', 'PUT', 'POST', etc.) :param path: request path :param headers: dictionary of headers :param query_string: request query string :param ssl: set True if SSL should be used (default: False) :returns: HTTPConnection object """ if not port: port = 443 if ssl else 80 if ssl: conn = HTTPSConnection('%s:%s' % (ipaddr, port)) else: conn = BufferedHTTPConnection('%s:%s' % (ipaddr, port)) if query_string: # Round trip to ensure proper quoting if six.PY2: query_string = urlencode( parse_qsl(query_string, keep_blank_values=True)) else: query_string = urlencode(parse_qsl(query_string, keep_blank_values=True, encoding='latin1'), encoding='latin1') path += '?' + query_string conn.path = path conn.putrequest(method, path, skip_host=(headers and 'Host' in headers)) if headers: for header, value in headers.items(): conn.putheader(header, str(value)) conn.endheaders() return conn
def http_connection(url): """ Make an HTTPConnection or HTTPSConnection :param url: url to connect to :returns: tuple of (parsed url, connection object) :raises ClientException: Unable to handle protocol scheme """ parsed = urlparse(url) if parsed.scheme == 'http': conn = HTTPConnection(parsed.netloc) elif parsed.scheme == 'https': conn = HTTPSConnection(parsed.netloc) else: raise ClientException('Cannot handle protocol scheme %s for url %s' % (parsed.scheme, repr(url))) return parsed, conn
def http_connection(url, proxy=None): """ Make an HTTPConnection or HTTPSConnection :param url: url to connect to :param proxy: proxy to connect through, if any; None by default; str of the format 'http://127.0.0.1:8888' to set one :returns: tuple of (parsed url, connection object) :raises ClientException: Unable to handle protocol scheme """ url = encode_utf8(url) parsed = urlparse(url) proxy_parsed = urlparse(proxy) if proxy else None if parsed.scheme == 'http': conn = HTTPConnection((proxy_parsed if proxy else parsed).netloc) elif parsed.scheme == 'https': conn = HTTPSConnection((proxy_parsed if proxy else parsed).netloc) else: raise ClientException('Cannot handle protocol scheme %s for url %s' % (parsed.scheme, repr(url))) def putheader_wrapper(func): @wraps(func) def putheader_escaped(key, value): func(encode_utf8(key), encode_utf8(value)) return putheader_escaped conn.putheader = putheader_wrapper(conn.putheader) def request_wrapper(func): @wraps(func) def request_escaped(method, url, body=None, headers=None): url = encode_utf8(url) if body: body = encode_utf8(body) func(method, url, body=body, headers=headers or {}) return request_escaped conn.request = request_wrapper(conn.request) if proxy: conn._set_tunnel(parsed.hostname, parsed.port) return parsed, conn
def http_connection(url, proxy=None): """ Make an HTTPConnection or HTTPSConnection :param url: url to connect to :param proxy: proxy to connect through, if any; None by default; str of the format 'http://127.0.0.1:8888' to set one :returns: tuple of (parsed url, connection object) :raises ClientException: Unable to handle protocol scheme """ parsed = urlparse(url) proxy_parsed = urlparse(proxy) if proxy else None if parsed.scheme == 'http': conn = HTTPConnection((proxy_parsed if proxy else parsed).netloc) elif parsed.scheme == 'https': conn = HTTPSConnection((proxy_parsed if proxy else parsed).netloc) else: raise ClientException('Cannot handle protocol scheme %s for url %s' % (parsed.scheme, repr(url))) if proxy: conn._set_tunnel(parsed.hostname, parsed.port) return parsed, conn
def send_to_peer(self, peer, sync_to_peer, key): peer = peer.lower() ssl = False if peer.startswith('https://'): ssl = True peer = peer[8:] if peer.startswith('http://'): peer = peer[7:] try: with Timeout(self.conn_timeout): if ssl: #print 'HTTPS %s ' % peer conn = HTTPSConnection(peer) else: #print 'HTTP %s ' % peer conn = HTTPConnection(peer) conn.putrequest(self.req.method, self.req.path_qs) conn.putheader('X-Orig-Cluster', self.my_cluster) conn.putheader('X-Account-Meta-Orig-Cluster', self.my_cluster) conn.putheader('X-Container-Meta-Orig-Cluster', self.my_cluster) if key: sync_to = sync_to_peer + self.env['PATH_INFO'] conn.putheader('X-Container-Sync-To', sync_to) for header, value in self.req.headers.iteritems(): if header != 'X-Container-Sync-To': conn.putheader(header, value) conn.endheaders(message_body=None) with Timeout(self.req_timeout): resp = conn.getresponse() status = resp.status return (status, resp.getheaders(), resp.read()) except (Exception, Timeout) as e: # Print error log print >> sys.stderr, peer + ': Exception, Timeout error: %s' % e print '<<<<<<<< HTTP_SERVICE_UNAVAILABLE'
def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0): self._method = method # save method for getexpect method return BaseHTTPSConnection.putrequest(self, method, url, skip_host, skip_accept_encoding)
def close(self): if self.sock: self.sock = None HTTPSConnection.close(self)