Ticket #6527: 6527 V2.patch
File 6527 V2.patch, 5.8 KB (added by , 17 years ago) |
---|
-
AUTHORS
373 373 ymasuda@ethercube.com 374 374 Jarek Zgoda <jarek.zgoda@gmail.com> 375 375 Cheng Zhang 376 John DeRosa <stugots@qwest.net> 376 377 378 377 379 A big THANK YOU goes to: 378 380 379 381 Rob Curley and Ralph Gage for letting us open-source Django. -
django/http/__init__.py
257 257 status_code = 200 258 258 259 259 def __init__(self, content='', mimetype=None, status=None, 260 content_type=None):260 content_type=None): 261 261 from django.conf import settings 262 262 263 self._charset = settings.DEFAULT_CHARSET 264 263 265 if mimetype: 264 266 content_type = mimetype # For backwards compatibility 265 267 if not content_type: 266 268 content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, 267 269 settings.DEFAULT_CHARSET) 268 if not isinstance(content, basestring) and hasattr(content, '__iter__'): 269 self._container = content270 self._is_string = False271 else:272 self._container = [content]273 self._is_string = True270 271 # Note - We expect content to be an iterable container, or a string. 272 self._container = [''.join(content)] 273 if hasattr(content, 'close'): 274 content.close() 275 274 276 self.cookies = SimpleCookie() 277 275 278 if status: 276 279 self.status_code = status 277 280 … … 283 286 def __str__(self): 284 287 """Full HTTP message, including headers.""" 285 288 return '\n'.join(['%s: %s' % (key, value) 286 for key, value in self._headers.values()]) \287 + '\n\n' + self.content289 for key, value in self._headers.values()]) \ 290 + '\n\n' + self.content 288 291 289 292 def _convert_to_ascii(self, *values): 290 293 """Converts all values to ascii strings.""" … … 347 350 return smart_str(''.join(self._container), self._charset) 348 351 349 352 def _set_content(self, value): 350 self._container = [value] 351 self._is_string = True 353 self._container = [''.join(value)] 352 354 353 355 content = property(_get_content, _set_content) 354 356 … … 363 365 return str(chunk) 364 366 365 367 def close(self): 366 if hasattr(self._container, 'close'): 367 self._container.close() 368 """This became a noop in the patch for ticket 369 http://code.djangoproject.com/ticket/6527. It's here for backwards- 370 compatibility. 371 """ 372 pass 368 373 369 374 # The remaining methods partially implement the file-like object interface. 370 375 # See http://docs.python.org/lib/bltin-file-objects.html 371 376 def write(self, content): 372 if not self._is_string:373 raise Exception("This %s instance is not writable" % self.__class__)374 377 self._container.append(content) 375 378 376 379 def flush(self): 377 380 pass 378 381 379 382 def tell(self): 380 if not self._is_string:381 raise Exception("This %s instance cannot tell its position" % self.__class__)382 383 return sum([len(chunk) for chunk in self._container]) 383 384 384 385 class HttpResponseRedirect(HttpResponse): -
docs/request_response.txt
369 369 ~~~~~~~~~~~~~~~~~ 370 370 371 371 Finally, you can pass ``HttpResponse`` an iterator rather than passing it 372 hard-coded strings. If you use this technique, follow these guidelines:372 hard-coded strings. If you use this technique, note the following: 373 373 374 374 * The iterator should return strings. 375 * If an ``HttpResponse`` has been initialized with an iterator as its 376 content, you can't use the ``HttpResponse`` instance as a file-like 377 object. Doing so will raise ``Exception``. 375 * ``HttpResponse.__init__()`` will read and store the iterator's contents. 378 376 379 377 Methods 380 378 ------- -
tests/regressiontests/httpwrappers/helloworld.txt
1 Hello world. -
tests/regressiontests/httpwrappers/tests.py
Property changes on: tests\regressiontests\httpwrappers\helloworld.txt ___________________________________________________________________ Name: svn:eol-style + native
427 427 ... 428 428 UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format 429 429 430 ###################################### 431 # HttpResponse with iterable content # 432 ###################################### 433 434 >>> from django.http import HttpResponse 435 >>> response = HttpResponse(file('regressiontests/httpwrappers/helloworld.txt','r')) 436 >>> print response 437 Content-Type: text/html; charset=utf-8 438 <BLANKLINE> 439 Hello world. 440 <BLANKLINE> 441 442 >>> print response 443 Content-Type: text/html; charset=utf-8 444 <BLANKLINE> 445 Hello world. 446 <BLANKLINE> 447 448 >>> print response 449 Content-Type: text/html; charset=utf-8 450 <BLANKLINE> 451 Hello world. 452 <BLANKLINE> 453 454 >>> response = HttpResponse("abc") 455 >>> print response 456 Content-Type: text/html; charset=utf-8 457 <BLANKLINE> 458 abc 459 >>> print response 460 Content-Type: text/html; charset=utf-8 461 <BLANKLINE> 462 abc 463 >>> print response 464 Content-Type: text/html; charset=utf-8 465 <BLANKLINE> 466 abc 467 430 468 """ 431 469 432 470 from django.http import QueryDict, HttpResponse