[albatross-users] Apache 2 Filters

Michael C. Neel neel at mediapulse.com
Fri Feb 20 02:50:28 EST 2004


Apache 2 adds in the concept of filters, in which you can alter content
from a handler before it is sent to the client.  An example would be
taking the output of a perl cgi script and formatting it with albatross.

To make this work, I've written a quick apacheapp Filter class, which
works just like a request class.  Filters are limited in what they can
do, so there is a lot of pass statements.  In pratice, this would work
just like a Request class, ie. app.run(Filter(filter)) - filters are not
passed the request object, but a filter object.

There should be anything in here to change, but it is a first cut, so
apply with caution.  It is intended to be placed in the apacheapp.py
file with the Request class for mod_python.

Mike

class FilterRequest(RequestFields):

    def __init__(self, filter):
        self.__req = filter.req
        self.__filter = filter
        RequestFields.__init__(self, util.FieldStorage(self.__req))
        self.__status = 200

    def get_uri(self):
        return self.__req.uri

    def get_path_info(self):
        return self.__req.path_info

    def get_servername(self):
        try:
            return self.__req.hostname                        #
mod_python 3
        except AttributeError:
            return self.__req.connection.server.server_hostname #
mod_python 2

    def get_header(self, name):
        if self.__req.headers_in.has_key(name):
            return self.__req.headers_in[name]

    def write_header(self, name, value):
        pass

    def end_headers(self):
        pass

    def redirect(self, loc):
        pass

    def write_content(self, data):
        self.__filter.write(data)

    def set_status(self, status):
        self.__status = status

    def status(self):
        if self.__status == 200:
            return 0
        return self.__status



More information about the Albatross-users mailing list