# -*- coding: iso8859-1 -*-## Copyright (C) 2004-2005 Edgewall Software# Copyright (C) 2004-2005 Christopher Lenz <cmlenz@gmx.de># Copyright (C) 2005 Matthew Good <trac@matt-good.net># All rights reserved.## This software is licensed as described in the file COPYING, which# you should have received as part of this distribution. The terms# are also available at http://trac.edgewall.com/license.html.## This software consists of voluntary contributions made by many# individuals. For the exact contribution history, see the revision# history and logs, available at http://projects.edgewall.com/trac/.## Author: Christopher Lenz <cmlenz@gmx.de># Matthew Good <trac@matt-good.net>import localeimport osfrom mod_python import apachefrom trac.web.main import dispatch_requestfrom trac.web.wsgi import WSGIGateway, _ErrorsWrapperclass InputWrapper(object): def __init__(self, req): self.req = req def close(self): pass def read(self, size=-1): return self.req.read(size) def readline(self): return self.req.readline() def readlines(self, hint=-1): return self.req.readlines(hint)class ModPythonGateway(WSGIGateway): wsgi_multithread = apache.mpm_query(apache.AP_MPMQ_IS_THREADED) > 0 wsgi_multiprocess = apache.mpm_query(apache.AP_MPMQ_IS_FORKED) > 0 def __init__(self, req, options): environ = {} environ.update(apache.build_cgi_env(req)) environ['mod_python.options'] = options environ['mod_python.subprocess_env'] = req.subprocess_env WSGIGateway.__init__(self, environ, InputWrapper(req), _ErrorsWrapper(lambda x: req.log_error(x))) self.req = req def _sendfile(self, fileobj): self.req.sendfile(fileobj.name) def _write(self, data): assert self.headers_set, 'Response not started' if not self.headers_sent: status, headers = self.headers_sent = self.headers_set self.req.status = int(status[:3]) for name, value in headers: if name.lower() == 'content-length': self.req.set_content_length(int(value)) elif name.lower() == 'content-type': self.req.content_type = value else: self.req.headers_out[name] = value try: self.req.write(data) except IOError, e: if 'client closed connection' not in e.strerror: raisedef app(wsgi_env, start_response): options = wsgi_env['mod_python.options'] locale.setlocale(locale.LC_ALL, options.get('TracLocale', '')) # Allow specifying the python eggs cache directory using SetEnv egg_cache = wsgi_env['mod_python.subprocess_env'].get('PYTHON_EGG_CACHE') if egg_cache: os.environ['PYTHON_EGG_CACHE'] = egg_cache wsgi_env['trac.env_path'] = options.get('TracEnv') wsgi_env['trac.env_parent_dir'] = options.get('TracEnvParentDir') wsgi_env['trac.env_index_template'] = options.get('TracEnvIndexTemplate') wsgi_env['trac.template_vars'] = options.get('TracTemplateVars') return dispatch_request(wsgi_env, start_response)def handler(req): options = req.get_options() gateway = ModPythonGateway(req, options) gateway.run(app) return apache.OK