<s> """<STR_LIT>""" <EOL> import re <EOL> import os <EOL> from requests . cookies import RequestsCookieJar , create_cookie <EOL> from httpie . compat import urlsplit <EOL> from httpie . config import BaseConfigDict , DEFAULT_CONFIG_DIR <EOL> from httpie . plugins import plugin_manager <EOL> SESSIONS_DIR_NAME = '<STR_LIT>' <EOL> DEFAULT_SESSIONS_DIR = os . path . join ( DEFAULT_CONFIG_DIR , SESSIONS_DIR_NAME ) <EOL> VALID_SESSION_NAME_PATTERN = re . compile ( '<STR_LIT>' ) <EOL> SESSION_IGNORED_HEADER_PREFIXES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_response ( requests_session , session_name , <EOL> config_dir , args , read_only = False ) : <EOL> """<STR_LIT>""" <EOL> from . client import get_requests_kwargs , dump_request <EOL> if os . path . sep in session_name : <EOL> path = os . path . expanduser ( session_name ) <EOL> else : <EOL> hostname = ( args . headers . get ( '<STR_LIT>' , None ) or <EOL> urlsplit ( args . url ) . netloc . split ( '<STR_LIT:@>' ) [ - <NUM_LIT:1> ] ) <EOL> if not hostname : <EOL> hostname = '<STR_LIT:localhost>' <EOL> hostname = hostname . replace ( '<STR_LIT::>' , '<STR_LIT:_>' ) <EOL> path = os . path . join ( config_dir , <EOL> SESSIONS_DIR_NAME , <EOL> hostname , <EOL> session_name + '<STR_LIT>' ) <EOL> session = Session ( path ) <EOL> session . load ( ) <EOL> kwargs = get_requests_kwargs ( args , base_headers = session . headers ) <EOL> if args . debug : <EOL> dump_request ( kwargs ) <EOL> session . update_headers ( kwargs [ '<STR_LIT>' ] ) <EOL> if args . auth : <EOL> session . auth = { <EOL> '<STR_LIT:type>' : args . auth_type , <EOL> '<STR_LIT:username>' : args . auth . key , <EOL> '<STR_LIT:password>' : args . auth . value , <EOL> } <EOL> elif session . auth : <EOL> kwargs [ '<STR_LIT>' ] = session . auth <EOL> requests_session . cookies = session . cookies <EOL> try : <EOL> response = requests_session . request ( ** kwargs ) <EOL> except Exception : <EOL> raise <EOL> else : <EOL> if session . is_new ( ) or not read_only : <EOL> session . cookies = requests_session . cookies <EOL> session . save ( ) <EOL> return response <EOL> class Session ( BaseConfigDict ) : <EOL> helpurl = '<STR_LIT>' <EOL> about = '<STR_LIT>' <EOL> def __init__ ( self , path , * args , ** kwargs ) : <EOL> super ( Session , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _path = path <EOL> self [ '<STR_LIT>' ] = { } <EOL> self [ '<STR_LIT>' ] = { } <EOL> self [ '<STR_LIT>' ] = { <EOL> '<STR_LIT:type>' : None , <EOL> '<STR_LIT:username>' : None , <EOL> '<STR_LIT:password>' : None <EOL> } <EOL> def _get_path ( self ) : <EOL> return self . _path <EOL> def update_headers ( self , request_headers ) : <EOL> """<STR_LIT>""" <EOL> for name , value in request_headers . items ( ) : <EOL> if value is None : <EOL> continue <EOL> value = value . decode ( '<STR_LIT:utf8>' ) <EOL> if name == '<STR_LIT>' and value . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> for prefix in SESSION_IGNORED_HEADER_PREFIXES : <EOL> if name . lower ( ) . startswith ( prefix . lower ( ) ) : <EOL> break <EOL> else : <EOL> self [ '<STR_LIT>' ] [ name ] = value <EOL> @ property <EOL> def headers ( self ) : <EOL> return self [ '<STR_LIT>' ] <EOL> @ property <EOL> def cookies ( self ) : <EOL> jar = RequestsCookieJar ( ) <EOL> for name , cookie_dict in self [ '<STR_LIT>' ] . items ( ) : <EOL> jar . set_cookie ( create_cookie ( <EOL> name , cookie_dict . pop ( '<STR_LIT:value>' ) , ** cookie_dict ) ) <EOL> jar . clear_expired_cookies ( ) <EOL> return jar <EOL> @ cookies . setter <EOL> def cookies ( self , jar ) : <EOL> """<STR_LIT>""" <EOL> stored_attrs = [ '<STR_LIT:value>' , '<STR_LIT:path>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self [ '<STR_LIT>' ] = { } <EOL> for cookie in jar : <EOL> self [ '<STR_LIT>' ] [ cookie . name ] = dict ( <EOL> ( attname , getattr ( cookie , attname ) ) <EOL> for attname in stored_attrs <EOL> ) <EOL> @ property <EOL> def auth ( self ) : <EOL> auth = self . get ( '<STR_LIT>' , None ) <EOL> if not auth or not auth [ '<STR_LIT:type>' ] : <EOL> return <EOL> auth_plugin = plugin_manager . get_auth_plugin ( auth [ '<STR_LIT:type>' ] ) ( ) <EOL> return auth_plugin . get_auth ( auth [ '<STR_LIT:username>' ] , auth [ '<STR_LIT:password>' ] ) <EOL> @ auth . setter <EOL> def auth ( self , auth ) : <EOL> assert set ( [ '<STR_LIT:type>' , '<STR_LIT:username>' , '<STR_LIT:password>' ] ) == set ( auth . keys ( ) ) <EOL> self [ '<STR_LIT>' ] = auth </s>
