on_request: allow hook to return a reponse

Closes #6130
This commit is contained in:
Jérôme Schneider 2014-12-03 16:49:23 +01:00
parent 9c0d7d498c
commit 9bc2c9eb4b
3 changed files with 15 additions and 14 deletions

View File

@ -6,6 +6,7 @@ from urlparse import urlparse
from importlib import import_module
from mandaye import config
from mandaye.http import HTTPRequest, HTTPResponse
from mandaye.log import logger
from mandaye.mappers import default
from mandaye.response import _500, _302
@ -37,8 +38,8 @@ class Dispatcher(object):
target_url: the full url of your destination
mepper: python module with the mapper
"""
self.target = urlparse(target_url)
self.env = env
self.target = urlparse(target_url)
self.env['target'] = self.target
self.mapper_name = mapper_name
self.mapper = import_mapping(mapper_name)
@ -164,13 +165,16 @@ class Dispatcher(object):
for hook in self.req_mapping['on_request']:
if not self._is_cond_respected(hook, request, None):
continue
new_request = self._call_hook(hook, request)
if new_request:
request = new_request
else:
logger.warning("%s On_request hook %s failed (empty request)" % \
(self.env['PATH_INFO'], hook['filter']))
return request
response = self._call_hook(hook, request)
if response and isinstance(response, HTTPResponse):
return response
elif response and isinstance(response, HTTPRequest):
logger.warning('DEPRECATED hook %s: a request hook must return None or an HTTPResponse nothing else',
hook['filter'])
elif response:
logger.warning("%s on_request hook (%s) must return None or an HTTPResponse nothing else",
self.env['PATH_INFO'], hook['filter'])
return
def mod_response(self, request, response):
""" Modify the response. This will load on_response filters.

View File

@ -25,7 +25,6 @@ class MandayeFilter(object):
# Add forwarded header like Apache
request.headers.addheader('X-Forwarded-For', env['REMOTE_ADDR'])
request.headers.addheader('X-Forwarded-Host', env['HTTP_HOST'])
return request
@staticmethod
def on_response(env, values, request, response):

View File

@ -120,7 +120,6 @@ class MandayeApp(object):
(conf_file, conf['mapper'])
logger.error(err)
raise ImproperlyConfigured, err
self.env['mandaye.auth_type'] = conf['mapper']
if not config.authentifications.has_key(conf['auth_type']):
err = '%s: authentification %s not found' % \
(conf_file, conf['auth_type'])
@ -198,10 +197,9 @@ class MandayeApp(object):
def on_request(self, start_response):
request = self._get_request()
# Calling the dispatcher hook for the request
request = self.dispatcher.mod_request(request)
if not request:
return self.on_response(start_response,
_500(self.env["PATH_INFO"], "Empty request"))
response = self.dispatcher.mod_request(request)
if response:
return response
if not request.target:
response = self.dispatcher.get_response(request)
elif not "://" in request.target: