<s> __all__ = [ "<STR_LIT>" ] <EOL> import sys , os <EOL> from SimpleHTTPServer import SimpleHTTPRequestHandler <EOL> import webapi as web <EOL> import net <EOL> import utils <EOL> def runbasic ( func , server_address = ( "<STR_LIT>" , <NUM_LIT> ) ) : <EOL> """<STR_LIT>""" <EOL> import SimpleHTTPServer , SocketServer , BaseHTTPServer , urlparse <EOL> import socket , errno <EOL> import traceback <EOL> class WSGIHandler ( SimpleHTTPServer . SimpleHTTPRequestHandler ) : <EOL> def run_wsgi_app ( self ) : <EOL> protocol , host , path , parameters , query , fragment = urlparse . urlparse ( '<STR_LIT>' % self . path ) <EOL> env = { '<STR_LIT>' : ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> , '<STR_LIT>' : '<STR_LIT:http>' <EOL> , '<STR_LIT>' : self . rfile <EOL> , '<STR_LIT>' : sys . stderr <EOL> , '<STR_LIT>' : <NUM_LIT:1> <EOL> , '<STR_LIT>' : <NUM_LIT:0> <EOL> , '<STR_LIT>' : <NUM_LIT:0> <EOL> , '<STR_LIT>' : self . command <EOL> , '<STR_LIT>' : self . path <EOL> , '<STR_LIT>' : path <EOL> , '<STR_LIT>' : query <EOL> , '<STR_LIT>' : self . headers . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> , '<STR_LIT>' : self . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> , '<STR_LIT>' : self . client_address [ <NUM_LIT:0> ] <EOL> , '<STR_LIT>' : self . server . server_address [ <NUM_LIT:0> ] <EOL> , '<STR_LIT>' : str ( self . server . server_address [ <NUM_LIT:1> ] ) <EOL> , '<STR_LIT>' : self . request_version <EOL> } <EOL> for http_header , http_value in self . headers . items ( ) : <EOL> env [ '<STR_LIT>' % http_header . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) . upper ( ) ] = http_value <EOL> self . wsgi_sent_headers = <NUM_LIT:0> <EOL> self . wsgi_headers = [ ] <EOL> try : <EOL> result = self . server . app ( env , self . wsgi_start_response ) <EOL> try : <EOL> try : <EOL> for data in result : <EOL> if data : <EOL> self . wsgi_write_data ( data ) <EOL> finally : <EOL> if hasattr ( result , '<STR_LIT>' ) : <EOL> result . close ( ) <EOL> except socket . error , socket_err : <EOL> if ( socket_err . args [ <NUM_LIT:0> ] in ( errno . ECONNABORTED , errno . EPIPE ) ) : <EOL> return <EOL> except socket . timeout , socket_timeout : <EOL> return <EOL> except : <EOL> print >> web . debug , traceback . format_exc ( ) , <EOL> if ( not self . wsgi_sent_headers ) : <EOL> self . wsgi_write_data ( "<STR_LIT:U+0020>" ) <EOL> return <EOL> do_POST = run_wsgi_app <EOL> do_PUT = run_wsgi_app <EOL> do_DELETE = run_wsgi_app <EOL> def do_GET ( self ) : <EOL> if self . path . startswith ( '<STR_LIT>' ) : <EOL> SimpleHTTPServer . SimpleHTTPRequestHandler . do_GET ( self ) <EOL> else : <EOL> self . run_wsgi_app ( ) <EOL> def wsgi_start_response ( self , response_status , response_headers , <EOL> exc_info = None ) : <EOL> if ( self . wsgi_sent_headers ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . wsgi_headers = ( response_status , response_headers ) <EOL> return self . wsgi_write_data <EOL> def wsgi_write_data ( self , data ) : <EOL> if ( not self . wsgi_sent_headers ) : <EOL> status , headers = self . wsgi_headers <EOL> status_code = status [ : status . find ( '<STR_LIT:U+0020>' ) ] <EOL> status_msg = status [ status . find ( '<STR_LIT:U+0020>' ) + <NUM_LIT:1> : ] <EOL> self . send_response ( int ( status_code ) , status_msg ) <EOL> for header , value in headers : <EOL> self . send_header ( header , value ) <EOL> self . end_headers ( ) <EOL> self . wsgi_sent_headers = <NUM_LIT:1> <EOL> self . wfile . write ( data ) <EOL> class WSGIServer ( SocketServer . ThreadingMixIn , BaseHTTPServer . HTTPServer ) : <EOL> def __init__ ( self , func , server_address ) : <EOL> BaseHTTPServer . HTTPServer . __init__ ( self , <EOL> server_address , <EOL> WSGIHandler ) <EOL> self . app = func <EOL> self . serverShuttingDown = <NUM_LIT:0> <EOL> print "<STR_LIT>" % server_address <EOL> WSGIServer ( func , server_address ) . serve_forever ( ) <EOL> def runsimple ( func , server_address = ( "<STR_LIT>" , <NUM_LIT> ) ) : <EOL> """<STR_LIT>""" <EOL> func = StaticMiddleware ( func ) <EOL> func = LogMiddleware ( func ) <EOL> server = WSGIServer ( server_address , func ) <EOL> print "<STR_LIT>" % server_address <EOL> try : <EOL> server . start ( ) <EOL> except KeyboardInterrupt : <EOL> server . stop ( ) <EOL> def WSGIServer ( server_address , wsgi_app ) : <EOL> """<STR_LIT>""" <EOL> from wsgiserver import CherryPyWSGIServer <EOL> return CherryPyWSGIServer ( server_address , wsgi_app , server_name = "<STR_LIT:localhost>" ) <EOL> class StaticApp ( SimpleHTTPRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , environ , start_response ) : <EOL> self . headers = [ ] <EOL> self . environ = environ <EOL> self . start_response = start_response <EOL> def send_response ( self , status , msg = "<STR_LIT>" ) : <EOL> self . status = str ( status ) + "<STR_LIT:U+0020>" + msg <EOL> def send_header ( self , name , value ) : <EOL> self . headers . append ( ( name , value ) ) <EOL> def end_headers ( self ) : <EOL> pass <EOL> def log_message ( * a ) : pass <EOL> def __iter__ ( self ) : <EOL> environ = self . environ <EOL> self . path = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . client_address = environ . get ( '<STR_LIT>' , '<STR_LIT:->' ) , environ . get ( '<STR_LIT>' , '<STR_LIT:->' ) <EOL> self . command = environ . get ( '<STR_LIT>' , '<STR_LIT:->' ) <EOL> from cStringIO import StringIO <EOL> self . wfile = StringIO ( ) <EOL> try : <EOL> path = self . translate_path ( self . path ) <EOL> etag = '<STR_LIT>' % os . path . getmtime ( path ) <EOL> client_etag = environ . get ( '<STR_LIT>' ) <EOL> self . send_header ( '<STR_LIT>' , etag ) <EOL> if etag == client_etag : <EOL> self . send_response ( <NUM_LIT> , "<STR_LIT>" ) <EOL> self . start_response ( self . status , self . headers ) <EOL> raise StopIteration <EOL> except OSError : <EOL> pass <EOL> f = self . send_head ( ) <EOL> self . start_response ( self . status , self . headers ) <EOL> if f : <EOL> block_size = <NUM_LIT:16> * <NUM_LIT> <EOL> while True : <EOL> buf = f . read ( block_size ) <EOL> if not buf : <EOL> break <EOL> yield buf <EOL> f . close ( ) <EOL> else : <EOL> value = self . wfile . getvalue ( ) <EOL> yield value <EOL> class StaticMiddleware : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , prefix = '<STR_LIT>' ) : <EOL> self . app = app <EOL> self . prefix = prefix <EOL> def __call__ ( self , environ , start_response ) : <EOL> path = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if path . startswith ( self . prefix ) : <EOL> return StaticApp ( environ , start_response ) <EOL> else : <EOL> return self . app ( environ , start_response ) <EOL> class LogMiddleware : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> self . format = '<STR_LIT>' <EOL> from BaseHTTPServer import BaseHTTPRequestHandler <EOL> import StringIO <EOL> f = StringIO . StringIO ( ) <EOL> class FakeSocket : <EOL> def makefile ( self , * a ) : <EOL> return f <EOL> self . log_date_time_string = BaseHTTPRequestHandler ( FakeSocket ( ) , None , None ) . log_date_time_string <EOL> def __call__ ( self , environ , start_response ) : <EOL> def xstart_response ( status , response_headers , * args ) : <EOL> out = start_response ( status , response_headers , * args ) <EOL> self . log ( status , environ ) <EOL> return out <EOL> return self . app ( environ , xstart_response ) <EOL> def log ( self , status , environ ) : <EOL> outfile = environ . get ( '<STR_LIT>' , web . debug ) <EOL> req = environ . get ( '<STR_LIT>' , '<STR_LIT:_>' ) <EOL> protocol = environ . get ( '<STR_LIT>' , '<STR_LIT:->' ) <EOL> method = environ . get ( '<STR_LIT>' , '<STR_LIT:->' ) <EOL> host = "<STR_LIT>" % ( environ . get ( '<STR_LIT>' , '<STR_LIT:->' ) , <EOL> environ . get ( '<STR_LIT>' , '<STR_LIT:->' ) ) <EOL> time = self . log_date_time_string ( ) <EOL> msg = self . format % ( host , time , protocol , method , req , status ) <EOL> print >> outfile , utils . safestr ( msg ) </s>
