<s> """<STR_LIT>""" <EOL> import httplib <EOL> from urlparse import urlparse , urlunparse <EOL> import functools <EOL> import urllib2 <EOL> import os <EOL> import json <EOL> import context <EOL> import connection_mgr <EOL> import async <EOL> from gevent import socket <EOL> from gevent import ssl <EOL> class _GHTTPConnection ( httplib . HTTPConnection ) : <EOL> default_port = httplib . HTTP_PORT <EOL> def __init__ ( self , host , port = None , strict = None , <EOL> timeout = socket . _GLOBAL_DEFAULT_TIMEOUT , <EOL> protected = None ) : <EOL> httplib . HTTPConnection . __init__ ( self , host , port , strict , timeout ) <EOL> self . protected = protected <EOL> def connect ( self ) : <EOL> ctx = context . get_context ( ) <EOL> self . sock = ctx . connection_mgr . get_connection ( <EOL> ( self . host , self . port ) , self . protected , <EOL> read_timeout = self . timeout if isinstance ( self . timeout , float ) else None ) <EOL> if self . _tunnel_host : <EOL> self . _tunnel ( ) <EOL> def release_sock ( self ) : <EOL> if ( self . _HTTPConnection__state == httplib . _CS_IDLE <EOL> and self . _HTTPConnection__response is None <EOL> and self . sock ) : <EOL> context . get_context ( ) . connection_mgr . release_connection ( self . sock ) <EOL> self . sock = None <EOL> def _set_content_length ( self , body ) : <EOL> thelen = None <EOL> try : <EOL> thelen = str ( len ( body ) ) <EOL> except TypeError : <EOL> try : <EOL> thelen = str ( os . fstat ( body . fileno ( ) ) . st_size ) <EOL> except ( AttributeError , OSError ) : <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> if thelen is not None : <EOL> self . putheader ( '<STR_LIT>' , thelen ) <EOL> def __del__ ( self ) : <EOL> self . release_sock ( ) <EOL> class _GHTTPSConnection ( _GHTTPConnection ) : <EOL> default_port = httplib . HTTPS_PORT <EOL> def __init__ ( self , host , port = None , strict = None , <EOL> timeout = socket . _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> _GHTTPConnection . __init__ ( self , host , port , strict , timeout , <EOL> protected = connection_mgr . PLAIN_SSL ) <EOL> def connect ( self ) : <EOL> ctx = context . get_context ( ) <EOL> if self . _tunnel_host : <EOL> self . sock = ctx . connection_mgr . get_connection ( <EOL> ( self . host , self . port ) , ssl = None , <EOL> read_timeout = self . timeout if isinstance ( self . timeout , float ) else None ) <EOL> self . _tunnel ( ) <EOL> self . sock . _msock = ( ssl . wrap_socket ( self . sock . _msock ) <EOL> if self . protected == connection_mgr . PLAIN_SSL <EOL> else <EOL> async . wrap_socket_context ( self . sock . _msock , <EOL> self . protected ) ) <EOL> else : <EOL> _GHTTPConnection . connect ( self ) <EOL> def urllib2_request ( u2req , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> method = u2req . get_method ( ) <EOL> url = u2req . _Request__original <EOL> body = u2req . get_data ( ) <EOL> headers = dict ( u2req . unredirected_hdrs ) <EOL> headers . update ( ( k , v ) for k , v in u2req . headers . items ( ) <EOL> if k not in headers ) <EOL> try : <EOL> kwargs = { } <EOL> if timeout is not None : <EOL> kwargs [ '<STR_LIT>' ] = timeout <EOL> resp = request ( method , url , body , headers , ** kwargs ) <EOL> hr = resp . http_response <EOL> hr . recv = hr . read <EOL> fp = socket . _fileobject ( hr , close = True ) <EOL> aiu = urllib2 . addinfourl ( fp = fp , <EOL> headers = hr . msg , <EOL> url = resp . request . url ) <EOL> aiu . code = hr . status <EOL> aiu . msg = hr . reason <EOL> return aiu <EOL> except ValueError as e : <EOL> raise urllib2 . URLError ( e . msg ) <EOL> def request ( method , url , body = None , headers = None , <EOL> literal = False , use_protected = False , <EOL> timeout = socket . _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> '''<STR_LIT>''' <EOL> method = method . upper ( ) <EOL> if method not in _HTTP_METHODS : <EOL> raise ValueError ( "<STR_LIT>" . format ( method ) ) <EOL> parsed = urlparse ( url ) <EOL> if parsed . scheme not in ( '<STR_LIT:http>' , '<STR_LIT>' ) : <EOL> raise ValueError ( '<STR_LIT>' % parsed . scheme ) <EOL> domain , _ , port = parsed . netloc . partition ( '<STR_LIT::>' ) <EOL> try : <EOL> port = int ( port ) <EOL> except ValueError : <EOL> port = <NUM_LIT> if parsed . scheme == '<STR_LIT:http>' else <NUM_LIT> <EOL> protected = ( parsed . scheme == '<STR_LIT>' ) and ( True if use_protected <EOL> else "<STR_LIT>" ) <EOL> conn = _GHTTPConnection ( domain , port , protected = protected , timeout = timeout ) <EOL> selector = urlunparse ( parsed . _replace ( scheme = '<STR_LIT>' , netloc = '<STR_LIT>' ) ) <EOL> skips = { '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True } if literal else { } <EOL> if headers is None : <EOL> headers = { } <EOL> if not literal : <EOL> headers . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if isinstance ( body , dict ) : <EOL> body = json . dumps ( body ) <EOL> if '<STR_LIT:Content-Type>' not in headers : <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> conn . putrequest ( method , selector , ** skips ) <EOL> if not literal and body is not None and '<STR_LIT>' not in headers : <EOL> conn . _set_content_length ( body ) <EOL> for header , value in headers . items ( ) : <EOL> if type ( value ) is list : <EOL> for subvalue in value : <EOL> conn . putheader ( header , subvalue ) <EOL> else : <EOL> conn . putheader ( header , value ) <EOL> conn . endheaders ( ) <EOL> if body is not None : <EOL> conn . send ( body ) <EOL> raw = conn . getresponse ( ) <EOL> raw . _connection = conn <EOL> return Response ( <EOL> Request ( method , url , headers , body ) , <EOL> raw . status , raw . msg , raw ) <EOL> class Request ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , method , url , headers , body ) : <EOL> self . method = method <EOL> self . url = url <EOL> self . headers = headers <EOL> self . body = body <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . method , self . url ) <EOL> class Response ( object ) : <EOL> r'''<STR_LIT>''' <EOL> def __init__ ( self , request , status , headers , http_response ) : <EOL> self . request = request <EOL> self . status = status <EOL> self . headers = headers <EOL> self . http_response = http_response <EOL> self . _body = None <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self . http_response , '<STR_LIT>' ) : <EOL> self . http_response . _connection . release_sock ( ) <EOL> del self . http_response . _connection <EOL> self . http_response . close ( ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , * exc_info ) : <EOL> self . close ( ) <EOL> @ property <EOL> def body ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _body is None : <EOL> with self : <EOL> self . _body = self . http_response . read ( ) <EOL> return self . _body <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> self . status , self . request . method , self . request . url ) <EOL> _HTTP_METHODS = ( '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _init_methods ( ) : <EOL> g = globals ( ) <EOL> for m in _HTTP_METHODS : <EOL> g [ m . lower ( ) ] = functools . partial ( request , m ) <EOL> _init_methods ( ) </s>
