Ticket #1569: 1569.trunk.diff
File 1569.trunk.diff, 5.1 KB (added by , 19 years ago) |
---|
-
django/utils/httpwrappers.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. … … 143 144 cookiedict[key] = c.get(key).value 144 145 return cookiedict 145 146 146 class HttpResponse :147 class HttpResponse(object): 147 148 "A basic HTTP response, with content and dictionary-accessed headers" 148 149 def __init__(self, content='', mimetype=None): 149 150 if not mimetype: 150 from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET151 151 mimetype = "%s; charset=%s" % (DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET) 152 self.content = content 152 if hasattr(content,'__iter__'): 153 self.iterator = content 154 self._is_string = False 155 else: 156 self.iterator = [content] 157 self._is_string = True 153 158 self.headers = {'Content-Type':mimetype} 154 159 self.cookies = SimpleCookie() 155 160 self.status_code = 200 … … 193 198 except KeyError: 194 199 pass 195 200 196 def get_content_as_string(self, encoding): 197 """ 198 Returns the content as a string, encoding it from a Unicode object if 199 necessary. 200 """ 201 if isinstance(self.content, unicode): 202 return self.content.encode(encoding) 203 return self.content 201 def _get_content(self): 202 content = ''.join(self.iterator) 203 if isinstance(content, unicode): 204 content = content.encode(DEFAULT_CHARSET) 205 return content 204 206 207 def _set_content(self, value): 208 self.iterator = [value] 209 self._is_string = True 210 211 content = property(_get_content,_set_content) 212 205 213 # The remaining methods partially implement the file-like object interface. 206 214 # See http://docs.python.org/lib/bltin-file-objects.html 207 215 def write(self, content): 208 self.content += content 216 if not self._is_string: 217 raise Exception, "This %s instance is not writable"%self.__class__ 218 self.iterator.append(content) 209 219 210 220 def flush(self): 211 221 pass 212 222 213 223 def tell(self): 214 return len(self.content) 224 if not self._is_string: 225 raise Exception, "This %s instance cannot tell its position"%self.__class__ 226 return sum(len(chunk) for chunk in self.iterator) 215 227 216 228 class HttpResponseRedirect(HttpResponse): 217 229 def __init__(self, redirect_to): -
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/core/handlers/wsgi.py
172 172 response_headers = response.headers.items() 173 173 for c in response.cookies.values(): 174 174 response_headers.append(('Set-Cookie', c.output(header=''))) 175 output = [response.get_content_as_string(settings.DEFAULT_CHARSET)]176 175 start_response(status, response_headers) 177 return output176 return response.iterator -
django/core/handlers/modpython.py
162 162 for c in http_response.cookies.values(): 163 163 mod_python_req.headers_out.add('Set-Cookie', c.output(header='')) 164 164 mod_python_req.status = http_response.status_code 165 mod_python_req.write(http_response.get_content_as_string(settings.DEFAULT_CHARSET)) 165 for chunk in http_response.iterator: 166 mod_python_req.write(chunk) 166 167 167 168 def handler(req): 168 169 # mod_python hooks into this function. -
django/middleware/common.py
67 67 68 68 # Use ETags, if requested. 69 69 if settings.USE_ETAGS: 70 etag = md5.new(response. get_content_as_string(settings.DEFAULT_CHARSET)).hexdigest()70 etag = md5.new(response.content).hexdigest() 71 71 if request.META.get('HTTP_IF_NONE_MATCH') == etag: 72 72 response = httpwrappers.HttpResponseNotModified() 73 73 else: