Contact: fumanchu@aminus.org

Log in as guest/misc to create tickets

Changeset 20

Show
Ignore:
Timestamp:
12/05/05 18:37:58
Author:
fumanchu
Message:

Yeat Another Status Fix for modpython_gateway.py. This finally corrects both the .css/.js problem, and incorrect status on redirects, 404s, etc.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • modpython_gateway.py

    r19 r20  
     1""" 
     2WSGI wrapper for mod_python. Requires Python 2.2 or greater. 
     3 
     4 
     5Example httpd.conf section for a CherryPy app called "mcontrol": 
     6 
     7<Directory D:\htdocs\mcontrol> 
     8    SetHandler python-program 
     9    PythonHandler wsgiref.modpy_wrapper::handler 
     10    PythonOption application cherrypy.wsgiapp::wsgiApp 
     11    PythonOption import mcontrol.cherry::startup 
     12</Directory> 
     13 
     14""" 
    115 
    216import sys 
     
    620 
    721class InputWrapper(object): 
    8  
     22     
    923    def __init__(self, req): 
    1024        self.req = req 
    11  
     25     
    1226    def close(self): 
    1327        pass 
    14  
     28     
    1529    def read(self, size=-1): 
    1630        return self.req.read(size) 
    17  
     31     
    1832    def readline(self): 
    1933        return self.req.readline() 
    20  
     34     
    2135    def readlines(self, hint=-1): 
    2236        return self.req.readlines(hint) 
    23  
     37     
    2438    def __iter__(self): 
    2539        line = self.readline() 
     
    3246 
    3347class ErrorWrapper(object): 
    34  
     48     
    3549    def __init__(self, req): 
    3650        self.req = req 
    37  
     51     
    3852    def flush(self): 
    3953        pass 
    40  
     54     
    4155    def write(self, msg): 
    4256        self.req.log_error(msg) 
    43  
     57     
    4458    def writelines(self, seq): 
    4559        self.write(''.join(seq)) 
     
    4761 
    4862bad_value = ("You must provide a PythonOption '%s', either 'on' or 'off', " 
    49              "when running a version of mod_python &lt; 3.1") 
     63             "when running a version of mod_python < 3.1") 
    5064 
    5165class Handler(BaseCGIHandler): 
    52  
     66     
    5367    def __init__(self, req): 
    5468        options = req.get_options() 
    55  
     69         
    5670        # Threading and forking 
    5771        try: 
     
    6579            else: 
    6680                raise ValueError(bad_value % "multithread") 
    67  
     81             
    6882            forked = options.get('multiprocess', '').lower() 
    6983            if forked == 'on': 
     
    7690            threaded = q(apache.AP_MPMQ_IS_THREADED) 
    7791            forked = q(apache.AP_MPMQ_IS_FORKED) 
    78  
     92         
    7993        env = dict(apache.build_cgi_env(req)) 
    80  
     94         
    8195        if req.headers_in.has_key("authorization"): 
    8296            env["HTTP_AUTHORIZATION"] = req.headers_in["authorization"] 
    83  
     97         
    8498        BaseCGIHandler.__init__(self, 
    8599                                stdin=InputWrapper(req), 
     
    92106        self.request = req 
    93107        self._write = req.write 
    94  
     108     
    95109    def _flush(self): 
    96110        pass 
    97  
     111     
    98112    def send_headers(self): 
    99113        self.cleanup_headers() 
    100114        self.headers_sent = True 
    101115        # Can't just return 200 or the page will hang until timeout 
    102         s = int(self.status[:3]) 
    103         if s == 200: 
    104             self.finalstatus = apache.OK 
    105         else: 
    106             self.finalstatus = s 
     116        self.request.status = int(self.status[:3]) 
    107117        # the headers.Headers class doesn't have an iteritems method... 
    108118        for key, val in self.headers.items(): 
    109119            if key.lower() == 'content-length': 
    110                 self.request.set_content_length(int(val)) 
     120                if val is not None: 
     121                    self.request.set_content_length(int(val)) 
    111122            elif key.lower() == 'content-type': 
    112123                self.request.content_type = val 
    113             self.request.headers_out[key] = val 
     124            else: 
     125                self.request.headers_out[key] = val 
    114126 
    115127 
     
    134146    config = req.get_config() 
    135147    debug = int(config.get("PythonDebug", 0)) 
    136  
     148     
    137149    options = req.get_options() 
    138150     
     
    144156    # be called per request, must be designed to run setup code only on the 
    145157    # first request (a global 'first_request' flag is usually enough). 
    146     atoms = options['import'].split('::', 1) 
    147     modname = atoms.pop(0) 
    148     module = __import__(modname, globals(), locals(), ['']) 
    149     if atoms: 
    150         func = getattr(module, atoms[0]) 
    151         func(req) 
     158    import_opt = options.get('import') 
     159    if import_opt: 
     160        atoms = import_opt.split('::', 1) 
     161        modname = atoms.pop(0) 
     162        module = __import__(modname, globals(), locals(), ['']) 
     163        if atoms: 
     164            func = getattr(module, atoms[0]) 
     165            func(req) 
    152166     
    153167    # Import the wsgi 'application' callable and pass it to Handler.run 
     
    155169    module = __import__(modname, globals(), locals(), ['']) 
    156170    app = getattr(module, objname) 
    157  
     171     
     172##    modname, objname = options['application'].split('::') 
     173##    module = apache.import_module(modname, autoreload=False, log=debug) 
     174##    app = apache.resolve_object(module, objname, arg=None, silent=False) 
     175     
     176    # You can un-comment the next line to get the value of req.interpreter. 
     177    # If you use a PythonImport directive as recommended, its interpreter_name 
     178    # value must EXACTLY match the value of req.interpreter (case-sensitive). 
     179##  raise StandardError(repr(req.interpreter)) 
     180     
    158181    h = Handler(req) 
    159182##    from paste import lint 
    160183##    app = lint.middleware(app) 
    161184    h.run(app) 
     185     
     186    # status was set in Handler.send_headers(); always return apache.OK 
     187    return apache.OK 
    162188 
    163     # finalstatus was set in Handler.send_headers() 
    164     return h.finalstatus 
    165