<s> from urllib import quote , unquote <EOL> from json import loads as json_loads <EOL> import copy <EOL> from slogging . compressing_file_reader import CompressingFileReader <EOL> from swift . proxy . server import Application <EOL> from swift . common import swob <EOL> def make_request_body_file ( source_file , compress = True ) : <EOL> if hasattr ( source_file , '<STR_LIT>' ) : <EOL> source_file . seek ( <NUM_LIT:0> ) <EOL> else : <EOL> source_file = open ( source_file , '<STR_LIT:rb>' ) <EOL> if compress : <EOL> compressed_file = CompressingFileReader ( source_file ) <EOL> return compressed_file <EOL> return source_file <EOL> def swob_request_copy ( orig_req , source_file = None , compress = True ) : <EOL> req_copy = swob . Request ( orig_req . environ . copy ( ) ) <EOL> req_copy . headers = dict ( orig_req . headers ) <EOL> if source_file : <EOL> req_copy . body_file = make_request_body_file ( source_file , <EOL> compress = compress ) <EOL> return req_copy <EOL> class InternalProxy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , proxy_server_conf = None , logger = None , retries = <NUM_LIT:0> , <EOL> memcache = None ) : <EOL> self . upload_app = Application ( proxy_server_conf , memcache = memcache , <EOL> logger = logger ) <EOL> self . retries = retries <EOL> def _handle_request ( self , req , source_file = None , compress = True ) : <EOL> req = self . upload_app . update_request ( req ) <EOL> req_copy = swob_request_copy ( req , source_file = source_file , <EOL> compress = compress ) <EOL> req_copy . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> resp = self . upload_app . handle_request ( req_copy ) <EOL> tries = <NUM_LIT:1> <EOL> while ( resp . status_int < <NUM_LIT:200> or resp . status_int > <NUM_LIT> ) and tries < self . retries : <EOL> req_copy = swob_request_copy ( req , source_file = source_file , <EOL> compress = compress ) <EOL> resp = self . upload_app . handle_request ( req_copy ) <EOL> tries += <NUM_LIT:1> <EOL> return resp <EOL> def upload_file ( self , source_file , account , container , object_name , <EOL> compress = True , content_type = '<STR_LIT>' , <EOL> etag = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> target_name = '<STR_LIT>' % ( account , container , object_name ) <EOL> if not self . create_container ( account , container ) : <EOL> return False <EOL> send_headers = { '<STR_LIT>' : '<STR_LIT>' } <EOL> if headers : <EOL> send_headers . update ( headers ) <EOL> req = swob . Request . blank ( target_name , <EOL> environ = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> headers = send_headers ) <EOL> req . environ [ '<STR_LIT>' ] = content_type <EOL> req . content_length = None <EOL> if etag : <EOL> req . headers [ '<STR_LIT>' ] = etag <EOL> resp = self . _handle_request ( req , source_file = source_file , <EOL> compress = compress ) <EOL> if not ( <NUM_LIT:200> <= resp . status_int < <NUM_LIT> ) : <EOL> return False <EOL> return True <EOL> def get_object ( self , account , container , object_name ) : <EOL> """<STR_LIT>""" <EOL> req = swob . Request . blank ( '<STR_LIT>' % <EOL> ( account , container , object_name ) , <EOL> environ = { '<STR_LIT>' : '<STR_LIT:GET>' } ) <EOL> resp = self . _handle_request ( req ) <EOL> return resp . status_int , resp . app_iter <EOL> def create_container ( self , account , container ) : <EOL> """<STR_LIT>""" <EOL> req = swob . Request . blank ( '<STR_LIT>' % ( account , container ) , <EOL> environ = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> resp = self . _handle_request ( req ) <EOL> return <NUM_LIT:200> <= resp . status_int < <NUM_LIT> <EOL> def get_container_list ( self , account , container , marker = None , <EOL> end_marker = None , limit = None , prefix = None , <EOL> delimiter = None , full_listing = True ) : <EOL> """<STR_LIT>""" <EOL> if full_listing : <EOL> rv = [ ] <EOL> listing = self . get_container_list ( account , container , marker , <EOL> end_marker , limit , prefix , <EOL> delimiter , full_listing = False ) <EOL> while listing : <EOL> rv . extend ( listing ) <EOL> if not delimiter : <EOL> marker = listing [ - <NUM_LIT:1> ] [ '<STR_LIT:name>' ] <EOL> else : <EOL> marker = listing [ - <NUM_LIT:1> ] . get ( '<STR_LIT:name>' , listing [ - <NUM_LIT:1> ] . get ( '<STR_LIT>' ) ) <EOL> listing = self . get_container_list ( account , container , marker , <EOL> end_marker , limit , prefix , <EOL> delimiter , <EOL> full_listing = False ) <EOL> return rv <EOL> path = '<STR_LIT>' % ( account , quote ( container ) ) <EOL> qs = '<STR_LIT>' <EOL> if marker : <EOL> qs += '<STR_LIT>' % quote ( marker ) <EOL> if end_marker : <EOL> qs += '<STR_LIT>' % quote ( end_marker ) <EOL> if limit : <EOL> qs += '<STR_LIT>' % limit <EOL> if prefix : <EOL> qs += '<STR_LIT>' % quote ( prefix ) <EOL> if delimiter : <EOL> qs += '<STR_LIT>' % quote ( delimiter ) <EOL> path += '<STR_LIT>' % qs <EOL> req = swob . Request . blank ( path , environ = { '<STR_LIT>' : '<STR_LIT:GET>' } ) <EOL> resp = self . _handle_request ( req ) <EOL> if resp . status_int < <NUM_LIT:200> or resp . status_int >= <NUM_LIT> : <EOL> return [ ] <EOL> if resp . status_int == <NUM_LIT> : <EOL> return [ ] <EOL> return json_loads ( resp . body ) <EOL> def get_container_metadata ( self , account , container ) : <EOL> path = '<STR_LIT>' % ( account , container ) <EOL> req = swob . Request . blank ( path , environ = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> resp = self . _handle_request ( req ) <EOL> out = { } <EOL> for k , v in resp . headers . iteritems ( ) : <EOL> if k . lower ( ) . startswith ( '<STR_LIT>' ) : <EOL> out [ k ] = v <EOL> return out </s>
