Ticket #376: django-modpython2-support.patch

File django-modpython2-support.patch, 4.5 KB (added by Andrew Fedorov, 17 years ago)

Updated patch for revision 7020

  • core/handlers/modpython.py

    diff -uprN django.orig/core/handlers/modpython.py django/core/handlers/modpython.py
    old new class ModPythonRequest(http.HttpRequest)  
    4747            return self._req.is_https()
    4848        except AttributeError:
    4949            # mod_python < 3.2.10 doesn't have req.is_https().
    50             return self._req.subprocess_env.get('HTTPS', '').lower() in ('on', '1')
     50            return self._req.subprocess_env.has_key('HTTPS') and self._req.subprocess_env['HTTPS'].lower() in ('on', '1')
    5151
    5252    def _load_post_and_files(self):
    5353        "Populates self._post and self._files"
    54         if 'content-type' in self._req.headers_in and self._req.headers_in['content-type'].startswith('multipart'):
     54        if self._req.headers_in.has_key('content-type') and self._req.headers_in['content-type'].startswith('multipart'):
    5555            self._post, self._files = http.parse_file_upload(self._req.headers_in, self.raw_post_data)
    5656        else:
    5757            self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict()
    class ModPythonRequest(http.HttpRequest)  
    7979
    8080    def _get_cookies(self):
    8181        if not hasattr(self, '_cookies'):
    82             self._cookies = http.parse_cookie(self._req.headers_in.get('cookie', ''))
     82            # mod_python2 Table object does not have get() method
     83            if not self._req.headers_in.has_key('cookie'):
     84                self._req.headers_in['cookie'] = ''
     85            self._cookies = http.parse_cookie(self._req.headers_in['cookie'])
    8386        return self._cookies
    8487
    8588    def _set_cookies(self, cookies):
    class ModPythonRequest(http.HttpRequest)  
    9396    def _get_meta(self):
    9497        "Lazy loader that returns self.META dictionary"
    9598        if not hasattr(self, '_meta'):
     99            # ap_uath_type and user are members of request object in
     100            # mod_python3, but members of request.connection in mod_python2
     101            if hasattr(self._req, 'ap_auth_type'):
     102                auth_type = self._req.ap_auth_type
     103            elif hasattr(self._req.connection, 'ap_auth_type'):
     104                auth_type = self._req.connection.ap_auth_type
     105            else:
     106                auth_type = None
     107            if hasattr(self._req, 'user'):
     108                user = self._req.user
     109            elif hasattr(self._req.connection, 'user'):
     110                user = self._req.connection.user
     111            else:
     112                user = None
    96113            self._meta = {
    97                 'AUTH_TYPE':         self._req.ap_auth_type,
     114                'AUTH_TYPE':         auth_type,
    98115                'CONTENT_LENGTH':    self._req.clength, # This may be wrong
    99116                'CONTENT_TYPE':      self._req.content_type, # This may be wrong
    100117                'GATEWAY_INTERFACE': 'CGI/1.1',
    class ModPythonRequest(http.HttpRequest)  
    104121                'REMOTE_ADDR':       self._req.connection.remote_ip,
    105122                'REMOTE_HOST':       None, # DNS lookups not supported
    106123                'REMOTE_IDENT':      self._req.connection.remote_logname,
    107                 'REMOTE_USER':       self._req.user,
     124                'REMOTE_USER':       user,
    108125                'REQUEST_METHOD':    self._req.method,
    109126                'SCRIPT_NAME':       None, # Not supported
    110127                'SERVER_NAME':       self._req.server.server_hostname,
    class ModPythonRequest(http.HttpRequest)  
    112129                'SERVER_PROTOCOL':   self._req.protocol,
    113130                'SERVER_SOFTWARE':   'mod_python'
    114131            }
    115             for key, value in self._req.headers_in.items():
    116                 key = 'HTTP_' + key.upper().replace('-', '_')
    117                 self._meta[key] = value
     132            for key in self._req.headers_in.keys():
     133                meta_key = 'HTTP_' + key.upper().replace('-', '_')
     134                self._meta[meta_key] = self._req.headers_in[key]
    118135        return self._meta
    119136
    120137    def _get_raw_post_data(self):
    class ModPythonHandler(BaseHandler):  
    176193            req.headers_out.add('Set-Cookie', c.output(header=''))
    177194        req.status = response.status_code
    178195        try:
     196            if hasattr(req, 'send_http_header'):
     197                # This method exists only in mod_python2 and must be called
     198                # before any data transfer
     199                req.send_http_header()
    179200            for chunk in response:
    180201                req.write(chunk)
    181202        finally:
Back to Top