#NEWLINE# def execute( self ) : #NEWLINE# #INDENT# ( rewrite , headers ) = self . rewrite_headers ( ) #NEWLINE# if ( not headers ) : #NEWLINE# #INDENT# msg = 'HTTP/1.1 502 Gateway Error\r\n\r\n bad request.' #NEWLINE# self . resp . send ( msg ) #NEWLINE# return #NEWLINE# #UNINDENT# if rewrite : #NEWLINE# #INDENT# body = self . parser . body_string ( ) #NEWLINE# if ( not body ) : #NEWLINE# #INDENT# rewritten_body = '' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# html = lxml . html . fromstring ( body ) #NEWLINE# html . rewrite_links ( self . rewrite_link ) #NEWLINE# absolute_path = ( '%s%s' % ( self . local_base , self . extra . get ( 'path' , '' ) ) ) #NEWLINE# old_base = html . find ( './/base' ) #NEWLINE# base = etree . Element ( 'base' ) #NEWLINE# base . attrib [ 'href' ] = absolute_path #NEWLINE# if ( not old_base ) : #NEWLINE# #INDENT# head = html . find ( './/head' ) #NEWLINE# head . append ( base ) #NEWLINE# #UNINDENT# rewritten_body = bytes ( lxml . html . tostring ( html ) ) #NEWLINE# #UNINDENT# headers . extend ( [ ( 'Content-Length: %s\r\n' % len ( rewritten_body ) ) , '\r\n' ] ) #NEWLINE# self . resp . writeall ( bytes ( '' . join ( headers ) ) ) #NEWLINE# stream = io . BytesIO ( rewritten_body ) #NEWLINE# while True : #NEWLINE# #INDENT# data = stream . read ( io . DEFAULT_BUFFER_SIZE ) #NEWLINE# if ( not data ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# self . resp . writeall ( data ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# self . resp . writeall ( bytes ( ( '' . join ( headers ) + '\r\n' ) ) ) #NEWLINE# body = self . parser . body_file ( ) #NEWLINE# send_body ( self . resp , body , self . parser . is_chunked ( ) )