<s> '''<STR_LIT>''' <EOL> import re <EOL> import os <EOL> import json <EOL> import base64 <EOL> import hashlib <EOL> import datetime <EOL> from framework . lib . filelock import FileLock <EOL> import tornado . httputil <EOL> class CacheHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cache_dir , request , cookie_regex , blacklist ) : <EOL> self . request = request <EOL> self . cache_dir = cache_dir <EOL> self . cookie_regex = cookie_regex <EOL> self . blacklist = blacklist <EOL> def calculate_hash ( self , callback = None ) : <EOL> cookie_string = '<STR_LIT>' <EOL> try : <EOL> if self . blacklist : <EOL> string_with_spaces = re . sub ( self . cookie_regex , '<STR_LIT>' , self . request . headers [ '<STR_LIT>' ] ) . strip ( ) <EOL> cookie_string = '<STR_LIT>' . join ( string_with_spaces . split ( '<STR_LIT:U+0020>' ) ) <EOL> else : <EOL> cookies_matrix = re . findall ( self . cookie_regex , self . request . headers [ '<STR_LIT>' ] ) <EOL> for cookie_tuple in cookies_matrix : <EOL> for item in cookie_tuple : <EOL> if item : <EOL> cookie_string += item . strip ( ) <EOL> except KeyError : <EOL> pass <EOL> request_mod = self . request . method + self . request . url + self . request . version <EOL> request_mod = request_mod + self . request . body + cookie_string <EOL> try : <EOL> request_mod = request_mod + self . request . headers [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> request_mod = request_mod + self . request . headers [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> pass <EOL> md5_hash = hashlib . md5 ( ) <EOL> md5_hash . update ( request_mod ) <EOL> self . request_hash = md5_hash . hexdigest ( ) <EOL> self . file_path = os . path . join ( self . cache_dir , self . request_hash ) <EOL> if callback : <EOL> callback ( self . request_hash ) <EOL> def create_response_object ( self ) : <EOL> return response_from_cache ( os . path . join ( self . cache_dir , self . request_hash ) ) <EOL> def dump ( self , response ) : <EOL> try : <EOL> response_body = unicode ( self . request . response_buffer , "<STR_LIT:utf-8>" ) <EOL> binary_response = False <EOL> except UnicodeDecodeError : <EOL> response_body = base64 . b64encode ( self . request . response_buffer ) <EOL> binary_response = True <EOL> cache_dict = { <EOL> '<STR_LIT>' : self . request . method , <EOL> '<STR_LIT>' : self . request . url , <EOL> '<STR_LIT>' : self . request . version , <EOL> '<STR_LIT>' : dict ( self . request . headers ) , <EOL> '<STR_LIT>' : self . request . body , <EOL> '<STR_LIT>' : response . request_time , <EOL> '<STR_LIT>' : self . request . local_timestamp . isoformat ( ) , <EOL> '<STR_LIT>' : response . code , <EOL> '<STR_LIT>' : dict ( response . headers ) , <EOL> '<STR_LIT>' : response_body , <EOL> '<STR_LIT>' : response . headers . get_list ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : binary_response <EOL> } <EOL> with open ( self . file_path , '<STR_LIT:w>' ) as outfile : <EOL> json . dump ( cache_dict , outfile ) <EOL> open ( self . file_path + '<STR_LIT>' , '<STR_LIT:w>' ) . close ( ) <EOL> self . file_lock . release ( ) <EOL> def load ( self ) : <EOL> try : <EOL> dummy = self . file_path <EOL> except Exception : <EOL> self . calculate_hash ( ) <EOL> finally : <EOL> if os . path . isfile ( self . file_path ) : <EOL> return ( self . create_response_object ( ) ) <EOL> else : <EOL> self . file_lock = FileLock ( self . file_path ) <EOL> self . file_lock . acquire ( ) <EOL> if os . path . isfile ( self . file_path ) : <EOL> self . file_lock . release ( ) <EOL> return ( self . create_response_object ( ) ) <EOL> else : <EOL> return None <EOL> class DummyObject ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . dummy_obj = True <EOL> def response_from_cache ( file_path ) : <EOL> dummyResponse = DummyObject ( ) <EOL> cache_dict = json . loads ( open ( file_path , '<STR_LIT:r>' ) . read ( ) ) <EOL> dummyResponse . code = cache_dict [ "<STR_LIT>" ] <EOL> dummyResponse . headers = tornado . httputil . HTTPHeaders ( cache_dict [ "<STR_LIT>" ] ) <EOL> dummyResponse . header_string = '<STR_LIT:\r\n>' . join ( [ "<STR_LIT>" % ( name , value ) for name , value in cache_dict [ "<STR_LIT>" ] . iteritems ( ) ] ) <EOL> if cache_dict [ "<STR_LIT>" ] == True : <EOL> dummyResponse . body = base64 . b64decode ( cache_dict [ "<STR_LIT>" ] ) <EOL> else : <EOL> dummyResponse . body = cache_dict [ "<STR_LIT>" ] <EOL> dummyResponse . request_time = cache_dict [ "<STR_LIT>" ] <EOL> dummyResponse . cookies = cache_dict [ "<STR_LIT>" ] <EOL> return dummyResponse <EOL> def request_from_cache ( file_path ) : <EOL> dummyRequest = DummyObject ( ) <EOL> cache_dict = json . loads ( open ( file_path , '<STR_LIT:r>' ) . read ( ) ) <EOL> dummyRequest . local_timestamp = datetime . datetime . strptime ( cache_dict [ "<STR_LIT>" ] . strip ( "<STR_LIT:\r\n>" ) , '<STR_LIT>' ) <EOL> dummyRequest . method = cache_dict [ "<STR_LIT>" ] <EOL> dummyRequest . url = cache_dict [ "<STR_LIT>" ] <EOL> dummyRequest . headers = cache_dict [ "<STR_LIT>" ] <EOL> dummyRequest . body = cache_dict [ "<STR_LIT>" ] <EOL> dummyRequest . raw_request = "<STR_LIT>" % ( cache_dict [ "<STR_LIT>" ] , cache_dict [ "<STR_LIT>" ] , cache_dict [ "<STR_LIT>" ] ) <EOL> for name , value in cache_dict [ "<STR_LIT>" ] . iteritems ( ) : <EOL> dummyRequest . raw_request += "<STR_LIT>" % ( name , value ) <EOL> if cache_dict [ "<STR_LIT>" ] : <EOL> dummyRequest . raw_request += cache_dict [ "<STR_LIT>" ] + "<STR_LIT>" <EOL> return dummyRequest </s>
