|
1 import os |
|
2 from pprint import pformat |
|
3 |
|
4 from django import http |
|
5 from django.core import signals |
|
6 from django.core.handlers.base import BaseHandler |
|
7 from django.dispatch import dispatcher |
|
8 from django.utils import datastructures |
|
9 from django.utils.encoding import force_unicode, smart_str |
|
10 |
|
11 # NOTE: do *not* import settings (or any module which eventually imports |
|
12 # settings) until after ModPythonHandler has been called; otherwise os.environ |
|
13 # won't be set up correctly (with respect to settings). |
|
14 |
|
15 class ModPythonRequest(http.HttpRequest): |
|
16 def __init__(self, req): |
|
17 self._req = req |
|
18 self.path = force_unicode(req.uri) |
|
19 |
|
20 def __repr__(self): |
|
21 # Since this is called as part of error handling, we need to be very |
|
22 # robust against potentially malformed input. |
|
23 try: |
|
24 get = pformat(self.GET) |
|
25 except: |
|
26 get = '<could not parse>' |
|
27 try: |
|
28 post = pformat(self.POST) |
|
29 except: |
|
30 post = '<could not parse>' |
|
31 try: |
|
32 cookies = pformat(self.COOKIES) |
|
33 except: |
|
34 cookies = '<could not parse>' |
|
35 try: |
|
36 meta = pformat(self.META) |
|
37 except: |
|
38 meta = '<could not parse>' |
|
39 return smart_str(u'<ModPythonRequest\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % |
|
40 (self.path, unicode(get), unicode(post), |
|
41 unicode(cookies), unicode(meta))) |
|
42 |
|
43 def get_full_path(self): |
|
44 return '%s%s' % (self.path, self._req.args and ('?' + self._req.args) or '') |
|
45 |
|
46 def is_secure(self): |
|
47 try: |
|
48 return self._req.is_https() |
|
49 except AttributeError: |
|
50 # mod_python < 3.2.10 doesn't have req.is_https(). |
|
51 return self._req.subprocess_env.get('HTTPS', '').lower() in ('on', '1') |
|
52 |
|
53 def _load_post_and_files(self): |
|
54 "Populates self._post and self._files" |
|
55 if 'content-type' in self._req.headers_in and self._req.headers_in['content-type'].startswith('multipart'): |
|
56 self._post, self._files = http.parse_file_upload(self._req.headers_in, self.raw_post_data) |
|
57 else: |
|
58 self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict() |
|
59 |
|
60 def _get_request(self): |
|
61 if not hasattr(self, '_request'): |
|
62 self._request = datastructures.MergeDict(self.POST, self.GET) |
|
63 return self._request |
|
64 |
|
65 def _get_get(self): |
|
66 if not hasattr(self, '_get'): |
|
67 self._get = http.QueryDict(self._req.args, encoding=self._encoding) |
|
68 return self._get |
|
69 |
|
70 def _set_get(self, get): |
|
71 self._get = get |
|
72 |
|
73 def _get_post(self): |
|
74 if not hasattr(self, '_post'): |
|
75 self._load_post_and_files() |
|
76 return self._post |
|
77 |
|
78 def _set_post(self, post): |
|
79 self._post = post |
|
80 |
|
81 def _get_cookies(self): |
|
82 if not hasattr(self, '_cookies'): |
|
83 self._cookies = http.parse_cookie(self._req.headers_in.get('cookie', '')) |
|
84 return self._cookies |
|
85 |
|
86 def _set_cookies(self, cookies): |
|
87 self._cookies = cookies |
|
88 |
|
89 def _get_files(self): |
|
90 if not hasattr(self, '_files'): |
|
91 self._load_post_and_files() |
|
92 return self._files |
|
93 |
|
94 def _get_meta(self): |
|
95 "Lazy loader that returns self.META dictionary" |
|
96 if not hasattr(self, '_meta'): |
|
97 self._meta = { |
|
98 'AUTH_TYPE': self._req.ap_auth_type, |
|
99 'CONTENT_LENGTH': self._req.clength, # This may be wrong |
|
100 'CONTENT_TYPE': self._req.content_type, # This may be wrong |
|
101 'GATEWAY_INTERFACE': 'CGI/1.1', |
|
102 'PATH_INFO': self._req.path_info, |
|
103 'PATH_TRANSLATED': None, # Not supported |
|
104 'QUERY_STRING': self._req.args, |
|
105 'REMOTE_ADDR': self._req.connection.remote_ip, |
|
106 'REMOTE_HOST': None, # DNS lookups not supported |
|
107 'REMOTE_IDENT': self._req.connection.remote_logname, |
|
108 'REMOTE_USER': self._req.user, |
|
109 'REQUEST_METHOD': self._req.method, |
|
110 'SCRIPT_NAME': None, # Not supported |
|
111 'SERVER_NAME': self._req.server.server_hostname, |
|
112 'SERVER_PORT': self._req.server.port, |
|
113 'SERVER_PROTOCOL': self._req.protocol, |
|
114 'SERVER_SOFTWARE': 'mod_python' |
|
115 } |
|
116 for key, value in self._req.headers_in.items(): |
|
117 key = 'HTTP_' + key.upper().replace('-', '_') |
|
118 self._meta[key] = value |
|
119 return self._meta |
|
120 |
|
121 def _get_raw_post_data(self): |
|
122 try: |
|
123 return self._raw_post_data |
|
124 except AttributeError: |
|
125 self._raw_post_data = self._req.read() |
|
126 return self._raw_post_data |
|
127 |
|
128 def _get_method(self): |
|
129 return self.META['REQUEST_METHOD'].upper() |
|
130 |
|
131 GET = property(_get_get, _set_get) |
|
132 POST = property(_get_post, _set_post) |
|
133 COOKIES = property(_get_cookies, _set_cookies) |
|
134 FILES = property(_get_files) |
|
135 META = property(_get_meta) |
|
136 REQUEST = property(_get_request) |
|
137 raw_post_data = property(_get_raw_post_data) |
|
138 method = property(_get_method) |
|
139 |
|
140 class ModPythonHandler(BaseHandler): |
|
141 request_class = ModPythonRequest |
|
142 |
|
143 def __call__(self, req): |
|
144 # mod_python fakes the environ, and thus doesn't process SetEnv. This fixes that |
|
145 os.environ.update(req.subprocess_env) |
|
146 |
|
147 # now that the environ works we can see the correct settings, so imports |
|
148 # that use settings now can work |
|
149 from django.conf import settings |
|
150 |
|
151 # if we need to set up middleware, now that settings works we can do it now. |
|
152 if self._request_middleware is None: |
|
153 self.load_middleware() |
|
154 |
|
155 dispatcher.send(signal=signals.request_started) |
|
156 try: |
|
157 try: |
|
158 request = self.request_class(req) |
|
159 except UnicodeDecodeError: |
|
160 response = http.HttpResponseBadRequest() |
|
161 else: |
|
162 response = self.get_response(request) |
|
163 |
|
164 # Apply response middleware |
|
165 for middleware_method in self._response_middleware: |
|
166 response = middleware_method(request, response) |
|
167 response = self.apply_response_fixes(request, response) |
|
168 finally: |
|
169 dispatcher.send(signal=signals.request_finished) |
|
170 |
|
171 # Convert our custom HttpResponse object back into the mod_python req. |
|
172 req.content_type = response['Content-Type'] |
|
173 for key, value in response.items(): |
|
174 if key != 'content-type': |
|
175 req.headers_out[str(key)] = str(value) |
|
176 for c in response.cookies.values(): |
|
177 req.headers_out.add('Set-Cookie', c.output(header='')) |
|
178 req.status = response.status_code |
|
179 try: |
|
180 for chunk in response: |
|
181 req.write(chunk) |
|
182 finally: |
|
183 response.close() |
|
184 |
|
185 return 0 # mod_python.apache.OK |
|
186 |
|
187 def handler(req): |
|
188 # mod_python hooks into this function. |
|
189 return ModPythonHandler()(req) |