|
4 | 4 | from django import http
|
5 | 5 | from django.core import signals
|
6 | 6 | from django.core.handlers.base import BaseHandler
|
| 7 | +from django.core.urlresolvers import set_script_prefix |
7 | 8 | from django.dispatch import dispatcher
|
8 | 9 | from django.utils import datastructures
|
9 | 10 | from django.utils.encoding import force_unicode, smart_str
|
|
15 | 16 | class ModPythonRequest(http.HttpRequest):
|
16 | 17 | def __init__(self, req):
|
17 | 18 | self._req = req
|
| 19 | + # FIXME: This isn't ideal. The request URI may be encoded (it's |
| 20 | + # non-normalized) slightly differently to the "real" SCRIPT_NAME |
| 21 | + # and PATH_INFO values. This causes problems when we compute path_info, |
| 22 | + # below. For now, don't use script names that will be subject to |
| 23 | + # encoding/decoding. |
18 | 24 | self.path = force_unicode(req.uri)
|
| 25 | + root = req.get_options().get('django.root', '') |
| 26 | + self.django_root = root |
| 27 | + # req.path_info isn't necessarily computed correctly in all |
| 28 | + # circumstances (it's out of mod_python's control a bit), so we use |
| 29 | + # req.uri and some string manipulations to get the right value. |
| 30 | + if root and req.uri.startswith(root): |
| 31 | + self.path_info = force_unicode(req.uri[len(root):]) |
| 32 | + else: |
| 33 | + self.path_info = self.path |
19 | 34 |
|
20 | 35 | def __repr__(self):
|
21 | 36 | # Since this is called as part of error handling, we need to be very
|
@@ -100,15 +115,15 @@ def _get_meta(self):
|
100 | 115 | 'CONTENT_LENGTH': self._req.clength, # This may be wrong
|
101 | 116 | 'CONTENT_TYPE': self._req.content_type, # This may be wrong
|
102 | 117 | 'GATEWAY_INTERFACE': 'CGI/1.1',
|
103 | | - 'PATH_INFO': self._req.path_info, |
| 118 | + 'PATH_INFO': self.path_info, |
104 | 119 | 'PATH_TRANSLATED': None, # Not supported
|
105 | 120 | 'QUERY_STRING': self._req.args,
|
106 | 121 | 'REMOTE_ADDR': self._req.connection.remote_ip,
|
107 | 122 | 'REMOTE_HOST': None, # DNS lookups not supported
|
108 | 123 | 'REMOTE_IDENT': self._req.connection.remote_logname,
|
109 | 124 | 'REMOTE_USER': self._req.user,
|
110 | 125 | 'REQUEST_METHOD': self._req.method,
|
111 | | - 'SCRIPT_NAME': None, # Not supported |
| 126 | + 'SCRIPT_NAME': self.django_root, |
112 | 127 | 'SERVER_NAME': self._req.server.server_hostname,
|
113 | 128 | 'SERVER_PORT': self._req.server.port,
|
114 | 129 | 'SERVER_PROTOCOL': self._req.protocol,
|
@@ -153,6 +168,7 @@ def __call__(self, req):
|
153 | 168 | if self._request_middleware is None:
|
154 | 169 | self.load_middleware()
|
155 | 170 |
|
| 171 | + set_script_prefix(req.get_options().get('django.root', '')) |
156 | 172 | dispatcher.send(signal=signals.request_started)
|
157 | 173 | try:
|
158 | 174 | try:
|
|
0 commit comments