Ticket #376: django-modpython.3.patch

File django-modpython.3.patch, 4.9 KB (added by Andrew Fedorov, 8 years ago)

Small fix and comments

  • core/handlers/modpython.py

    diff -uprN django.orig/core/handlers/modpython.py django/core/handlers/modpython.py
    old new import os 
    1111# settings) until after ModPythonHandler has been called; otherwise os.environ
    1212# won't be set up correctly (with respect to settings).
    1313
     14# NOTE: Use only has_key() and keys() methods of Table objects to be compatible
     15# with mod_python2
     16
    1417class ModPythonRequest(http.HttpRequest):
    1518    def __init__(self, req):
    1619        self._req = req
    class ModPythonRequest(http.HttpRequest) 
    4346
    4447    def is_secure(self):
    4548        # Note: modpython 3.2.10+ has req.is_https(), but we need to support previous versions
    46         return 'HTTPS' in self._req.subprocess_env and self._req.subprocess_env['HTTPS'] == 'on'
     49        return self._req.subprocess_env.has_key('HTTPS') and self._req.subprocess_env['HTTPS'] == 'on'
    4750
    4851    def _load_post_and_files(self):
    4952        "Populates self._post and self._files"
    50         if 'content-type' in self._req.headers_in and self._req.headers_in['content-type'].startswith('multipart'):
     53        if self._req.headers_in.has_key('content-type') and self._req.headers_in['content-type'].startswith('multipart'):
    5154            self._post, self._files = http.parse_file_upload(self._req.headers_in, self.raw_post_data)
    5255        else:
    5356            self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict()
    class ModPythonRequest(http.HttpRequest) 
    7578
    7679    def _get_cookies(self):
    7780        if not hasattr(self, '_cookies'):
    78             self._cookies = http.parse_cookie(self._req.headers_in.get('cookie', ''))
     81            # In mod_python2 Table object does not have get() method
     82            if not self._req.headers_in.has_key('cookie'):
     83                self._req.headers_in['cookie'] = ''
     84            self._cookies = http.parse_cookie(self._req.headers_in['cookie'])
    7985        return self._cookies
    8086
    8187    def _set_cookies(self, cookies):
    class ModPythonRequest(http.HttpRequest) 
    8995    def _get_meta(self):
    9096        "Lazy loader that returns self.META dictionary"
    9197        if not hasattr(self, '_meta'):
     98            # ap_uath_type and user are members of request object in
     99            # mod_python3, but members of request.connection in mod_python2
     100            if hasattr(self._req, 'ap_auth_type'):
     101                auth_type = self._req.ap_auth_type
     102            elif hasattr(self._req.connection, 'ap_auth_type'):
     103                auth_type = self._req.connection.ap_auth_type
     104            else:
     105                auth_type = None
     106            if hasattr(self._req, 'user'):
     107                user = self._req.user
     108            elif hasattr(self._req.connection, 'user'):
     109                user = self._req.connection.user
     110            else:
     111                user = None
    92112            self._meta = {
    93                 'AUTH_TYPE':         self._req.ap_auth_type,
     113                'AUTH_TYPE':         auth_type,
    94114                'CONTENT_LENGTH':    self._req.clength, # This may be wrong
    95115                'CONTENT_TYPE':      self._req.content_type, # This may be wrong
    96116                'GATEWAY_INTERFACE': 'CGI/1.1',
    class ModPythonRequest(http.HttpRequest) 
    100120                'REMOTE_ADDR':       self._req.connection.remote_ip,
    101121                'REMOTE_HOST':       None, # DNS lookups not supported
    102122                'REMOTE_IDENT':      self._req.connection.remote_logname,
    103                 'REMOTE_USER':       self._req.user,
     123                'REMOTE_USER':       user,
    104124                'REQUEST_METHOD':    self._req.method,
    105125                'SCRIPT_NAME':       None, # Not supported
    106126                'SERVER_NAME':       self._req.server.server_hostname,
    class ModPythonRequest(http.HttpRequest) 
    108128                'SERVER_PROTOCOL':   self._req.protocol,
    109129                'SERVER_SOFTWARE':   'mod_python'
    110130            }
    111             for key, value in self._req.headers_in.items():
    112                 key = 'HTTP_' + key.upper().replace('-', '_')
    113                 self._meta[key] = value
     131            for key in self._req.headers_in.keys():
     132                meta_key = 'HTTP_' + key.upper().replace('-', '_')
     133                self._meta[meta_key] = self._req.headers_in[key]
    114134        return self._meta
    115135
    116136    def _get_raw_post_data(self):
    class ModPythonHandler(BaseHandler): 
    166186            req.headers_out.add('Set-Cookie', c.output(header=''))
    167187        req.status = response.status_code
    168188        try:
     189            if hasattr(req, 'send_http_header'):
     190                # This method exists only in mod_python2 and must be called
     191                # before any data transfer
     192                req.send_http_header()
    169193            for chunk in response:
    170194                req.write(chunk)
    171195        finally:
Back to Top