<s> import logging <EOL> from oic . oauth2 . exception import TimeFormatError <EOL> from oic . exception import UnSupported <EOL> from six import string_types <EOL> import six . moves . http_cookiejar as cookielib <EOL> from six . moves . http_cookiejar import http2time <EOL> logger = logging . getLogger ( __name__ ) <EOL> __author__ = '<STR_LIT>' <EOL> URL_ENCODED = '<STR_LIT>' <EOL> JSON_ENCODED = "<STR_LIT:application/json>" <EOL> DEFAULT_POST_CONTENT_TYPE = URL_ENCODED <EOL> PAIRS = { <EOL> "<STR_LIT:port>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:path>" : "<STR_LIT>" <EOL> } <EOL> ATTRS = { "<STR_LIT:version>" : None , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:value>" : None , <EOL> "<STR_LIT:port>" : None , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT:path>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : True } <EOL> def get_or_post ( uri , method , req , content_type = DEFAULT_POST_CONTENT_TYPE , <EOL> accept = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if method in [ "<STR_LIT:GET>" , "<STR_LIT>" ] : <EOL> _qp = req . to_urlencoded ( ) <EOL> if _qp : <EOL> path = uri + '<STR_LIT:?>' + _qp <EOL> else : <EOL> path = uri <EOL> body = None <EOL> elif method in [ "<STR_LIT:POST>" , "<STR_LIT>" ] : <EOL> path = uri <EOL> if content_type == URL_ENCODED : <EOL> body = req . to_urlencoded ( ) <EOL> elif content_type == JSON_ENCODED : <EOL> body = req . to_json ( ) <EOL> else : <EOL> raise UnSupported ( <EOL> "<STR_LIT>" % content_type ) <EOL> header_ext = { "<STR_LIT:Content-Type>" : content_type } <EOL> if accept : <EOL> header_ext = { "<STR_LIT>" : accept } <EOL> if "<STR_LIT>" in kwargs . keys ( ) : <EOL> kwargs [ "<STR_LIT>" ] . update ( header_ext ) <EOL> else : <EOL> kwargs [ "<STR_LIT>" ] = header_ext <EOL> else : <EOL> raise UnSupported ( "<STR_LIT>" % method ) <EOL> return path , body , kwargs <EOL> def set_cookie ( cookiejar , kaka ) : <EOL> """<STR_LIT>""" <EOL> for cookie_name , morsel in kaka . items ( ) : <EOL> std_attr = ATTRS . copy ( ) <EOL> std_attr [ "<STR_LIT:name>" ] = cookie_name <EOL> _tmp = morsel . coded_value <EOL> if _tmp . startswith ( '<STR_LIT:">' ) and _tmp . endswith ( '<STR_LIT:">' ) : <EOL> std_attr [ "<STR_LIT:value>" ] = _tmp [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> else : <EOL> std_attr [ "<STR_LIT:value>" ] = _tmp <EOL> std_attr [ "<STR_LIT:version>" ] = <NUM_LIT:0> <EOL> attr = "<STR_LIT>" <EOL> try : <EOL> for attr in morsel . keys ( ) : <EOL> if attr in ATTRS : <EOL> if morsel [ attr ] : <EOL> if attr == "<STR_LIT>" : <EOL> std_attr [ attr ] = http2time ( morsel [ attr ] ) <EOL> else : <EOL> std_attr [ attr ] = morsel [ attr ] <EOL> elif attr == "<STR_LIT>" : <EOL> if morsel [ attr ] : <EOL> std_attr [ "<STR_LIT>" ] = http2time ( morsel [ attr ] ) <EOL> except TimeFormatError : <EOL> logger . info ( <EOL> "<STR_LIT>" % ( <EOL> attr , ) ) <EOL> continue <EOL> for att , spec in PAIRS . items ( ) : <EOL> if std_attr [ att ] : <EOL> std_attr [ spec ] = True <EOL> if std_attr [ "<STR_LIT>" ] and std_attr [ "<STR_LIT>" ] . startswith ( "<STR_LIT:.>" ) : <EOL> std_attr [ "<STR_LIT>" ] = True <EOL> if morsel [ "<STR_LIT>" ] is <NUM_LIT:0> : <EOL> try : <EOL> cookiejar . clear ( domain = std_attr [ "<STR_LIT>" ] , <EOL> path = std_attr [ "<STR_LIT:path>" ] , <EOL> name = std_attr [ "<STR_LIT:name>" ] ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> if "<STR_LIT:version>" in std_attr : <EOL> try : <EOL> std_attr [ "<STR_LIT:version>" ] = std_attr [ "<STR_LIT:version>" ] . split ( "<STR_LIT:U+002C>" ) [ <NUM_LIT:0> ] <EOL> except ( TypeError , AttributeError ) : <EOL> pass <EOL> new_cookie = cookielib . Cookie ( ** std_attr ) <EOL> cookiejar . set_cookie ( new_cookie ) <EOL> def match_to_ ( val , vlist ) : <EOL> if isinstance ( vlist , string_types ) : <EOL> if vlist . startswith ( val ) : <EOL> return True <EOL> else : <EOL> for v in vlist : <EOL> if v . startswith ( val ) : <EOL> return True <EOL> return False <EOL> def verify_header ( reqresp , body_type ) : <EOL> logger . debug ( "<STR_LIT>" % ( reqresp . headers , ) ) <EOL> logger . debug ( "<STR_LIT>" % ( reqresp . text , ) ) <EOL> if body_type == "<STR_LIT>" : <EOL> _ctype = reqresp . headers [ "<STR_LIT>" ] <EOL> if match_to_ ( "<STR_LIT:application/json>" , _ctype ) : <EOL> body_type = '<STR_LIT>' <EOL> elif match_to_ ( "<STR_LIT>" , _ctype ) : <EOL> body_type = "<STR_LIT>" <EOL> elif match_to_ ( URL_ENCODED , _ctype ) : <EOL> body_type = '<STR_LIT>' <EOL> else : <EOL> body_type = '<STR_LIT>' <EOL> elif body_type == "<STR_LIT>" : <EOL> try : <EOL> assert match_to_ ( "<STR_LIT:application/json>" , <EOL> reqresp . headers [ "<STR_LIT>" ] ) <EOL> except AssertionError : <EOL> try : <EOL> assert match_to_ ( "<STR_LIT>" , <EOL> reqresp . headers [ "<STR_LIT>" ] ) <EOL> body_type = "<STR_LIT>" <EOL> except AssertionError : <EOL> raise AssertionError ( "<STR_LIT>" % ( <EOL> reqresp . headers [ "<STR_LIT>" ] , ) ) <EOL> elif body_type == "<STR_LIT>" : <EOL> try : <EOL> assert match_to_ ( "<STR_LIT>" , <EOL> reqresp . headers [ "<STR_LIT>" ] ) <EOL> except AssertionError : <EOL> raise AssertionError ( <EOL> "<STR_LIT>" . format ( <EOL> reqresp . headers [ "<STR_LIT>" ] ) ) <EOL> elif body_type == "<STR_LIT>" : <EOL> try : <EOL> assert match_to_ ( DEFAULT_POST_CONTENT_TYPE , <EOL> reqresp . headers [ "<STR_LIT>" ] ) <EOL> except AssertionError : <EOL> assert match_to_ ( "<STR_LIT>" , <EOL> reqresp . headers [ "<STR_LIT>" ] ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % body_type ) <EOL> return body_type </s>
