Ticket #1569: 1569.m-r.diff
File 1569.m-r.diff, 5.1 KB (added by , 19 years ago) |
---|
-
django/http/__init__.py
2 2 from pprint import pformat 3 3 from urllib import urlencode 4 4 from django.utils.datastructures import MultiValueDict 5 from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET 5 6 6 7 try: 7 8 # The mod_python version is more efficient, so try importing it first. … … 146 147 cookiedict[key] = c.get(key).value 147 148 return cookiedict 148 149 149 class HttpResponse :150 class HttpResponse(object): 150 151 "A basic HTTP response, with content and dictionary-accessed headers" 151 152 def __init__(self, content='', mimetype=None): 152 153 if not mimetype: 153 from django.conf import settings 154 mimetype = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, settings.DEFAULT_CHARSET) 155 self.content = content 154 mimetype = "%s; charset=%s" % (DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET) 155 if hasattr(content,'__iter__'): 156 self.iterator = content 157 self._is_string = False 158 else: 159 self.iterator = [content] 160 self._is_string = True 156 161 self.headers = {'Content-Type':mimetype} 157 162 self.cookies = SimpleCookie() 158 163 self.status_code = 200 … … 196 201 except KeyError: 197 202 pass 198 203 199 def get_content_as_string(self, encoding): 200 """ 201 Returns the content as a string, encoding it from a Unicode object if 202 necessary. 203 """ 204 if isinstance(self.content, unicode): 205 return self.content.encode(encoding) 206 return self.content 204 def _get_content(self): 205 content = ''.join(self.iterator) 206 if isinstance(content, unicode): 207 content = content.encode(DEFAULT_CHARSET) 208 return content 207 209 210 def _set_content(self, value): 211 self.iterator = [value] 212 self._is_string = True 213 214 content = property(_get_content,_set_content) 215 208 216 # The remaining methods partially implement the file-like object interface. 209 217 # See http://docs.python.org/lib/bltin-file-objects.html 210 218 def write(self, content): 211 self.content += content 219 if not self._is_string: 220 raise Exception, "This %s instance is not writable"%self.__class__ 221 self.iterator.append(content) 212 222 213 223 def flush(self): 214 224 pass 215 225 216 226 def tell(self): 217 return len(self.content) 227 if not self._is_string: 228 raise Exception, "This %s instance cannot tell its position"%self.__class__ 229 return sum(len(chunk) for chunk in self.iterator) 218 230 219 231 class HttpResponseRedirect(HttpResponse): 220 232 def __init__(self, redirect_to): -
django/core/handlers/wsgi.py
159 159 response_headers = response.headers.items() 160 160 for c in response.cookies.values(): 161 161 response_headers.append(('Set-Cookie', c.output(header=''))) 162 output = [response.get_content_as_string(settings.DEFAULT_CHARSET)]163 162 start_response(status, response_headers) 164 return output163 return response.iterator -
django/core/handlers/modpython.py
149 149 for c in http_response.cookies.values(): 150 150 mod_python_req.headers_out.add('Set-Cookie', c.output(header='')) 151 151 mod_python_req.status = http_response.status_code 152 mod_python_req.write(http_response.get_content_as_string(settings.DEFAULT_CHARSET)) 152 for chunk in http_response.iterator: 153 mod_python_req.write(chunk) 153 154 154 155 def handler(req): 155 156 # mod_python hooks into this function. -
django/utils/cache.py
74 74 cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS 75 75 now = datetime.datetime.utcnow() 76 76 if not response.has_header('ETag'): 77 response['ETag'] = md5.new(response. get_content_as_string('utf8')).hexdigest()77 response['ETag'] = md5.new(response.content).hexdigest() 78 78 if not response.has_header('Last-Modified'): 79 79 response['Last-Modified'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT') 80 80 if not response.has_header('Expires'): -
django/middleware/common.py
68 68 69 69 # Use ETags, if requested. 70 70 if settings.USE_ETAGS: 71 etag = md5.new(response. get_content_as_string(settings.DEFAULT_CHARSET)).hexdigest()71 etag = md5.new(response.content).hexdigest() 72 72 if request.META.get('HTTP_IF_NONE_MATCH') == etag: 73 73 response = http.HttpResponseNotModified() 74 74 else: