#NEWLINE# def _handle_request( self , request ) : #NEWLINE# #INDENT# res = webob . Response ( ) #NEWLINE# res_content_type = None #NEWLINE# path = request . path #NEWLINE# if path . startswith ( self . _webpath ) : #NEWLINE# #INDENT# path = path [ len ( self . _webpath ) : ] #NEWLINE# #UNINDENT# ( routepath , func ) = self . find_route ( path ) #NEWLINE# if routepath : #NEWLINE# #INDENT# content = func ( ) #NEWLINE# if isinstance ( content , six . text_type ) : #NEWLINE# #INDENT# res . text = content #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( content , six . binary_type ) : #NEWLINE# #INDENT# res . body = content #NEWLINE# #UNINDENT# #UNINDENT# res . content_type = func . _cfg [ 'content-type' ] #NEWLINE# return res #NEWLINE# #UNINDENT# if ( request . path == ( self . _webpath + '/api.spore' ) ) : #NEWLINE# #INDENT# res . body = spore . getdesc ( self , request . host_url ) #NEWLINE# res . content_type = 'application/json' #NEWLINE# return res #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# msg = None #NEWLINE# error_status = 500 #NEWLINE# protocol = self . _select_protocol ( request ) #NEWLINE# #UNINDENT# except ClientSideError as e : #NEWLINE# #INDENT# error_status = e . code #NEWLINE# msg = e . faultstring #NEWLINE# protocol = None #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# msg = ( 'Unexpected error while selecting protocol: %s' % str ( e ) ) #NEWLINE# log . exception ( msg ) #NEWLINE# protocol = None #NEWLINE# error_status = 500 #NEWLINE# #UNINDENT# if ( protocol is None ) : #NEWLINE# #INDENT# if ( not msg ) : #NEWLINE# #INDENT# msg = ( 'None of the following protocols can handle this request : %s' % ',' . join ( ( p . name for p in self . protocols ) ) ) #NEWLINE# #UNINDENT# res . status = error_status #NEWLINE# res . content_type = 'text/plain' #NEWLINE# try : #NEWLINE# #INDENT# res . text = u ( msg ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# res . text = msg #NEWLINE# #UNINDENT# log . error ( msg ) #NEWLINE# return res #NEWLINE# #UNINDENT# request . calls = [ ] #NEWLINE# request . client_errorcount = 0 #NEWLINE# request . client_last_status_code = None #NEWLINE# request . server_errorcount = 0 #NEWLINE# try : #NEWLINE# #INDENT# context = None #NEWLINE# if hasattr ( protocol , 'prepare_response_body' ) : #NEWLINE# #INDENT# prepare_response_body = protocol . prepare_response_body #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# prepare_response_body = default_prepare_response_body #NEWLINE# #UNINDENT# body = prepare_response_body ( request , ( self . _do_call ( protocol , context ) for context in protocol . iter_calls ( request ) ) ) #NEWLINE# if isinstance ( body , six . text_type ) : #NEWLINE# #INDENT# res . text = body #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# res . body = body #NEWLINE# #UNINDENT# if ( len ( request . calls ) == 1 ) : #NEWLINE# #INDENT# if hasattr ( protocol , 'get_response_status' ) : #NEWLINE# #INDENT# res . status = protocol . get_response_status ( request ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( request . client_errorcount == 1 ) : #NEWLINE# #INDENT# res . status = request . client_last_status_code #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if request . client_errorcount : #NEWLINE# #INDENT# res . status = 400 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if request . server_errorcount : #NEWLINE# #INDENT# res . status = 500 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# res . status = 200 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# res . status = protocol . get_response_status ( request ) #NEWLINE# res_content_type = protocol . get_response_contenttype ( request ) #NEWLINE# #UNINDENT# #UNINDENT# except ClientSideError as e : #NEWLINE# #INDENT# request . server_errorcount += 1 #NEWLINE# res . status = e . code #NEWLINE# res . text = e . faultstring #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# infos = wsme . api . format_exception ( sys . exc_info ( ) , self . _debug ) #NEWLINE# request . server_errorcount += 1 #NEWLINE# res . text = protocol . encode_error ( context , infos ) #NEWLINE# res . status = 500 #NEWLINE# #UNINDENT# if ( res_content_type is None ) : #NEWLINE# #INDENT# ctypes = [ ct for ct in protocol . content_types if ct ] #NEWLINE# if ctypes : #NEWLINE# #INDENT# res_content_type = request . accept . best_match ( ctypes ) #NEWLINE# #UNINDENT# #UNINDENT# if ( res_content_type is None ) : #NEWLINE# #INDENT# if ( 'text/html' in request . accept ) : #NEWLINE# #INDENT# res . text = self . _html_format ( res . body , protocol . content_types ) #NEWLINE# res_content_type = 'text/html' #NEWLINE# #UNINDENT# #UNINDENT# res . headers [ 'Content-Type' ] = ( '%s; charset=UTF-8' % res_content_type ) #NEWLINE# return res