<s> import logging <EOL> import ssl <EOL> import re <EOL> from polaris_health import ProtocolError <EOL> from . import tcp <EOL> __all__ = [ '<STR_LIT>' ] <EOL> LOG = logging . getLogger ( __name__ ) <EOL> LOG . addHandler ( logging . NullHandler ( ) ) <EOL> UNVERIFIED_SSL_CONTEXT = ssl . _create_unverified_context ( ) <EOL> class HTTPResponse : <EOL> """<STR_LIT>""" <EOL> _STATUS_LINE_RE = re . compile ( <EOL> r'<STR_LIT>' , flags = re . I ) <EOL> def __init__ ( self , raw ) : <EOL> """<STR_LIT>""" <EOL> self . raw = raw <EOL> self . _parse_status ( ) <EOL> def _parse_status ( self ) : <EOL> """<STR_LIT>""" <EOL> m = self . _STATUS_LINE_RE . search ( self . raw [ : <NUM_LIT> ] ) <EOL> if not m : <EOL> raise ProtocolError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( self . raw [ : <NUM_LIT> ] ) ) <EOL> self . status_code = int ( m . group ( <NUM_LIT:1> ) ) <EOL> self . status_reason = m . group ( <NUM_LIT:2> ) <EOL> class HTTPRequest : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ip , use_ssl = False , hostname = None , url_path = '<STR_LIT:/>' , <EOL> port = None , timeout = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> self . use_ssl = use_ssl <EOL> self . ip = ip <EOL> self . url_path = url_path <EOL> if not self . url_path . startswith ( '<STR_LIT:/>' ) : <EOL> self . url_path = '<STR_LIT>' . format ( self . url_path ) <EOL> self . hostname = hostname <EOL> self . port = port <EOL> if self . port is None : <EOL> if self . use_ssl : <EOL> self . port = <NUM_LIT> <EOL> else : <EOL> self . port = <NUM_LIT> <EOL> self . timeout = timeout <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _make ( method = '<STR_LIT:GET>' ) <EOL> def _make ( self , method = '<STR_LIT:GET>' ) : <EOL> """<STR_LIT>""" <EOL> host_header = '<STR_LIT>' <EOL> if self . hostname : <EOL> host_header = '<STR_LIT>' . format ( self . hostname ) <EOL> req_str = ( '<STR_LIT>' . <EOL> format ( method , self . url_path , host_header ) ) <EOL> tcp_sock = tcp . TCPSocket ( ip = self . ip , port = self . port , <EOL> timeout = self . timeout ) <EOL> if self . use_ssl : <EOL> tcp_sock . _sock = UNVERIFIED_SSL_CONTEXT . wrap_socket ( <EOL> tcp_sock . _sock , server_hostname = self . hostname ) <EOL> tcp_sock . connect ( ) <EOL> tcp_sock . sendall ( req_str . encode ( ) ) <EOL> response_string = '<STR_LIT>' <EOL> while True : <EOL> try : <EOL> recv_bytes = tcp_sock . recv ( ) <EOL> except ProtocolError as e : <EOL> log_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( error = e , <EOL> response_string = response_string [ : <NUM_LIT> ] ) ) <EOL> raise ProtocolError ( log_msg ) <EOL> if recv_bytes == b'<STR_LIT>' : <EOL> raise ProtocolError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( response_string [ : <NUM_LIT> ] ) ) <EOL> else : <EOL> response_string += recv_bytes . decode ( errors = '<STR_LIT:ignore>' ) <EOL> if HTTPResponse . _STATUS_LINE_RE . search ( response_string [ : <NUM_LIT> ] ) : <EOL> tcp_sock . close ( ) <EOL> return HTTPResponse ( response_string ) </s>
