Django

Code

Ticket #5682: patch_django_5682.diff

File patch_django_5682.diff, 9.2 kB (added by david, 1 year ago)

Patch for http request, wsgi and tests' client, without documentation and tests (modpython patch is missing too)

  • django_src/django/http/__init__.py

    old new  
    2020class HttpRequest(object): 
    2121    "A basic HTTP request" 
    2222 
    23     # The encoding used in GET/POST dicts. None means use default setting. 
     23    # The encoding used in GET/POST/PUT dicts. None means use default setting. 
    2424    _encoding = None 
    2525 
    2626    def __init__(self): 
    27         self.GET, self.POST, self.COOKIES, self.META, self.FILES = {}, {}, {}, {}, {} 
     27        self.GET, self.POST, self.PUT, self.COOKIES, self.META, self.DATA, self.FILES = {}, {}, {}, {}, {}, {}, {} 
    2828        self.path = '' 
    2929        self.method = None 
    3030 
    3131    def __repr__(self): 
    32         return '<HttpRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \ 
    33             (pformat(self.GET), pformat(self.POST), pformat(self.COOKIES), 
    34             pformat(self.META)) 
     32        return '<HttpRequest\nGET:%s,\nPOST:%s,\nPUT:%s,\nCOOKIES:%s,\nMETA:%s>' % \ 
     33            (pformat(self.GET), pformat(self.POST), pformat(self.PUT),  
     34            pformat(self.COOKIES), pformat(self.META)) 
    3535 
    3636    def __getitem__(self, key): 
    37         for d in (self.POST, self.GET): 
     37        for d in (self.POST, self.GET, self.PUT): 
    3838            if key in d: 
    3939                return d[key] 
    40         raise KeyError, "%s not found in either POST or GET" % key 
     40        raise KeyError, "%s not found in either POST, GET or PUT" % key 
    4141 
    4242    def has_key(self, key): 
    43         return key in self.GET or key in self.POST 
     43        return key in self.GET or key in self.POST or key in self.PUT 
    4444 
    4545    __contains__ = has_key 
    4646 
     
    8181 
    8282    def _set_encoding(self, val): 
    8383        """ 
    84         Sets the encoding used for GET/POST accesses. If the GET or POS
     84        Sets the encoding used for GET/POST/PUT accesses. If the GET, POST or PU
    8585        dictionary has already been created, it is removed and recreated on the 
    8686        next access (so that it is decoded correctly). 
    8787        """ 
     
    9090            del self._get 
    9191        if hasattr(self, '_post'): 
    9292            del self._post 
     93        if hasattr(self, '_put'): 
     94            del self._put 
    9395 
    9496    def _get_encoding(self): 
    9597        return self._encoding 
     
    9799    encoding = property(_get_encoding, _set_encoding) 
    98100 
    99101def parse_file_upload(header_dict, post_data): 
    100     "Returns a tuple of (POST QueryDict, FILES MultiValueDict)" 
     102    "Returns a tuple of (DATA QueryDict, FILES MultiValueDict)" 
    101103    import email, email.Message 
    102104    from cgi import parse_header 
    103105    raw_message = '\r\n'.join(['%s:%s' % pair for pair in header_dict.items()]) 
    104106    raw_message += '\r\n\r\n' + post_data 
    105107    msg = email.message_from_string(raw_message) 
    106     POST = QueryDict('', mutable=True) 
     108    DATA = QueryDict('', mutable=True) 
    107109    FILES = MultiValueDict() 
    108110    for submessage in msg.get_payload(): 
    109111        if submessage and isinstance(submessage, email.Message.Message): 
     
    126128                    'content': submessage.get_payload(), 
    127129                })) 
    128130            else: 
    129                 POST.appendlist(name_dict['name'], submessage.get_payload()) 
    130     return POST, FILES 
     131                DATA.appendlist(name_dict['name'], submessage.get_payload()) 
     132    return DATA, FILES 
    131133 
    132134class QueryDict(MultiValueDict): 
    133135    """ 
  • django_src/django/test/client.py

    old new  
    218218 
    219219        return self.request(**r) 
    220220 
     221    def delete(self, path, data={}, **extra): 
     222        "Request a response from the server using DELETE." 
     223        r = { 
     224            'CONTENT_LENGTH':  None, 
     225            'CONTENT_TYPE':    'text/html; charset=utf-8', 
     226            'PATH_INFO':       path, 
     227            'QUERY_STRING':    urlencode(data, doseq=True), 
     228            'REQUEST_METHOD': 'DELETE', 
     229        } 
     230        r.update(extra) 
     231 
     232        return self.request(**r) 
     233 
    221234    def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra): 
    222235        "Request a response from the server using POST." 
    223236 
     
    237250 
    238251        return self.request(**r) 
    239252 
     253    def put(self, path, data={}, content_type=MULTIPART_CONTENT, **extra): 
     254        "Request a response from the server using PUT." 
     255 
     256        if content_type is MULTIPART_CONTENT: 
     257            put_data = encode_multipart(BOUNDARY, data) 
     258        else: 
     259            put_data = data 
     260 
     261        r = { 
     262            'CONTENT_LENGTH': len(put_data), 
     263            'CONTENT_TYPE':   content_type, 
     264            'PATH_INFO':      path, 
     265            'REQUEST_METHOD': 'PUT', 
     266            'wsgi.input':     StringIO(put_data), 
     267        } 
     268        r.update(extra) 
     269 
     270        return self.request(**r) 
     271 
    240272    def login(self, **credentials): 
    241273        """Set the Client to appear as if it has sucessfully logged into a site. 
    242274 
  • django_src/django/core/handlers/wsgi.py

    old new  
    9191        except: 
    9292            post = '<could not parse>' 
    9393        try: 
     94            put = pformat(self.PUT) 
     95        except: 
     96            put = '<could not parse>' 
     97        try: 
    9498            cookies = pformat(self.COOKIES) 
    9599        except: 
    96100            cookies = '<could not parse>' 
     
    98102            meta = pformat(self.META) 
    99103        except: 
    100104            meta = '<could not parse>' 
    101         return '<WSGIRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \ 
    102             (get, post, cookies, meta) 
     105        return '<WSGIRequest\nGET:%s,\nPOST:%s,\nPUT:%s,\nCOOKIES:%s,\nMETA:%s>' % \ 
     106            (get, post, put, cookies, meta) 
    103107 
    104108    def get_full_path(self): 
    105109        return '%s%s' % (self.path, self.environ.get('QUERY_STRING', '') and ('?' + self.environ.get('QUERY_STRING', '')) or '') 
     
    108112        return 'wsgi.url_scheme' in self.environ \ 
    109113            and self.environ['wsgi.url_scheme'] == 'https' 
    110114 
    111     def _load_post_and_files(self): 
    112         # Populates self._post and self._files 
    113         if self.method == 'POST'
     115    def _load_data_and_files(self): 
     116        # Populates self._data and self._files 
     117        if self.method in ('POST', 'PUT')
    114118            if self.environ.get('CONTENT_TYPE', '').startswith('multipart'): 
    115119                header_dict = dict([(k, v) for k, v in self.environ.items() if k.startswith('HTTP_')]) 
    116120                header_dict['Content-Type'] = self.environ.get('CONTENT_TYPE', '') 
    117                 self._post, self._files = http.parse_file_upload(header_dict, self.raw_post_data) 
     121                self._data, self._files = http.parse_file_upload(header_dict, self.raw_post_data) 
    118122            else: 
    119                 self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict() 
     123                self._data, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict() 
    120124        else: 
    121             self._post, self._files = http.QueryDict('', encoding=self._encoding), datastructures.MultiValueDict() 
     125            self._data, self._files = http.QueryDict('', encoding=self._encoding), datastructures.MultiValueDict() 
    122126 
     127    def _load_post_and_files(self): 
     128        # Populates self._post, preserve backward compatibility. 
     129        if not hasattr(self, '_data'): 
     130            self._load_data_and_files() 
     131        self._post = self._data 
     132 
    123133    def _get_request(self): 
    124134        if not hasattr(self, '_request'): 
    125             self._request = datastructures.MergeDict(self.POST, self.GET) 
     135            self._request = datastructures.MergeDict(self.POST, self.PUT, self.GET) 
    126136        return self._request 
    127137 
    128138    def _get_get(self): 
     
    136146 
    137147    def _get_post(self): 
    138148        if not hasattr(self, '_post'): 
    139             self._load_post_and_files() 
     149            self._post = self.DATA 
    140150        return self._post 
    141151 
    142152    def _set_post(self, post): 
    143153        self._post = post 
    144154 
     155    def _get_put(self): 
     156        if not hasattr(self, '_put'): 
     157            self._put = self.DATA 
     158        return self._put 
     159 
     160    def _set_put(self, put): 
     161        self._put = put 
     162 
    145163    def _get_cookies(self): 
    146164        if not hasattr(self, '_cookies'): 
    147165            self._cookies = http.parse_cookie(self.environ.get('HTTP_COOKIE', '')) 
     
    152170 
    153171    def _get_files(self): 
    154172        if not hasattr(self, '_files'): 
    155             self._load_post_and_files() 
     173            self._load_data_and_files() 
    156174        return self._files 
    157175 
     176    def _get_data(self): 
     177        if not hasattr(self, '_data'): 
     178            self._load_data_and_files() 
     179        return self._data 
     180 
    158181    def _get_raw_post_data(self): 
    159182        try: 
    160183            return self._raw_post_data 
     
    174197 
    175198    GET = property(_get_get, _set_get) 
    176199    POST = property(_get_post, _set_post) 
     200    PUT = property(_get_put, _set_put) 
    177201    COOKIES = property(_get_cookies, _set_cookies) 
    178202    FILES = property(_get_files) 
     203    DATA = property(_get_data) 
    179204    REQUEST = property(_get_request) 
    180205    raw_post_data = property(_get_raw_post_data) 
    181206 
     207 
    182208class WSGIHandler(BaseHandler): 
    183209    initLock = Lock() 
    184210    request_class = WSGIRequest