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

Ticket #928 (defect)

Opened 10 months ago

Last modified 9 months ago

custom error page returning an iterable results in bad Content-Length

Status: closed (fixed)

Reported by: visteya Assigned to: fumanchu
Priority: normal Milestone: 3.2
Component: CherryPy code Keywords:
Cc:

The docstring for request.error_page says:

If a callable is provided, it will be called by default with keyword arguments 'status', 'message', 'traceback', and 'version', as for a string-formatting template. The callable must return a string which will be set to response.body.

The alleged requirement to return a string is different from ordinary page handlers which may return an iterable such as a list of strings.

What happens if an error page callable returns a list of strings instead of a single string? In _cperror.py, HTTPError.set_response() will set Content-Length to the number of elements in the list:

        content = self.get_error_page(self.status, traceback=tb,
                                      message=self._message)
        response.body = content
        response.headers['Content-Length'] = len(content)

A list of strings is a perfectly acceptable iterable to return to WSGI. And, in fact, the response.body provided by the error page callable is returned as-is to WSGI. Unfortunately, since HTTPError.set_response() insists on setting Content-Length based on the assumption the error page callable returns a single string, the HTTP response seen by the client will receive the entire response body but an incorrect Content-Length header.

If Content-Length were not set by HTTPError.set_response(), response.finalize() would calculate and set a correct Content-Length header.

I see no reason to require error page callables to return a single string instead of any iterable like ordinary page handlers. It is an inconsistency and a needless restriction which silently causes broken responses if not complied with.

Change History

06/14/09 23:52:35: Modified by visteya

  • status changed from new to closed.
  • resolution set to fixed.

fixed in r2449 (trunk) and r2450 (python3)

Hosted by WebFaction

Log in as guest/cpguest to create tickets