|
1 |
| -import os |
2 |
| -from django.utils import httpwrappers |
3 |
| - |
4 |
| -# NOTE: do *not* import settings (or any module which eventually imports |
5 |
| -# settings) until after ModPythonHandler has been called; otherwise os.environ |
6 |
| -# won't be set up correctly (with respect to settings). |
7 |
| - |
8 |
| -class ModPythonHandler: |
9 |
| - |
10 |
| - def __init__(self): |
11 |
| - self._request_middleware = self._view_middleware = self._response_middleware = None |
12 |
| - |
13 |
| - def __call__(self, req): |
14 |
| - # mod_python fakes the environ, and thus doesn't process SetEnv. This fixes that |
15 |
| - os.environ.update(req.subprocess_env) |
16 |
| - |
17 |
| - # now that the environ works we can see the correct settings, so imports |
18 |
| - # that use settings now can work |
19 |
| - from django.conf import settings |
20 |
| - from django.core import db |
21 |
| - from django.utils.httpwrappers import ModPythonRequest |
22 |
| - |
23 |
| - # if we need to set up middleware, now that settings works we can do it now. |
24 |
| - if self._request_middleware is None: |
25 |
| - self.load_middleware() |
26 |
| - |
27 |
| - try: |
28 |
| - request = ModPythonRequest(req) |
29 |
| - response = self.get_response(req.uri, request) |
30 |
| - finally: |
31 |
| - db.db.close() |
32 |
| - |
33 |
| - # Apply response middleware |
34 |
| - for middleware_method in self._response_middleware: |
35 |
| - response = middleware_method(request, response) |
36 |
| - |
37 |
| - # Convert our custom HttpResponse object back into the mod_python req. |
38 |
| - httpwrappers.populate_apache_request(response, req) |
39 |
| - return 0 # mod_python.apache.OK |
40 |
| - |
41 |
| - def load_middleware(self): |
42 |
| - """ |
43 |
| - Populate middleware lists from settings.MIDDLEWARE_CLASSES. |
44 |
| -
|
45 |
| - Must be called after the environment is fixed (see __call__). |
46 |
| - """ |
47 |
| - from django.conf import settings |
48 |
| - from django.core import exceptions |
49 |
| - self._request_middleware = [] |
50 |
| - self._view_middleware = [] |
51 |
| - self._response_middleware = [] |
52 |
| - for middleware_path in settings.MIDDLEWARE_CLASSES: |
53 |
| - dot = middleware_path.rindex('.') |
54 |
| - mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:] |
55 |
| - try: |
56 |
| - mod = __import__(mw_module, '', '', ['']) |
57 |
| - except ImportError, e: |
58 |
| - raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e) |
59 |
| - try: |
60 |
| - mw_class = getattr(mod, mw_classname) |
61 |
| - except AttributeError: |
62 |
| - raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname) |
63 |
| - |
64 |
| - try: |
65 |
| - mw_instance = mw_class() |
66 |
| - except exceptions.MiddlewareNotUsed: |
67 |
| - continue |
68 |
| - |
69 |
| - if hasattr(mw_instance, 'process_request'): |
70 |
| - self._request_middleware.append(mw_instance.process_request) |
71 |
| - if hasattr(mw_instance, 'process_view'): |
72 |
| - self._view_middleware.append(mw_instance.process_view) |
73 |
| - if hasattr(mw_instance, 'process_response'): |
74 |
| - self._response_middleware.insert(0, mw_instance.process_response) |
75 |
| - |
76 |
| - def get_response(self, path, request): |
77 |
| - "Returns an HttpResponse object for the given HttpRequest" |
78 |
| - from django.core import db, exceptions, urlresolvers |
79 |
| - from django.core.mail import mail_admins |
80 |
| - from django.conf.settings import DEBUG, INTERNAL_IPS, ROOT_URLCONF |
81 |
| - |
82 |
| - # Apply request middleware |
83 |
| - for middleware_method in self._request_middleware: |
84 |
| - response = middleware_method(request) |
85 |
| - if response: |
86 |
| - return response |
87 |
| - |
88 |
| - conf_module = __import__(ROOT_URLCONF, '', '', ['']) |
89 |
| - resolver = urlresolvers.RegexURLResolver(conf_module.urlpatterns) |
90 |
| - try: |
91 |
| - callback, param_dict = resolver.resolve(path) |
92 |
| - # Apply view middleware |
93 |
| - for middleware_method in self._view_middleware: |
94 |
| - response = middleware_method(request, callback, param_dict) |
95 |
| - if response: |
96 |
| - return response |
97 |
| - return callback(request, **param_dict) |
98 |
| - except exceptions.Http404: |
99 |
| - if DEBUG: |
100 |
| - return self.get_technical_error_response(is404=True) |
101 |
| - else: |
102 |
| - resolver = urlresolvers.Error404Resolver(conf_module.handler404) |
103 |
| - callback, param_dict = resolver.resolve() |
104 |
| - return callback(request, **param_dict) |
105 |
| - except db.DatabaseError: |
106 |
| - db.db.rollback() |
107 |
| - if DEBUG: |
108 |
| - return self.get_technical_error_response() |
109 |
| - else: |
110 |
| - subject = 'Database error (%s IP)' % (request.META['REMOTE_ADDR'] in INTERNAL_IPS and 'internal' or 'EXTERNAL') |
111 |
| - message = "%s\n\n%s" % (self._get_traceback(), request) |
112 |
| - mail_admins(subject, message, fail_silently=True) |
113 |
| - return self.get_friendly_error_response(request, conf_module) |
114 |
| - except exceptions.PermissionDenied: |
115 |
| - return httpwrappers.HttpResponseForbidden('<h1>Permission denied</h1>') |
116 |
| - except: # Handle everything else, including SuspiciousOperation, etc. |
117 |
| - if DEBUG: |
118 |
| - return self.get_technical_error_response() |
119 |
| - else: |
120 |
| - subject = 'Coding error (%s IP)' % (request.META['REMOTE_ADDR'] in INTERNAL_IPS and 'internal' or 'EXTERNAL') |
121 |
| - message = "%s\n\n%s" % (self._get_traceback(), request) |
122 |
| - mail_admins(subject, message, fail_silently=True) |
123 |
| - return self.get_friendly_error_response(request, conf_module) |
124 |
| - |
125 |
| - def get_friendly_error_response(self, request, conf_module): |
126 |
| - """ |
127 |
| - Returns an HttpResponse that displays a PUBLIC error message for a |
128 |
| - fundamental database or coding error. |
129 |
| - """ |
130 |
| - from django.core import urlresolvers |
131 |
| - resolver = urlresolvers.Error404Resolver(conf_module.handler500) |
132 |
| - callback, param_dict = resolver.resolve() |
133 |
| - return callback(request, **param_dict) |
134 |
| - |
135 |
| - def get_technical_error_response(self, is404=False): |
136 |
| - """ |
137 |
| - Returns an HttpResponse that displays a TECHNICAL error message for a |
138 |
| - fundamental database or coding error. |
139 |
| - """ |
140 |
| - error_string = "<pre>There's been an error:\n\n%s</pre>" % self._get_traceback() |
141 |
| - responseClass = is404 and httpwrappers.HttpResponseNotFound or httpwrappers.HttpResponseServerError |
142 |
| - return responseClass(error_string) |
143 |
| - |
144 |
| - def _get_traceback(self): |
145 |
| - "Helper function to return the traceback as a string" |
146 |
| - import sys, traceback |
147 |
| - return '\n'.join(traceback.format_exception(*sys.exc_info())) |
| 1 | +# This module is DEPRECATED! |
| 2 | +# |
| 3 | +# You should no longer be pointing your mod_python configuration |
| 4 | +# at "django.core.handler". |
| 5 | +# |
| 6 | +# Use "django.core.handlers.modpython" instead. |
| 7 | + |
| 8 | +from django.core.handlers.modpython import ModPythonHandler |
148 | 9 |
|
149 | 10 | def handler(req):
|
150 | 11 | return ModPythonHandler()(req)
|
0 commit comments