ApacheHandler.py :  » Template-Engines » Myghty » Myghty-1.1 » lib » myghty » http » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Template Engines » Myghty 
Myghty » Myghty 1.1 » lib » myghty » http » ApacheHandler.py
# $Id: ApacheHandler.py 2028 2006-01-16 23:39:04Z zzzeek $
# ApacheHandler.py - handles apache requests for Myghty
# Copyright (C) 2004, 2005 Michael Bayer mike_mp@zzzcomputing.com
#
# This module is part of Myghty and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
#
#

from mod_python import apache
from mod_python import util
import myghty.exception as exception
import myghty.buffer
import re,string, types, time
from myghty.util import *
from myghty.resolver import *
import myghty.http.HTTPHandler as HTTPHandler

def handle(httpreq, interpreter_name = None, **params):
    return get_handler(httpreq, interpreter_name, **params).handle(httpreq = httpreq, **params)

def get_handler(httpreq, interpreter_name = None, **params):
    if interpreter_name is None:
        try:
            interpreter_name = httpreq.get_options()['MyghtyInterpreterName']
        except KeyError:
            pass
    return HTTPHandler.get_handler(ApacheHandler, httpreq = httpreq, interpreter_name = interpreter_name, **params)

class ApacheHandler(HTTPHandler.HTTPHandler):
    def __init__(self, httpreq, **params):
        HTTPHandler.HTTPHandler.__init__(self, httpreq, LogBuffer(httpreq.server), **params)

    def do_handle_result(self, httpreq, status_code, reason):
        httpreq.status = HTTPHandler.HTTP_OK
        if status_code == HTTPHandler.HTTP_OK:
            return apache.OK
        else:
            return status_code

    def do_get_init_params(self, httpreq, **params):
        table = httpreq.get_options()
        
        options = params.copy()
        for key in table.keys():
            match = re.match("^Myghty(.*)$", key)
            if not match: continue
            param = match.group(1)
            param = string.lower(re.sub(r"(\w)([A-Z])", r"\1_\2", param))
            if not options.has_key(param):
                options[param] = eval(table[key])
            
        return options
    
    def do_make_request_impl(self, httpreq, **params):
        httpreq.add_common_vars()
        return ApacheRequestImpl(httpreq, **params)

    def do_get_resolver(self, **params):
        return ApacheResolver(**params)
    
    def do_get_component(self, httpreq, **params):
        return httpreq.uri
        
            


class LogBuffer(object):
    def __init__(self, server, retcode = apache.APLOG_NOTICE):
        self.server = server
        self.retcode = retcode

    def write(self, s):
        apache.log_error(string.rstrip(s), self.retcode, self.server)

    def writelines(self, list):
        for line in list:
            apache.log_error(string.rstrip(line), self.retcode, self.server)

    def flush(self):
        pass

class ApacheBuffer(HTTPHandler.HTTPWriter):

    def send_headers(self):
        if self.headers_sent:
            return

        self.headers_sent = True

        if not self.httpreq._content_type_set:
            self.httpreq.content_type = 'text/html'

        # this method is only documented in mod py 2.7
        # (where its needed) but seems harmless though useless
        # to use in mod py 3.0 as well
        self.httpreq.send_http_header()


    def writelines(self, list):
        for line in list:
            self.write(line + "\n")

        def flush(self):pass

class ApacheResolver(HTTPHandler.HTTPResolver):pass

class ApacheRequestImpl(HTTPHandler.HTTPRequestImpl):
    def __init__(self, httpreq, use_modpython_session = False, **params):

        self.use_modpython_session = use_modpython_session

        HTTPHandler.HTTPRequestImpl.__init__(self, httpreq, LogBuffer(httpreq.server),
            LogBuffer(httpreq.server, apache.APLOG_ERR), **params)
        
        
    def get_session(self, **params):
        if not hasattr(self, 'session'):
            if self.use_modpython_session:
                import mod_python.Session
                self.session = mod_python.Session.Session(timeout = self.session_args.timeout)
            else:
                self.session = self.session_args.get_session(self.httpreq, **params)
            
        return self.session

    def do_get_out_buffer(self, httpreq, out_buffer = None, **params):
        if out_buffer is None:
            out_buffer = httpreq
            
        return ApacheBuffer(httpreq, out_buffer = out_buffer, **params)

    def do_make_request_args(self, httpreq, **params):
        """given a mod_python request, returns a real dict of key values, where the values are
        one of:  a string, a list of strings, a Field object with an upload value, a list of
        Field objects with upload values."""

        def formatfield(field):
            if type(field) == types.ListType:
                return map(formatfield, field)
            elif isinstance(field, mputil.Field):
                return field
            else:
                return str(field)

        request_args = {}
        fields = mputil.FieldStorage(httpreq, keep_blank_values = True)
        for key in fields.keys():
            request_args[key] = formatfield(fields[key])
        return request_args
        



www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.