<s> "<STR_LIT>" <EOL> from dashlivesim import SERVER_AGENT <EOL> import httplib <EOL> from os . path import splitext <EOL> from time import time <EOL> from dashlivesim . dashlib import dash_proxy <EOL> def reply ( code , resp , body = '<STR_LIT>' , headers = { } ) : <EOL> "<STR_LIT>" <EOL> status = str ( code ) + '<STR_LIT:U+0020>' + httplib . responses [ code ] <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> headers [ '<STR_LIT>' ] = SERVER_AGENT <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:*>' <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if body : <EOL> headers [ '<STR_LIT>' ] = str ( len ( body ) ) <EOL> if not '<STR_LIT:Content-Type>' in headers : <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> resp ( status , headers . items ( ) ) <EOL> return [ body ] <EOL> def application ( environment , start_response ) : <EOL> "<STR_LIT>" <EOL> hostname = environment [ '<STR_LIT>' ] <EOL> url = environment [ '<STR_LIT>' ] <EOL> vod_conf_dir = environment [ '<STR_LIT>' ] <EOL> content_root = environment [ '<STR_LIT>' ] <EOL> is_https = environment . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> path_parts = url . split ( '<STR_LIT:/>' ) <EOL> ext = splitext ( path_parts [ - <NUM_LIT:1> ] ) [ <NUM_LIT:1> ] <EOL> args = None <EOL> now = time ( ) <EOL> range_line = None <EOL> if '<STR_LIT>' in environment : <EOL> range_line = environment [ '<STR_LIT>' ] <EOL> success = True <EOL> mimetype = get_mime_type ( ext ) <EOL> status = httplib . OK <EOL> payload_in = None <EOL> try : <EOL> response = dash_proxy . handle_request ( hostname , path_parts [ <NUM_LIT:1> : ] , args , vod_conf_dir , content_root , now , None , <EOL> is_https ) <EOL> if isinstance ( response , basestring ) : <EOL> payload_in = response <EOL> if not payload_in : <EOL> success = False <EOL> else : <EOL> if not response [ '<STR_LIT>' ] : <EOL> success = False <EOL> payload_in = response [ '<STR_LIT>' ] <EOL> except Exception , exc : <EOL> success = False <EOL> print "<STR_LIT>" % exc <EOL> payload_in = "<STR_LIT>" % ( exc , url ) <EOL> if not success : <EOL> if payload_in == "<STR_LIT>" : <EOL> print "<STR_LIT>" <EOL> payload_in = "<STR_LIT>" <EOL> elif payload_in is None : <EOL> print "<STR_LIT>" <EOL> payload_in = "<STR_LIT>" <EOL> status = httplib . NOT_FOUND <EOL> mimetype = "<STR_LIT>" <EOL> payload_out = payload_in <EOL> headers = { '<STR_LIT:Content-Type>' : mimetype } <EOL> if status != httplib . NOT_FOUND : <EOL> if range_line : <EOL> payload_out , range_out = handle_byte_range ( payload_in , range_line ) <EOL> if range_out != "<STR_LIT>" : <EOL> headers [ '<STR_LIT>' ] = range_out <EOL> status = httplib . PARTIAL_CONTENT <EOL> else : <EOL> print "<STR_LIT>" % ( range_line ) <EOL> return reply ( status , start_response , payload_out , headers ) <EOL> def get_mime_type ( ext ) : <EOL> "<STR_LIT>" <EOL> if ext == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> elif ext == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> elif ext == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> return "<STR_LIT>" <EOL> def handle_byte_range ( payload , range_line ) : <EOL> """<STR_LIT>""" <EOL> range_parts = range_line . split ( "<STR_LIT:=>" ) [ - <NUM_LIT:1> ] <EOL> ranges = range_parts . split ( "<STR_LIT:U+002C>" ) <EOL> if len ( ranges ) > <NUM_LIT:1> : <EOL> return ( payload , None ) <EOL> length = len ( payload ) <EOL> range_interval = ranges [ <NUM_LIT:0> ] <EOL> range_start , range_end = range_interval . split ( "<STR_LIT:->" ) <EOL> bad_range = False <EOL> if range_start == "<STR_LIT>" and range_end != "<STR_LIT>" : <EOL> range_start = length - int ( range_end ) <EOL> range_end = length - <NUM_LIT:1> <EOL> elif range_start != "<STR_LIT>" : <EOL> range_start = int ( range_start ) <EOL> if range_end != "<STR_LIT>" : <EOL> range_end = min ( int ( range_end ) , length - <NUM_LIT:1> ) <EOL> else : <EOL> range_end = length - <NUM_LIT:1> <EOL> else : <EOL> bad_range = True <EOL> if range_end < range_start : <EOL> bad_range = True <EOL> if bad_range : <EOL> return ( payload , "<STR_LIT>" ) <EOL> ranged_payload = payload [ range_start : range_end + <NUM_LIT:1> ] <EOL> range_response = "<STR_LIT>" % ( range_start , range_end , len ( payload ) ) <EOL> return ( ranged_payload , range_response ) <EOL> def main ( ) : <EOL> "<STR_LIT>" <EOL> from argparse import ArgumentParser <EOL> parser = ArgumentParser ( ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = str , <EOL> help = "<STR_LIT>" , required = True ) <EOL> parser . add_argument ( "<STR_LIT:-c>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = str , <EOL> help = "<STR_LIT>" , required = True ) <EOL> parser . add_argument ( "<STR_LIT>" , dest = "<STR_LIT:host>" , type = str , help = "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , dest = "<STR_LIT:port>" , type = int , help = "<STR_LIT>" , default = <NUM_LIT> ) <EOL> args = parser . parse_args ( ) <EOL> def application_wrapper ( env , resp ) : <EOL> "<STR_LIT>" <EOL> env [ '<STR_LIT>' ] = env [ '<STR_LIT>' ] <EOL> env [ '<STR_LIT>' ] = args . vod_conf_dir <EOL> env [ '<STR_LIT>' ] = args . content_dir <EOL> return application ( env , resp ) <EOL> def run_local_webserver ( wrapper , host , port ) : <EOL> "<STR_LIT>" <EOL> from wsgiref . simple_server import make_server <EOL> print '<STR_LIT>' . format ( host , port ) <EOL> httpd = make_server ( host , port , wrapper ) <EOL> httpd . serve_forever ( ) <EOL> run_local_webserver ( application_wrapper , args . host , args . port ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
