#NEWLINE# def __call__( self , environ , start_response ) : #NEWLINE# #INDENT# url = [ ] #NEWLINE# code_message = [ ] #NEWLINE# try : #NEWLINE# #INDENT# def change_response( status , headers , exc_info = None ) : #NEWLINE# #INDENT# new_url = None #NEWLINE# parts = status . split ( ' ' ) #NEWLINE# try : #NEWLINE# #INDENT# code = int ( parts [ 0 ] ) #NEWLINE# #UNINDENT# except ( ValueError , TypeError ) : #NEWLINE# #INDENT# raise Exception ( ( '_StatusBasedRedirect middleware received an invalid status code %s' % repr ( parts [ 0 ] ) ) ) #NEWLINE# #UNINDENT# message = ' ' . join ( parts [ 1 : ] ) #NEWLINE# new_url = self . mapper ( code , message , environ , self . global_conf , self . kw ) #NEWLINE# if ( not ( ( new_url == None ) or isinstance ( new_url , str ) ) ) : #NEWLINE# #INDENT# raise TypeError ( ( 'Expected the url to internally redirect to in the _StatusBasedRedirect error_mapperto be a string or None, not %s' % repr ( new_url ) ) ) #NEWLINE# #UNINDENT# if new_url : #NEWLINE# #INDENT# url . append ( new_url ) #NEWLINE# #UNINDENT# code_message . append ( [ code , message ] ) #NEWLINE# return start_response ( status , headers , exc_info ) #NEWLINE# #UNINDENT# app_iter = self . application ( environ , change_response ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# import sys #NEWLINE# error = str ( sys . exc_info ( ) [ 1 ] ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# error = '' #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# ( code , message ) = code_message [ 0 ] #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# ( code , message ) = [ '' , '' ] #NEWLINE# #UNINDENT# environ [ 'wsgi.errors' ] . write ( ( 'Error occurred in _StatusBasedRedirect intercepting the response: ' + str ( error ) ) ) #NEWLINE# return [ ( self . fallback_template % { #NEWLINE# #INDENT# 'message' : error , #NEWLINE# 'code' : code , #NEWLINE# #UNINDENT# } ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if url : #NEWLINE# #INDENT# url_ = url [ 0 ] #NEWLINE# new_environ = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( k , v ) in environ . items ( ) : #NEWLINE# #INDENT# if ( k != 'QUERY_STRING' ) : #NEWLINE# #INDENT# new_environ [ 'QUERY_STRING' ] = urlparse . urlparse ( url_ ) [ 4 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# new_environ [ k ] = v #NEWLINE# #UNINDENT# #UNINDENT# class InvalidForward ( Exception ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# def eat_start_response( status , headers , exc_info = None ) : #NEWLINE# #INDENT# "\n                    We don't want start_response to do anything since it\n                    has already been called\n                    " #NEWLINE# if ( status [ : 3 ] != '200' ) : #NEWLINE# #INDENT# raise InvalidForward ( ( "The URL %s to internally forward to in order to create an error document did not return a '200' status code." % url_ ) ) #NEWLINE# #UNINDENT# #UNINDENT# forward = environ [ 'paste.recursive.forward' ] #NEWLINE# old_start_response = forward . start_response #NEWLINE# forward . start_response = eat_start_response #NEWLINE# try : #NEWLINE# #INDENT# app_iter = forward ( url_ , new_environ ) #NEWLINE# #UNINDENT# except InvalidForward : #NEWLINE# #INDENT# ( code , message ) = code_message [ 0 ] #NEWLINE# environ [ 'wsgi.errors' ] . write ( ( 'Error occurred in _StatusBasedRedirect redirecting to new URL: ' + str ( url [ 0 ] ) ) ) #NEWLINE# return [ ( self . fallback_template % { #NEWLINE# #INDENT# 'message' : message , #NEWLINE# 'code' : code , #NEWLINE# #UNINDENT# } ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# forward . start_response = old_start_response #NEWLINE# return app_iter #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# return app_iter