<s> import io <EOL> import logging <EOL> from . rewrite import RewriteProxy <EOL> class Route ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , script ) : <EOL> if hasattr ( script , "<STR_LIT>" ) : <EOL> self . script = script . load ( ) <EOL> else : <EOL> self . script = script <EOL> self . empty_buf = True <EOL> if hasattr ( self . script , '<STR_LIT>' ) : <EOL> self . proxy_input = self . rewrite_request <EOL> self . empty_buf = False <EOL> else : <EOL> self . proxy_input = self . proxy_io <EOL> if hasattr ( self . script , '<STR_LIT>' ) : <EOL> self . proxy_connected = self . rewrite_response <EOL> else : <EOL> self . proxy_connected = self . proxy_io <EOL> self . log = logging . getLogger ( __name__ ) <EOL> def proxy ( self , data ) : <EOL> return self . script . proxy ( data ) <EOL> def proxy_io ( self , src , dest , buf = None , extra = None ) : <EOL> while True : <EOL> data = src . recv ( io . DEFAULT_BUFFER_SIZE ) <EOL> if not data : <EOL> break <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> dest . sendall ( data ) <EOL> def rewrite ( self , src , dest , fun , buf = None , extra = None ) : <EOL> rwproxy = RewriteProxy ( src , dest , fun , extra = extra , buf = buf ) <EOL> rwproxy . run ( ) <EOL> def rewrite_request ( self , src , dest , buf = None , extra = None ) : <EOL> self . rewrite ( src , dest , self . script . rewrite_request , buf = buf , <EOL> extra = extra ) <EOL> def rewrite_response ( self , src , dest , extra = None ) : <EOL> self . rewrite ( src , dest , self . script . rewrite_response , <EOL> extra = extra ) </s>
