<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> import base64 <EOL> import httplib <EOL> import logging <EOL> import re <EOL> import socket <EOL> import urllib2 <EOL> from urllib import splittype <EOL> from urllib import splituser <EOL> from urllib import splitpasswd <EOL> class InvalidCertificateException ( httplib . HTTPException ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , cert , reason ) : <EOL> """<STR_LIT>""" <EOL> httplib . HTTPException . __init__ ( self ) <EOL> self . host = host <EOL> self . cert = cert <EOL> self . reason = reason <EOL> def __str__ ( self ) : <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( self . host , self . reason , self . cert ) ) <EOL> try : <EOL> import ssl <EOL> _CAN_VALIDATE_CERTS = True <EOL> except ImportError : <EOL> _CAN_VALIDATE_CERTS = False <EOL> def can_validate_certs ( ) : <EOL> """<STR_LIT>""" <EOL> return _CAN_VALIDATE_CERTS <EOL> if can_validate_certs ( ) : <EOL> SSLError = ssl . SSLError <EOL> else : <EOL> SSLError = None <EOL> def create_fancy_connection ( tunnel_host = None , key_file = None , <EOL> cert_file = None , ca_certs = None ) : <EOL> class PresetProxyHTTPSConnection ( httplib . HTTPSConnection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> httplib . HTTPSConnection . __init__ ( self , * args , ** kwargs ) <EOL> self . _tunnel_host = tunnel_host <EOL> if tunnel_host : <EOL> logging . debug ( "<STR_LIT>" , tunnel_host ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> self . ca_certs = ca_certs <EOL> if can_validate_certs ( ) : <EOL> if self . ca_certs : <EOL> self . cert_reqs = ssl . CERT_REQUIRED <EOL> else : <EOL> self . cert_reqs = ssl . CERT_NONE <EOL> def _tunnel ( self ) : <EOL> self . _set_hostport ( self . _tunnel_host , None ) <EOL> logging . info ( "<STR_LIT>" , <EOL> self . host , self . port ) <EOL> self . send ( "<STR_LIT>" % ( self . host , self . port ) ) <EOL> response = self . response_class ( self . sock , strict = self . strict , <EOL> method = self . _method ) <EOL> ( _ , code , message ) = response . _read_status ( ) <EOL> if code != <NUM_LIT:200> : <EOL> self . close ( ) <EOL> raise socket . error , "<STR_LIT>" % ( <EOL> code , message . strip ( ) ) <EOL> while True : <EOL> line = response . fp . readline ( ) <EOL> if line == "<STR_LIT:\r\n>" : <EOL> break <EOL> def _get_valid_hosts_for_cert ( self , cert ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in cert : <EOL> return [ x [ <NUM_LIT:1> ] for x in cert [ '<STR_LIT>' ] if x [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT>' ] <EOL> else : <EOL> return [ x [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] for x in cert [ '<STR_LIT>' ] <EOL> if x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT>' ] <EOL> def _validate_certificate_hostname ( self , cert , hostname ) : <EOL> """<STR_LIT>""" <EOL> hosts = self . _get_valid_hosts_for_cert ( cert ) <EOL> for host in hosts : <EOL> host_re = host . replace ( '<STR_LIT:.>' , '<STR_LIT>' ) . replace ( '<STR_LIT:*>' , '<STR_LIT>' ) <EOL> if re . search ( '<STR_LIT>' % ( host_re , ) , hostname , re . I ) : <EOL> return True <EOL> return False <EOL> def connect ( self ) : <EOL> self . sock = _create_connection ( ( self . host , self . port ) ) <EOL> if self . _tunnel_host : <EOL> self . _tunnel ( ) <EOL> if can_validate_certs ( ) : <EOL> self . sock = ssl . wrap_socket ( self . sock , <EOL> keyfile = self . key_file , <EOL> certfile = self . cert_file , <EOL> ca_certs = self . ca_certs , <EOL> cert_reqs = self . cert_reqs ) <EOL> if self . cert_reqs & ssl . CERT_REQUIRED : <EOL> cert = self . sock . getpeercert ( ) <EOL> hostname = self . host . split ( '<STR_LIT::>' , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> if not self . _validate_certificate_hostname ( cert , hostname ) : <EOL> raise InvalidCertificateException ( hostname , cert , <EOL> '<STR_LIT>' ) <EOL> else : <EOL> ssl_socket = socket . ssl ( self . sock , <EOL> keyfile = self . key_file , <EOL> certfile = self . cert_file ) <EOL> self . sock = httplib . FakeSocket ( self . sock , ssl_socket ) <EOL> return PresetProxyHTTPSConnection <EOL> _GLOBAL_DEFAULT_TIMEOUT = object ( ) <EOL> def _create_connection ( address , timeout = _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> """<STR_LIT>""" <EOL> msg = "<STR_LIT>" <EOL> host , port = address <EOL> for res in socket . getaddrinfo ( host , port , <NUM_LIT:0> , socket . SOCK_STREAM ) : <EOL> af , socktype , proto , canonname , sa = res <EOL> sock = None <EOL> try : <EOL> sock = socket . socket ( af , socktype , proto ) <EOL> if timeout is not _GLOBAL_DEFAULT_TIMEOUT : <EOL> sock . settimeout ( timeout ) <EOL> sock . connect ( sa ) <EOL> return sock <EOL> except socket . error , msg : <EOL> if sock is not None : <EOL> sock . close ( ) <EOL> raise socket . error , msg <EOL> class FancyRequest ( urllib2 . Request ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> urllib2 . Request . __init__ ( self , * args , ** kwargs ) <EOL> self . _tunnel_host = None <EOL> self . _key_file = None <EOL> self . _cert_file = None <EOL> self . _ca_certs = None <EOL> def set_proxy ( self , host , type ) : <EOL> saved_type = None <EOL> if self . get_type ( ) == "<STR_LIT>" and not self . _tunnel_host : <EOL> self . _tunnel_host = self . get_host ( ) <EOL> saved_type = self . get_type ( ) <EOL> urllib2 . Request . set_proxy ( self , host , type ) <EOL> if saved_type : <EOL> self . type = saved_type <EOL> def set_ssl_info ( self , key_file = None , cert_file = None , ca_certs = None ) : <EOL> self . _key_file = key_file <EOL> self . _cert_file = cert_file <EOL> self . _ca_certs = ca_certs <EOL> class FancyProxyHandler ( urllib2 . ProxyHandler ) : <EOL> """<STR_LIT>""" <EOL> def _parse_proxy ( self , proxy ) : <EOL> """<STR_LIT>""" <EOL> scheme , r_scheme = splittype ( proxy ) <EOL> if not r_scheme . startswith ( "<STR_LIT:/>" ) : <EOL> scheme = None <EOL> authority = proxy <EOL> else : <EOL> if not r_scheme . startswith ( "<STR_LIT>" ) : <EOL> raise ValueError ( "<STR_LIT>" % proxy ) <EOL> end = r_scheme . find ( "<STR_LIT:/>" , <NUM_LIT:2> ) <EOL> if end == - <NUM_LIT:1> : <EOL> end = None <EOL> authority = r_scheme [ <NUM_LIT:2> : end ] <EOL> userinfo , hostport = splituser ( authority ) <EOL> if userinfo is not None : <EOL> user , password = splitpasswd ( userinfo ) <EOL> else : <EOL> user = password = None <EOL> return scheme , user , password , hostport <EOL> def proxy_open ( self , req , proxy , type ) : <EOL> orig_type = req . get_type ( ) <EOL> proxy_type , user , password , hostport = self . _parse_proxy ( proxy ) <EOL> if proxy_type is None : <EOL> proxy_type = orig_type <EOL> if user and password : <EOL> user_pass = "<STR_LIT>" % ( urllib2 . unquote ( user ) , urllib2 . unquote ( password ) ) <EOL> creds = base64 . b64encode ( user_pass ) . strip ( ) <EOL> req . add_header ( "<STR_LIT>" , "<STR_LIT>" + creds ) <EOL> hostport = urllib2 . unquote ( hostport ) <EOL> req . set_proxy ( hostport , proxy_type ) <EOL> if orig_type == "<STR_LIT>" : <EOL> return None <EOL> return urllib2 . ProxyHandler . proxy_open ( self , req , proxy , type ) <EOL> class FancyHTTPSHandler ( urllib2 . HTTPSHandler ) : <EOL> """<STR_LIT>""" <EOL> def do_open ( self , http_class , req ) : <EOL> try : <EOL> return urllib2 . HTTPSHandler . do_open ( <EOL> self , <EOL> create_fancy_connection ( req . _tunnel_host , <EOL> req . _key_file , <EOL> req . _cert_file , <EOL> req . _ca_certs ) , <EOL> req ) <EOL> except urllib2 . URLError , url_error : <EOL> try : <EOL> import ssl <EOL> if ( type ( url_error . reason ) == ssl . SSLError and <EOL> url_error . reason . args [ <NUM_LIT:0> ] == <NUM_LIT:1> ) : <EOL> raise InvalidCertificateException ( req . host , '<STR_LIT>' , <EOL> url_error . reason . args [ <NUM_LIT:1> ] ) <EOL> except ImportError : <EOL> pass <EOL> raise url_error <EOL> class FancyRedirectHandler ( urllib2 . HTTPRedirectHandler ) : <EOL> """<STR_LIT>""" <EOL> def redirect_request ( self , req , * args , ** kwargs ) : <EOL> new_req = urllib2 . HTTPRedirectHandler . redirect_request ( <EOL> self , req , * args , ** kwargs ) <EOL> if hasattr ( req , "<STR_LIT>" ) and isinstance ( new_req , urllib2 . Request ) : <EOL> if new_req . get_type ( ) == "<STR_LIT>" : <EOL> if req . _tunnel_host : <EOL> new_req . _tunnel_host = new_req . get_host ( ) <EOL> new_req . set_proxy ( req . host , "<STR_LIT>" ) <EOL> else : <EOL> new_req . _tunnel_host = None <EOL> new_req . type = "<STR_LIT>" <EOL> if hasattr ( req , "<STR_LIT>" ) and isinstance ( new_req , urllib2 . Request ) : <EOL> new_req . _key_file = req . _key_file <EOL> new_req . _cert_file = req . _cert_file <EOL> new_req . _ca_certs = req . _ca_certs <EOL> return new_req </s>
