def _handle_request(self, request):
        res = webob.Response()
        res_content_type = None

        path = request.path
        if path.startswith(self._webpath):
            path = path[len(self._webpath):]
        routepath, func = self.find_route(path)
        if routepath:
            content = func()
            if isinstance(content, six.text_type):
                res.text = content
            elif isinstance(content, six.binary_type):
                res.body = content
            res.content_type = func._cfg['content-type']
            return res

        if request.path == self._webpath + '/api.spore':
            res.body = spore.getdesc(self, request.host_url)
            res.content_type = 'application/json'
            return res

        try:
            msg = None
            error_status = 500
            protocol = self._select_protocol(request)
        except ClientSideError as e:
            error_status = e.code
            msg = e.faultstring
            protocol = None
        except Exception as e:
            msg = ("Unexpected error while selecting protocol: %s" % str(e))
            log.exception(msg)
            protocol = None
            error_status = 500

        if protocol is None:
            if not msg:
                msg = ("None of the following protocols can handle this "
                       "request : %s" % ','.join((
                           p.name for p in self.protocols)))
            res.status = error_status
            res.content_type = 'text/plain'
            try:
                res.text = u(msg)
            except __HOLE__:
                res.text = msg
            log.error(msg)
            return res

        request.calls = []
        request.client_errorcount = 0
        request.client_last_status_code = None
        request.server_errorcount = 0

        try:

            context = None

            if hasattr(protocol, 'prepare_response_body'):
                prepare_response_body = protocol.prepare_response_body
            else:
                prepare_response_body = default_prepare_response_body

            body = prepare_response_body(request, (
                self._do_call(protocol, context)
                for context in protocol.iter_calls(request)))

            if isinstance(body, six.text_type):
                res.text = body
            else:
                res.body = body

            if len(request.calls) == 1:
                if hasattr(protocol, 'get_response_status'):
                    res.status = protocol.get_response_status(request)
                else:
                    if request.client_errorcount == 1:
                        res.status = request.client_last_status_code
                    elif request.client_errorcount:
                        res.status = 400
                    elif request.server_errorcount:
                        res.status = 500
                    else:
                        res.status = 200
            else:
                res.status = protocol.get_response_status(request)
                res_content_type = protocol.get_response_contenttype(request)
        except ClientSideError as e:
            request.server_errorcount += 1
            res.status = e.code
            res.text = e.faultstring
        except Exception:
            infos = wsme.api.format_exception(sys.exc_info(), self._debug)
            request.server_errorcount += 1
            res.text = protocol.encode_error(context, infos)
            res.status = 500

        if res_content_type is None:
            # Attempt to correctly guess what content-type we should return.
            ctypes = [ct for ct in protocol.content_types if ct]
            if ctypes:
                res_content_type = request.accept.best_match(ctypes)

        # If not we will attempt to convert the body to an accepted
        # output format.
        if res_content_type is None:
            if "text/html" in request.accept:
                res.text = self._html_format(res.body, protocol.content_types)
                res_content_type = "text/html"

        # TODO should we consider the encoding asked by
        # the web browser ?
        res.headers['Content-Type'] = "%s; charset=UTF-8" % res_content_type

        return res
dataset/ETHPy150Open openstack/wsme/wsme/root.py/WSRoot._handle_request