'Try to convert the parameter into something WSGI compatible and set
correct HTTP headers when possible.
Support: False, str, unicode, dict, HTTPResponse, HTTPError, file-like,
iterable of strings and iterable of unicodes'

def _cast(self, out, request, response, peek=None):

    if (not out):
        response['Content-Length'] = 0
        return []
    if (isinstance(out, (tuple, list)) and isinstance(out[0], (bytes, unicode))):
        out = out[0][0:0].join(out)
    if isinstance(out, unicode):
        out = out.encode(response.charset)
    if isinstance(out, bytes):
        response['Content-Length'] = len(out)
        return [out]
    if isinstance(out, HTTPError):
        out.apply(response)
        out = self.error_handler.get(out.status, repr)(out)
        if isinstance(out, HTTPResponse):
            depr('Error    handlers    must    not    return    :exc:`HTTPResponse`.')
        return self._cast(out, request, response)
    if isinstance(out, HTTPResponse):
        out.apply(response)
        return self._cast(out.output, request, response)
    if hasattr(out, 'read'):
        if ('wsgi.file_wrapper' in request.environ):
            return request.environ['wsgi.file_wrapper'](out)
        elif (hasattr(out, 'close') or (not hasattr(out, '__iter__'))):
            return WSGIFileWrapper(out)
    try:
        out = iter(out)
        first = out.next()
        while (not first):
            first = out.next()
    except StopIteration:
        return self._cast('', request, response)
    except HTTPResponse as e:
        first = e
    except Exception as e:
        first = HTTPError(500, 'Unhandled    exception', e, format_exc(10))
        if (isinstance(e, (KeyboardInterrupt, SystemExit, MemoryError)) or (not self.catchall)):
            raise
    if isinstance(first, HTTPResponse):
        return self._cast(first, request, response)
    if isinstance(first, bytes):
        return itertools.chain([first], out)
    if isinstance(first, unicode):
        return itertools.imap((lambda x: x.encode(response.charset)), itertools.chain([first], out))
    return self._cast(HTTPError(500, ('Unsupported    response    type:    %s' % type(first))), request, response)
