<s> import os <EOL> import mimetypes <EOL> from . http import Request , Response <EOL> from . util import PappyStringTransport , PappyException <EOL> from twisted . test . proto_helpers import StringTransport <EOL> from twisted . web . server import Site , NOT_DONE_YET <EOL> from twisted . web import static <EOL> from twisted . web . resource import Resource , NoResource <EOL> from jinja2 import Environment , FileSystemLoader <EOL> from twisted . internet import defer <EOL> class PappyWebServer ( object ) : <EOL> """<STR_LIT>""" <EOL> from pappyproxy . pappy import session <EOL> site_dir = session . config . pappy_dir + '<STR_LIT>' <EOL> loader = FileSystemLoader ( site_dir ) <EOL> env = Environment ( loader = loader ) <EOL> def __init__ ( self ) : <EOL> root = RootResource ( self . site_dir ) <EOL> self . site = Site ( root ) <EOL> @ staticmethod <EOL> def render_template ( * args , ** kwargs ) : <EOL> return PappyWebServer . env . get_template ( args [ <NUM_LIT:0> ] ) . render ( args [ <NUM_LIT:1> : ] , ** kwargs ) . encode ( '<STR_LIT:utf-8>' ) <EOL> @ defer . inlineCallbacks <EOL> def handle_request ( self , req ) : <EOL> protocol = self . site . buildProtocol ( None ) <EOL> tr = PappyStringTransport ( ) <EOL> protocol . makeConnection ( tr ) <EOL> protocol . dataReceived ( req . full_request ) <EOL> tr . waitForProducers ( ) <EOL> yield tr . complete_deferred <EOL> rsp_raw = tr . value ( ) <EOL> rsp = Response ( rsp_raw ) <EOL> req . response = rsp <EOL> def blocking_string_request ( func ) : <EOL> """<STR_LIT>""" <EOL> def f ( self , request ) : <EOL> request . transport . complete_deferred . callback ( None ) <EOL> return func ( self , request ) <EOL> return f <EOL> class PappyResource ( Resource ) : <EOL> """<STR_LIT>""" <EOL> def getChild ( self , name , request ) : <EOL> if name == '<STR_LIT>' : <EOL> return self <EOL> return Resource . getChild ( self , name , request ) <EOL> class RootResource ( PappyResource ) : <EOL> def __init__ ( self , site_dir ) : <EOL> PappyResource . __init__ ( self ) <EOL> self . site_dir = site_dir <EOL> self . dirListing = False <EOL> self . static_resource = NoDirFile ( self . site_dir + '<STR_LIT>' ) <EOL> self . putChild ( '<STR_LIT>' , self . static_resource ) <EOL> self . putChild ( '<STR_LIT>' , CertResource ( ) ) <EOL> self . putChild ( '<STR_LIT>' , ResponseResource ( ) ) <EOL> @ blocking_string_request <EOL> def render_GET ( self , request ) : <EOL> return PappyWebServer . render_template ( '<STR_LIT>' ) <EOL> class NoDirFile ( static . File ) : <EOL> def directoryListing ( self ) : <EOL> return NoResource ( ) <EOL> @ blocking_string_request <EOL> def render_GET ( self , request ) : <EOL> return static . File . render_GET ( self , request ) <EOL> class CertResource ( PappyResource ) : <EOL> def __init__ ( self ) : <EOL> PappyResource . __init__ ( self ) <EOL> self . putChild ( '<STR_LIT>' , CertDownload ( ) ) <EOL> @ blocking_string_request <EOL> def render_GET ( self , request ) : <EOL> return PappyWebServer . render_template ( '<STR_LIT>' ) <EOL> class CertDownload ( PappyResource ) : <EOL> @ blocking_string_request <EOL> def render_GET ( self , request ) : <EOL> from . pappy import session <EOL> cert_dir = session . config . cert_dir <EOL> ssl_ca_file = session . config . ssl_ca_file <EOL> with open ( os . path . join ( cert_dir , ssl_ca_file ) , '<STR_LIT:r>' ) as f : <EOL> ca_raw = f . read ( ) <EOL> request . responseHeaders . addRawHeader ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> return ca_raw <EOL> class ResponseResource ( PappyResource ) : <EOL> def getChild ( self , name , request ) : <EOL> if name == '<STR_LIT>' : <EOL> return self <EOL> return ViewResponseResource ( name ) <EOL> @ blocking_string_request <EOL> def render_GET ( self , request ) : <EOL> return PappyWebServer . render_template ( '<STR_LIT>' ) <EOL> class ViewResponseResource ( PappyResource ) : <EOL> def __init__ ( self , reqid ) : <EOL> PappyResource . __init__ ( self ) <EOL> self . reqid = reqid <EOL> def render_GET ( self , request ) : <EOL> d = Request . load_request ( self . reqid ) <EOL> d . addCallback ( self . _render_response , request ) <EOL> d . addErrback ( self . _render_response_err , request ) <EOL> d . addCallback ( lambda _ : request . transport . complete_deferred . callback ( None ) ) <EOL> return NOT_DONE_YET <EOL> def _render_response ( self , req , tw_request ) : <EOL> if req . response : <EOL> if not req . response . body : <EOL> raise PappyException ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in req . response . headers : <EOL> tw_request . responseHeaders . addRawHeader ( "<STR_LIT:Content-Type>" , req . response . headers [ '<STR_LIT>' ] ) <EOL> else : <EOL> guess = mimetypes . guess_type ( req . url ) <EOL> if guess [ <NUM_LIT:0> ] : <EOL> tw_request . responseHeaders . addRawHeader ( "<STR_LIT:Content-Type>" , guess [ <NUM_LIT:0> ] ) <EOL> tw_request . write ( req . response . body ) <EOL> else : <EOL> tw_request . write ( PappyWebServer . render_template ( '<STR_LIT>' ) ) <EOL> tw_request . finish ( ) <EOL> def _render_response_err ( self , err , tw_request ) : <EOL> tw_request . write ( PappyWebServer . render_template ( '<STR_LIT>' , errmsg = err . getErrorMessage ( ) ) ) <EOL> tw_request . finish ( ) <EOL> err . trap ( Exception ) </s>
