From a1669ce409c9b9e004732203d7331f4abe2dfc36 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Schneider?= <jschneider@entrouvert.com>
Date: Wed, 3 Dec 2014 16:49:23 +0100
Subject: [PATCH 1/5] on_request: allow hook to return a reponse

Closes #6130
---
 mandaye/dispatcher.py      | 20 ++++++++++++--------
 mandaye/filters/default.py |  1 -
 mandaye/server.py          |  8 +++-----
 3 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/mandaye/dispatcher.py b/mandaye/dispatcher.py
index 96d2b66..ed59ac8 100644
--- a/mandaye/dispatcher.py
+++ b/mandaye/dispatcher.py
@@ -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.
diff --git a/mandaye/filters/default.py b/mandaye/filters/default.py
index 4af55f4..3cfd74a 100644
--- a/mandaye/filters/default.py
+++ b/mandaye/filters/default.py
@@ -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):
diff --git a/mandaye/server.py b/mandaye/server.py
index 45f76b5..9025a27 100644
--- a/mandaye/server.py
+++ b/mandaye/server.py
@@ -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:
-- 
2.1.3

