<s> from logging import getLogger <EOL> from tornado . web import RequestHandler <EOL> from os . path import join , relpath , pardir <EOL> class ResponseFromFileHandler ( RequestHandler ) : <EOL> log = getLogger ( '<STR_LIT>' ) <EOL> def __init__ ( self , application , request , ** kwargs ) : <EOL> self . path = None <EOL> RequestHandler . __init__ ( self , application , request , ** kwargs ) <EOL> def initialize ( self , path ) : <EOL> self . path = path <EOL> def get ( self , file_path ) : <EOL> file_path = file_path . split ( "<STR_LIT:?>" ) [ <NUM_LIT:0> ] <EOL> file_path = join ( self . path , file_path ) <EOL> if relpath ( file_path , self . path ) [ : <NUM_LIT:2> ] == pardir : <EOL> self . set_status ( <NUM_LIT> ) <EOL> self . finish ( '<STR_LIT>' ) <EOL> try : <EOL> f = open ( file_path , '<STR_LIT:r>' ) <EOL> file_contents = f . read ( ) <EOL> if hasattr ( self . request , "<STR_LIT>" ) : <EOL> self . request . connection . stream . set_close_callback ( None ) <EOL> if len ( file_contents ) > <NUM_LIT:0> : <EOL> self . request . write ( file_contents ) <EOL> self . request . finish ( ) <EOL> f . close ( ) <EOL> return <EOL> except IOError : <EOL> self . set_status ( <NUM_LIT> ) <EOL> self . finish ( '<STR_LIT>' % file_path ) </s>
