<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> import urllib2 <EOL> import time <EOL> import cStringIO <EOL> import sys <EOL> import openid <EOL> import openid . urinorm <EOL> try : <EOL> import httplib2 <EOL> except ImportError : <EOL> httplib2 = None <EOL> try : <EOL> import pycurl <EOL> except ImportError : <EOL> pycurl = None <EOL> USER_AGENT = "<STR_LIT>" % ( openid . __version__ , sys . platform ) <EOL> MAX_RESPONSE_KB = <NUM_LIT> <EOL> def fetch ( url , body = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> fetcher = getDefaultFetcher ( ) <EOL> return fetcher . fetch ( url , body , headers ) <EOL> def createHTTPFetcher ( ) : <EOL> """<STR_LIT>""" <EOL> if pycurl is None : <EOL> fetcher = Urllib2Fetcher ( ) <EOL> else : <EOL> fetcher = CurlHTTPFetcher ( ) <EOL> return fetcher <EOL> _default_fetcher = None <EOL> def getDefaultFetcher ( ) : <EOL> """<STR_LIT>""" <EOL> global _default_fetcher <EOL> if _default_fetcher is None : <EOL> setDefaultFetcher ( createHTTPFetcher ( ) ) <EOL> return _default_fetcher <EOL> def setDefaultFetcher ( fetcher , wrap_exceptions = True ) : <EOL> """<STR_LIT>""" <EOL> global _default_fetcher <EOL> if fetcher is None or not wrap_exceptions : <EOL> _default_fetcher = fetcher <EOL> else : <EOL> _default_fetcher = ExceptionWrappingFetcher ( fetcher ) <EOL> def usingCurl ( ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( getDefaultFetcher ( ) , CurlHTTPFetcher ) <EOL> class HTTPResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> headers = None <EOL> status = None <EOL> body = None <EOL> final_url = None <EOL> def __init__ ( self , final_url = None , status = None , headers = None , body = None ) : <EOL> self . final_url = final_url <EOL> self . status = status <EOL> self . headers = headers <EOL> self . body = body <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , <EOL> self . status , <EOL> self . final_url ) <EOL> class HTTPFetcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def fetch ( self , url , body = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def _allowedURL ( url ) : <EOL> return url . startswith ( '<STR_LIT>' ) or url . startswith ( '<STR_LIT>' ) <EOL> class HTTPFetchingError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , why = None ) : <EOL> Exception . __init__ ( self , why ) <EOL> self . why = why <EOL> class ExceptionWrappingFetcher ( HTTPFetcher ) : <EOL> """<STR_LIT>""" <EOL> uncaught_exceptions = ( SystemExit , KeyboardInterrupt , MemoryError ) <EOL> def __init__ ( self , fetcher ) : <EOL> self . fetcher = fetcher <EOL> def fetch ( self , * args , ** kwargs ) : <EOL> try : <EOL> return self . fetcher . fetch ( * args , ** kwargs ) <EOL> except self . uncaught_exceptions : <EOL> raise <EOL> except : <EOL> exc_cls , exc_inst = sys . exc_info ( ) [ : <NUM_LIT:2> ] <EOL> if exc_inst is None : <EOL> exc_inst = exc_cls <EOL> raise HTTPFetchingError ( why = exc_inst ) <EOL> class Urllib2Fetcher ( HTTPFetcher ) : <EOL> """<STR_LIT>""" <EOL> urlopen = staticmethod ( urllib2 . urlopen ) <EOL> def fetch ( self , url , body = None , headers = None ) : <EOL> if not _allowedURL ( url ) : <EOL> raise ValueError ( '<STR_LIT>' % ( url , ) ) <EOL> if headers is None : <EOL> headers = { } <EOL> headers . setdefault ( <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" % ( USER_AGENT , urllib2 . __version__ , ) ) <EOL> headers . setdefault ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % ( <NUM_LIT> * MAX_RESPONSE_KB , ) ) <EOL> req = urllib2 . Request ( url , data = body , headers = headers ) <EOL> try : <EOL> f = self . urlopen ( req ) <EOL> try : <EOL> return self . _makeResponse ( f ) <EOL> finally : <EOL> f . close ( ) <EOL> except urllib2 . HTTPError , why : <EOL> try : <EOL> return self . _makeResponse ( why ) <EOL> finally : <EOL> why . close ( ) <EOL> def _makeResponse ( self , urllib2_response ) : <EOL> resp = HTTPResponse ( ) <EOL> resp . body = urllib2_response . read ( MAX_RESPONSE_KB * <NUM_LIT> ) <EOL> resp . final_url = urllib2_response . geturl ( ) <EOL> resp . headers = dict ( urllib2_response . info ( ) . items ( ) ) <EOL> if hasattr ( urllib2_response , '<STR_LIT:code>' ) : <EOL> resp . status = urllib2_response . code <EOL> else : <EOL> resp . status = <NUM_LIT:200> <EOL> return resp <EOL> class HTTPError ( HTTPFetchingError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class CurlHTTPFetcher ( HTTPFetcher ) : <EOL> """<STR_LIT>""" <EOL> ALLOWED_TIME = <NUM_LIT:20> <EOL> def __init__ ( self ) : <EOL> HTTPFetcher . __init__ ( self ) <EOL> if pycurl is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> def _parseHeaders ( self , header_file ) : <EOL> header_file . seek ( <NUM_LIT:0> ) <EOL> unused_http_status_line = header_file . readline ( ) <EOL> lines = [ line . strip ( ) for line in header_file ] <EOL> empty_line = lines . pop ( ) <EOL> if empty_line : <EOL> raise HTTPError ( "<STR_LIT>" % ( line , ) ) <EOL> headers = { } <EOL> for line in lines : <EOL> try : <EOL> name , value = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> raise HTTPError ( <EOL> "<STR_LIT>" % ( line , ) ) <EOL> value = value . strip ( ) <EOL> name = name . lower ( ) <EOL> headers [ name ] = value <EOL> return headers <EOL> def _checkURL ( self , url ) : <EOL> return _allowedURL ( url ) <EOL> def fetch ( self , url , body = None , headers = None ) : <EOL> stop = int ( time . time ( ) ) + self . ALLOWED_TIME <EOL> off = self . ALLOWED_TIME <EOL> if headers is None : <EOL> headers = { } <EOL> headers . setdefault ( '<STR_LIT>' , <EOL> "<STR_LIT>" % ( USER_AGENT , pycurl . version , ) ) <EOL> header_list = [ ] <EOL> if headers is not None : <EOL> for header_name , header_value in headers . iteritems ( ) : <EOL> header_list . append ( '<STR_LIT>' % ( header_name , header_value ) ) <EOL> c = pycurl . Curl ( ) <EOL> try : <EOL> c . setopt ( pycurl . NOSIGNAL , <NUM_LIT:1> ) <EOL> if header_list : <EOL> c . setopt ( pycurl . HTTPHEADER , header_list ) <EOL> if body is not None : <EOL> c . setopt ( pycurl . POST , <NUM_LIT:1> ) <EOL> c . setopt ( pycurl . POSTFIELDS , body ) <EOL> while off > <NUM_LIT:0> : <EOL> if not self . _checkURL ( url ) : <EOL> raise HTTPError ( "<STR_LIT>" % ( url , ) ) <EOL> data = cStringIO . StringIO ( ) <EOL> def write_data ( chunk ) : <EOL> if data . tell ( ) > <NUM_LIT> * MAX_RESPONSE_KB : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return data . write ( chunk ) <EOL> response_header_data = cStringIO . StringIO ( ) <EOL> c . setopt ( pycurl . WRITEFUNCTION , write_data ) <EOL> c . setopt ( pycurl . HEADERFUNCTION , response_header_data . write ) <EOL> c . setopt ( pycurl . TIMEOUT , off ) <EOL> c . setopt ( pycurl . URL , openid . urinorm . urinorm ( url ) ) <EOL> c . setopt ( pycurl . RANGE , '<STR_LIT>' % ( MAX_RESPONSE_KB * <NUM_LIT> ) ) <EOL> c . perform ( ) <EOL> response_headers = self . _parseHeaders ( response_header_data ) <EOL> code = c . getinfo ( pycurl . RESPONSE_CODE ) <EOL> if code in [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] : <EOL> url = response_headers . get ( '<STR_LIT:location>' ) <EOL> if url is None : <EOL> raise HTTPError ( <EOL> '<STR_LIT>' % code ) <EOL> c . setopt ( pycurl . POST , <NUM_LIT:0> ) <EOL> else : <EOL> resp = HTTPResponse ( ) <EOL> resp . headers = response_headers <EOL> resp . status = code <EOL> resp . final_url = url <EOL> resp . body = data . getvalue ( ) <EOL> return resp <EOL> off = stop - int ( time . time ( ) ) <EOL> raise HTTPError ( "<STR_LIT>" % ( url , ) ) <EOL> finally : <EOL> c . close ( ) <EOL> class HTTPLib2Fetcher ( HTTPFetcher ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cache = None ) : <EOL> """<STR_LIT>""" <EOL> if httplib2 is None : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> super ( HTTPLib2Fetcher , self ) . __init__ ( ) <EOL> self . httplib2 = httplib2 . Http ( cache ) <EOL> self . httplib2 . force_exception_to_status_code = False <EOL> def fetch ( self , url , body = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if body : <EOL> method = '<STR_LIT:POST>' <EOL> else : <EOL> method = '<STR_LIT:GET>' <EOL> if headers is None : <EOL> headers = { } <EOL> headers . setdefault ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % ( <NUM_LIT> * MAX_RESPONSE_KB , ) ) <EOL> if not ( url . startswith ( '<STR_LIT>' ) or url . startswith ( '<STR_LIT>' ) ) : <EOL> raise ValueError ( '<STR_LIT>' % ( url , ) ) <EOL> httplib2_response , content = self . httplib2 . request ( <EOL> url , method , body = body , headers = headers ) <EOL> try : <EOL> final_url = httplib2_response [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> assert not httplib2_response . previous <EOL> assert httplib2_response . status != <NUM_LIT:200> <EOL> final_url = url <EOL> return HTTPResponse ( <EOL> body = content , <EOL> final_url = final_url , <EOL> headers = dict ( httplib2_response . items ( ) ) , <EOL> status = httplib2_response . status , <EOL> ) </s>
