Changeset 788
- Timestamp:
- 11/04/05 23:32:32
- Files:
-
- trunk/cherrypy/_cphttptools.py (modified) (3 diffs)
- trunk/cherrypy/_cputil.py (modified) (3 diffs)
- trunk/cherrypy/test/test_core.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cphttptools.py
r786 r788 352 352 _header_order_map[_] = 2 353 353 354 _ie_friendly_error_sizes = {400: 512, 403: 256, 404: 512, 405: 256,355 406: 512, 408: 512, 409: 512, 410: 256,356 500: 512, 501: 512, 505: 512,357 }358 359 354 360 355 class Response(object): … … 401 396 self.headerMap['Content-Length'] = len(content) 402 397 403 # For some statuses, Internet Explorer 5+ shows "friendly error messages"404 # instead of our response.body if the body is smaller than a given size.405 # Fix this by returning a body over that size (by adding whitespace).406 # See http://support.microsoft.com/kb/q218155/407 s = int(self.status.split(" ")[0])408 s = _ie_friendly_error_sizes.get(s, 0)409 if s:410 s += 1411 # Since we are issuing an HTTP error status, we assume that412 # the entity is short, and we should just collapse it.413 content = ''.join([chunk for chunk in self.body])414 self.body = [content]415 l = len(content)416 if l and l < s:417 # IN ADDITION: the response must be written to IE418 # in one chunk or it will still get replaced! Bah.419 self.body = [self.body[0] + (" " * (s - l))]420 self.headerMap['Content-Length'] = s421 422 398 # Headers 423 399 headers = [] … … 428 404 for value in valueList: 429 405 headers.append((order, (key, str(value)))) 430 # RFC 2616: '... it is "good practice" to send general-header fields431 # fi rst, followed by request-header or response-header fields, and432 # ending with the entity-header fields.'406 # RFC 2616: '... it is "good practice" to send general-header 407 # fields first, followed by request-header or response-header 408 # fields, and ending with the entity-header fields.' 433 409 headers.sort() 434 410 self.headers = [item[1] for item in headers] trunk/cherrypy/_cputil.py
r786 r788 189 189 return template % kwargs 190 190 191 191 192 def _cpOnHTTPError(status, message): 192 193 """ Default _cpOnHTTPError method. … … 205 206 # Remove headers which applied to the original content, 206 207 # but do not apply to the error page. 207 for key in ["Accept-Ranges", "Age", "ETag", "Location", 208 "Retry-After", "Vary", "Content-Encoding", 209 "Content-Length", "Content-Location", "Content-MD5", 210 "Expires", "Last-Modified"]: 208 for key in ["Accept-Ranges", "Age", "ETag", "Location", "Retry-After", 209 "Vary", "Content-Encoding", "Content-Length", "Expires", 210 "Content-Location", "Content-MD5", "Last-Modified"]: 211 211 if response.headerMap.has_key(key): 212 212 del response.headerMap[key] … … 227 227 response.headerMap['Content-Type'] = "text/html" 228 228 response.body = getErrorPage(status, traceback=tb, message=message) 229 230 be_ie_unfriendly(status) 231 232 response.headerMap['Content-Length'] = len(response.body) 233 234 235 _ie_friendly_error_sizes = { 236 400: 512, 403: 256, 404: 512, 405: 256, 237 406: 512, 408: 512, 409: 512, 410: 256, 238 500: 512, 501: 512, 505: 512, 239 } 240 241 242 def be_ie_unfriendly(status): 243 244 response = cherrypy.response 245 246 # For some statuses, Internet Explorer 5+ shows "friendly error 247 # messages" instead of our response.body if the body is smaller 248 # than a given size. Fix this by returning a body over that size 249 # (by adding whitespace). 250 # See http://support.microsoft.com/kb/q218155/ 251 s = _ie_friendly_error_sizes.get(status, 0) 252 if s: 253 s += 1 254 # Since we are issuing an HTTP error status, we assume that 255 # the entity is short, and we should just collapse it. 256 content = ''.join([chunk for chunk in response.body]) 257 l = len(content) 258 if l and l < s: 259 # IN ADDITION: the response must be written to IE 260 # in one chunk or it will still get replaced! Bah. 261 response.body = [content + (" " * (s - l))] 262 else: 263 response.body = [content] 229 264 230 265 def formatExc(exc=None): trunk/cherrypy/test/test_core.py
r782 r788 151 151 152 152 def custom(self): 153 raise cherrypy.HTTPError(404, "No, <b>really</b>, not found!") 154 155 def noexist(self): 153 156 raise cherrypy.HTTPError(404, "No, <b>really</b>, not found!") 154 157 … … 329 332 }, 330 333 '/error/custom': { 334 'errorPage.404': os.path.join(localDir, "static/index.html"), 335 }, 336 '/error/noexist': { 331 337 'errorPage.404': "nonexistent.html", 332 338 }, … … 566 572 ignore.pop() 567 573 574 # Test custom error page. 575 self.getPage("/error/custom") 576 self.assertStatus("404 Not Found") 577 self.assertEqual(len(self.body), 513) 578 self.assertBody("Hello, world\r\n" + (" " * 499)) 579 568 580 # Test error in custom error page (ticket #305). 569 581 # Note that the message is escaped for HTML (ticket #310). 570 self.getPage("/error/ custom")582 self.getPage("/error/noexist") 571 583 self.assertStatus("404 Not Found") 572 584 msg = ("No, <b>really</b>, not found!<br />"

