Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 788

Show
Ignore:
Timestamp:
11/04/05 23:32:32
Author:
fumanchu
Message:

Moved IE-friendly-error-fix from finalize method to HTTPError.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cphttptools.py

    r786 r788  
    352352    _header_order_map[_] = 2 
    353353 
    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  
    359354 
    360355class Response(object): 
     
    401396                self.headerMap['Content-Length'] = len(content) 
    402397         
    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 += 1 
    411             # Since we are issuing an HTTP error status, we assume that 
    412             # 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 IE 
    418                 # in one chunk or it will still get replaced! Bah. 
    419                 self.body = [self.body[0] + (" " * (s - l))] 
    420                 self.headerMap['Content-Length'] = s 
    421          
    422398        # Headers 
    423399        headers = [] 
     
    428404            for value in valueList: 
    429405                headers.append((order, (key, str(value)))) 
    430         # RFC 2616: '... it is "good practice" to send general-header fields 
    431         # first, followed by request-header or response-header fields, and 
    432         # 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.' 
    433409        headers.sort() 
    434410        self.headers = [item[1] for item in headers] 
  • trunk/cherrypy/_cputil.py

    r786 r788  
    189189    return template % kwargs 
    190190 
     191 
    191192def _cpOnHTTPError(status, message): 
    192193    """ Default _cpOnHTTPError method. 
     
    205206    # Remove headers which applied to the original content, 
    206207    # 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"]: 
    211211        if response.headerMap.has_key(key): 
    212212            del response.headerMap[key] 
     
    227227    response.headerMap['Content-Type'] = "text/html" 
    228228    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 
     242def 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] 
    229264 
    230265def formatExc(exc=None): 
  • trunk/cherrypy/test/test_core.py

    r782 r788  
    151151     
    152152    def custom(self): 
     153        raise cherrypy.HTTPError(404, "No, <b>really</b>, not found!") 
     154     
     155    def noexist(self): 
    153156        raise cherrypy.HTTPError(404, "No, <b>really</b>, not found!") 
    154157     
     
    329332    }, 
    330333    '/error/custom': { 
     334        'errorPage.404': os.path.join(localDir, "static/index.html"), 
     335    }, 
     336    '/error/noexist': { 
    331337        'errorPage.404': "nonexistent.html", 
    332338    }, 
     
    566572            ignore.pop() 
    567573         
     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         
    568580        # Test error in custom error page (ticket #305). 
    569581        # Note that the message is escaped for HTML (ticket #310). 
    570         self.getPage("/error/custom") 
     582        self.getPage("/error/noexist") 
    571583        self.assertStatus("404 Not Found") 
    572584        msg = ("No, &lt;b&gt;really&lt;/b&gt;, not found!<br />" 

Hosted by WebFaction

Log in as guest/cpguest to create tickets