human
Complete this python program:
def _rewriter_middleware(request_rewriter_chain, response_rewriter_chain, application, environ, start_response):
assistant
#Here is the Python code you requested.

    """Wraps an application and applies a chain of rewriters to its response.
    This first applies each function in request_rewriter_chain to the environ. It
    then executes the application, and applies each function in
    response_rewriter_chain to the response.
    Args:
    request_rewriter_chain: A chain of functions to apply to the environ.
    response_rewriter_chain: A chain of functions to apply to the response.
    application: The WSGI application to wrap as defined in PEP-333.
    environ: An environ dict for the current request as defined in PEP-333.
    start_response: A function with semantics defined in PEP-333.
    Returns:
    An iterable of strings containing the body of an HTTP response.
    """
    response_dict = {'headers_sent': False}
    write_body = cStringIO.StringIO()
    def wrapped_start_response(status, response_headers, exc_info=None):
        if (exc_info and response_dict['headers_sent']):
            raise exc_info[0], exc_info[1], exc_info[2]
        response_dict['status'] = status
        response_dict['response_headers'] = response_headers
        return write_body.write
    for rewriter in request_rewriter_chain:
        rewriter(environ)
    response_body = iter(application(environ, wrapped_start_response))
    first = write_body.getvalue()
    while (not first):
        try:
            first = response_body.next()
        except StopIteration:
            break
    response_dict['headers_sent'] = True
    try:
        status = response_dict['status']
        response_headers = response_dict['response_headers']
    except KeyError:
        raise AssertionError('Application    yielded    before    calling    start_response.')
    def reconstructed_body():
        (yield first)
        for string in response_body:
            (yield string)
    body = reconstructed_body()
    state = RewriterState(environ, status, response_headers, body)
    for rewriter in response_rewriter_chain:
        rewriter(state)
    start_response(state.status, state.headers.items())
    return state.body