#920 closed defect (fixed)
[patch] patch_response_headers breaks when response.content is unicode
| Reported by: | Owned by: | Jacob | |
|---|---|---|---|
| Component: | Core (Cache system) | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | Forest Bond | Triage Stage: | Unreviewed |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
It's because hexdigest is not unicode-safe, so when a non-ascii unicode response.content is encoded with the default ascii encoding things break. A fix:
--- django/utils/cache.py (revision 3523)
+++ django/utils/cache.py (local)
@@ -75,7 +75,7 @@
now = datetime.datetime.utcnow()
expires = now + datetime.timedelta(0, cache_timeout)
if not response.has_header('ETag'):
- response['ETag'] = md5.new(response.content).hexdigest()
+ response['ETag'] = md5.new(response.content.encode('utf8')).hexdigest()
if not response.has_header('Last-Modified'):
response['Last-Modified'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
if not response.has_header('Expires'):
Change History (5)
comment:1 by , 20 years ago
comment:2 by , 20 years ago
Ooops.. I tried ' '.encode which works just fine, and assumed that str.encode was an identity-mapping :( Here's a more appropriate patch:
--- cache.py (/mirror/django/trunk/django/utils/cache.py) (revision 3596)
+++ cache.py (/mirror/private/django/django/utils/cache.py) (local)
@@ -75,7 +75,10 @@
now = datetime.datetime.utcnow()
expires = now + datetime.timedelta(0, cache_timeout)
if not response.has_header('ETag'):
- response['ETag'] = md5.new(response.content).hexdigest()
+ if isinstance(response.content, unicode):
+ response['ETag'] = md5.new(response.content.encode('utf8')).hexdigest()
+ else:
+ response['ETag'] = md5.new(response.content).hexdigest()
if not response.has_header('Last-Modified'):
response['Last-Modified'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
if not response.has_header('Expires'):
comment:3 by , 20 years ago
yeah, 'xxxx'.encode('utf-8') works for xxxxx that itself doesn't use 8bit chars in the non-unicode string. As soon as there is some 8bit stuff in there, it will break. It's only logical: the system can't know for bytestrings what charset they are in, except if it is the default encoding (ascii).
comment:4 by , 20 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
comment:5 by , 16 years ago
| Cc: | added |
|---|
Uhm - your patch will break if content is _not_ unicode :-)
Best to put an "if type(response.content) is unicode" switch in there.