def _rewriter_middleware(request_rewriter_chain, response_rewriter_chain, application, environ, start_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

def _rewriter_middleware ( request_rewriter_chain , response_rewriter_chain , application , environ , start_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
wraps an application and applies a chain of rewriters to its response .