Ticket #5682: patch_django_5682.20071203.diff
File patch_django_5682.20071203.diff, 12.7 KB (added by , 17 years ago) |
---|
-
django/test/client.py
218 218 219 219 return self.request(**r) 220 220 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 221 234 def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra): 222 235 "Request a response from the server using POST." 223 236 … … 237 250 238 251 return self.request(**r) 239 252 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 240 272 def login(self, **credentials): 241 273 """Set the Client to appear as if it has sucessfully logged into a site. 242 274 -
django/http/__init__.py
21 21 class HttpRequest(object): 22 22 "A basic HTTP request" 23 23 24 # The encoding used in GET/POST dicts. None means use default setting.24 # The encoding used in GET/POST/PUT dicts. None means use default setting. 25 25 _encoding = None 26 26 27 27 def __init__(self): 28 self.GET, self.POST, self. COOKIES, self.META, self.FILES ={}, {}, {}, {}, {}28 self.GET, self.POST, self.PUT, self.COOKIES, self.META, self.DATA, self.FILES = {}, {}, {}, {}, {}, {}, {} 29 29 self.path = '' 30 30 self.method = None 31 31 32 32 def __repr__(self): 33 return '<HttpRequest\nGET:%s,\nPOST:%s,\n COOKIES:%s,\nMETA:%s>' % \34 (pformat(self.GET), pformat(self.POST), pformat(self. COOKIES),35 pformat(self. META))33 return '<HttpRequest\nGET:%s,\nPOST:%s,\nPUT:%s,\nCOOKIES:%s,\nMETA:%s>' % \ 34 (pformat(self.GET), pformat(self.POST), pformat(self.PUT), 35 pformat(self.COOKIES), pformat(self.META)) 36 36 37 37 def __getitem__(self, key): 38 for d in (self.POST, self.GET ):38 for d in (self.POST, self.GET, self.PUT): 39 39 if key in d: 40 40 return d[key] 41 raise KeyError, "%s not found in either POST or GET" % key41 raise KeyError, "%s not found in either POST, GET or PUT" % key 42 42 43 43 def has_key(self, key): 44 return key in self.GET or key in self.POST 44 return key in self.GET or key in self.POST or key in self.PUT 45 45 46 46 __contains__ = has_key 47 47 … … 82 82 83 83 def _set_encoding(self, val): 84 84 """ 85 Sets the encoding used for GET/POST accesses. If the GET or POST85 Sets the encoding used for GET/POST/PUT accesses. If the GET, POST or PUT 86 86 dictionary has already been created, it is removed and recreated on the 87 87 next access (so that it is decoded correctly). 88 88 """ … … 91 91 del self._get 92 92 if hasattr(self, '_post'): 93 93 del self._post 94 if hasattr(self, '_put'): 95 del self._put 94 96 95 97 def _get_encoding(self): 96 98 return self._encoding … … 98 100 encoding = property(_get_encoding, _set_encoding) 99 101 100 102 def parse_file_upload(header_dict, post_data): 101 "Returns a tuple of ( POSTQueryDict, FILES MultiValueDict)"103 "Returns a tuple of (DATA QueryDict, FILES MultiValueDict)" 102 104 import email, email.Message 103 105 from cgi import parse_header 104 106 raw_message = '\r\n'.join(['%s:%s' % pair for pair in header_dict.items()]) 105 107 raw_message += '\r\n\r\n' + post_data 106 108 msg = email.message_from_string(raw_message) 107 POST= QueryDict('', mutable=True)109 DATA = QueryDict('', mutable=True) 108 110 FILES = MultiValueDict() 109 111 for submessage in msg.get_payload(): 110 112 if submessage and isinstance(submessage, email.Message.Message): … … 127 129 'content': submessage.get_payload(), 128 130 })) 129 131 else: 130 POST.appendlist(name_dict['name'], submessage.get_payload())131 return POST, FILES132 DATA.appendlist(name_dict['name'], submessage.get_payload()) 133 return DATA, FILES 132 134 133 135 class QueryDict(MultiValueDict): 134 136 """ -
django/core/handlers/wsgi.py
91 91 except: 92 92 post = '<could not parse>' 93 93 try: 94 put = pformat(self.PUT) 95 except: 96 put = '<could not parse>' 97 try: 94 98 cookies = pformat(self.COOKIES) 95 99 except: 96 100 cookies = '<could not parse>' … … 98 102 meta = pformat(self.META) 99 103 except: 100 104 meta = '<could not parse>' 101 return '<WSGIRequest\nGET:%s,\nPOST:%s,\n COOKIES:%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) 103 107 104 108 def get_full_path(self): 105 109 return '%s%s' % (self.path, self.environ.get('QUERY_STRING', '') and ('?' + self.environ.get('QUERY_STRING', '')) or '') … … 108 112 return 'wsgi.url_scheme' in self.environ \ 109 113 and self.environ['wsgi.url_scheme'] == 'https' 110 114 111 def _load_ post_and_files(self):112 # Populates self._ postand self._files113 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'): 114 118 if self.environ.get('CONTENT_TYPE', '').startswith('multipart'): 115 119 header_dict = dict([(k, v) for k, v in self.environ.items() if k.startswith('HTTP_')]) 116 120 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) 118 122 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() 120 124 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() 122 126 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 123 133 def _get_request(self): 124 134 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) 126 136 return self._request 127 137 128 138 def _get_get(self): … … 136 146 137 147 def _get_post(self): 138 148 if not hasattr(self, '_post'): 139 self._ load_post_and_files()149 self._post = self.DATA 140 150 return self._post 141 151 142 152 def _set_post(self, post): 143 153 self._post = post 144 154 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 145 163 def _get_cookies(self): 146 164 if not hasattr(self, '_cookies'): 147 165 self._cookies = http.parse_cookie(self.environ.get('HTTP_COOKIE', '')) … … 152 170 153 171 def _get_files(self): 154 172 if not hasattr(self, '_files'): 155 self._load_ post_and_files()173 self._load_data_and_files() 156 174 return self._files 157 175 176 def _get_data(self): 177 if not hasattr(self, '_data'): 178 self._load_data_and_files() 179 return self._data 180 158 181 def _get_raw_post_data(self): 159 182 try: 160 183 return self._raw_post_data … … 174 197 175 198 GET = property(_get_get, _set_get) 176 199 POST = property(_get_post, _set_post) 200 PUT = property(_get_put, _set_put) 177 201 COOKIES = property(_get_cookies, _set_cookies) 178 202 FILES = property(_get_files) 203 DATA = property(_get_data) 179 204 REQUEST = property(_get_request) 180 205 raw_post_data = property(_get_raw_post_data) 181 206 207 182 208 class WSGIHandler(BaseHandler): 183 209 initLock = Lock() 184 210 request_class = WSGIRequest -
django/core/handlers/modpython.py
29 29 except: 30 30 post = '<could not parse>' 31 31 try: 32 put = pformat(self.PUT) 33 except: 34 put = '<could not parse>' 35 try: 32 36 cookies = pformat(self.COOKIES) 33 37 except: 34 38 cookies = '<could not parse>' … … 36 40 meta = pformat(self.META) 37 41 except: 38 42 meta = '<could not parse>' 39 return '<ModPythonRequest\npath:%s,\nGET:%s,\nPOST:%s,\n COOKIES:%s,\nMETA:%s>' % \40 (self.path, get, post, cookies, meta)43 return '<ModPythonRequest\npath:%s,\nGET:%s,\nPOST:%s,\nPUT:%s,\nCOOKIES:%s,\nMETA:%s>' % \ 44 (self.path, get, post, put, cookies, meta) 41 45 42 46 def get_full_path(self): 43 47 return '%s%s' % (self.path, self._req.args and ('?' + self._req.args) or '') … … 49 53 # mod_python < 3.2.10 doesn't have req.is_https(). 50 54 return self._req.subprocess_env.get('HTTPS', '').lower() in ('on', '1') 51 55 52 def _load_ post_and_files(self):56 def _load_data_and_files(self): 53 57 "Populates self._post and self._files" 54 58 if 'content-type' in self._req.headers_in and self._req.headers_in['content-type'].startswith('multipart'): 55 self._ post, self._files = http.parse_file_upload(self._req.headers_in, self.raw_post_data)59 self._data, self._files = http.parse_file_upload(self._req.headers_in, self.raw_post_data) 56 60 else: 57 self._ post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict()61 self._data, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict() 58 62 63 def _load_post_and_files(self): 64 # Populates self._post, preserve backward compatibility. 65 if not hasattr(self, '_data'): 66 self._load_data_and_files() 67 self._post = self._data 68 59 69 def _get_request(self): 60 70 if not hasattr(self, '_request'): 61 self._request = datastructures.MergeDict(self.POST, self. GET)71 self._request = datastructures.MergeDict(self.POST, self.PUT, self.GET) 62 72 return self._request 63 73 64 74 def _get_get(self): … … 71 81 72 82 def _get_post(self): 73 83 if not hasattr(self, '_post'): 74 self._ load_post_and_files()84 self._post = self.DATA 75 85 return self._post 76 86 77 87 def _set_post(self, post): 78 88 self._post = post 79 89 90 def _get_put(self): 91 if not hasattr(self, '_put'): 92 self._put = self.DATA 93 return self._put 94 95 def _set_put(self, put): 96 self._put = put 97 80 98 def _get_cookies(self): 81 99 if not hasattr(self, '_cookies'): 82 100 self._cookies = http.parse_cookie(self._req.headers_in.get('cookie', '')) … … 87 105 88 106 def _get_files(self): 89 107 if not hasattr(self, '_files'): 90 self._load_ post_and_files()108 self._load_data_and_files() 91 109 return self._files 92 110 93 111 def _get_meta(self): … … 129 147 130 148 GET = property(_get_get, _set_get) 131 149 POST = property(_get_post, _set_post) 150 PUT = property(_get_put, _set_put) 132 151 COOKIES = property(_get_cookies, _set_cookies) 133 152 FILES = property(_get_files) 153 DATA = property(_get_data) 134 154 META = property(_get_meta) 135 155 REQUEST = property(_get_request) 136 156 raw_post_data = property(_get_raw_post_data)