示例#1
0
    def __call__(self, req):
        """Generate a WSGI response based on the exception passed to ctor."""
        code = self.wrapped_exc.status_int
        message = self.wrapped_exc.explanation

        if code == 501:
            message = "The requested function is not supported"
        code = str(code)

        if 'AWSAccessKeyId' not in req.params:
            raise webob.exc.HTTPBadRequest()
        user_id, _sep, project_id = req.params['AWSAccessKeyId'].partition(':')
        project_id = project_id or user_id
        remote_address = getattr(req, 'remote_address', '127.0.0.1')
        if FLAGS.use_forwarded_for:
            remote_address = req.headers.get('X-Forwarded-For', remote_address)

        ctxt = context.RequestContext(user_id,
                                      project_id,
                                      remote_address=remote_address)

        resp = webob.Response()
        resp.status = self.wrapped_exc.status_int
        resp.headers['Content-Type'] = 'text/xml'
        resp.body = str('<?xml version="1.0"?>\n'
                         '<Response><Errors><Error><Code>%s</Code>'
                         '<Message>%s</Message></Error></Errors>'
                         '<RequestID>%s</RequestID></Response>' %
                         (utils.xhtml_escape(utils.utf8(code)),
                          utils.xhtml_escape(utils.utf8(message)),
                          utils.xhtml_escape(utils.utf8(ctxt.request_id))))

        return resp
    def __call__(self, req):
        """Generate a WSGI response based on the exception passed to ctor."""
        code = self.wrapped_exc.status_int
        message = self.wrapped_exc.explanation

        if code == 501:
            message = "The requested function is not supported"
        code = str(code)

        if "AWSAccessKeyId" not in req.params:
            raise webob.exc.HTTPBadRequest()
        user_id, _sep, project_id = req.params["AWSAccessKeyId"].partition(":")
        project_id = project_id or user_id
        remote_address = getattr(req, "remote_address", "127.0.0.1")
        if CONF.use_forwarded_for:
            remote_address = req.headers.get("X-Forwarded-For", remote_address)

        ctxt = context.RequestContext(user_id, project_id, remote_address=remote_address)

        resp = webob.Response()
        resp.status = self.wrapped_exc.status_int
        resp.headers["Content-Type"] = "text/xml"
        resp.body = str(
            '<?xml version="1.0"?>\n'
            "<Response><Errors><Error><Code>%s</Code>"
            "<Message>%s</Message></Error></Errors>"
            "<RequestID>%s</RequestID></Response>"
            % (
                utils.xhtml_escape(utils.utf8(code)),
                utils.xhtml_escape(utils.utf8(message)),
                utils.xhtml_escape(utils.utf8(ctxt.request_id)),
            )
        )

        return resp
示例#3
0
文件: __init__.py 项目: isolosun/nova
def ec2_error(req, request_id, code, message):
    """Helper to send an ec2_compatible error"""
    LOG.error(_('%(code)s: %(message)s') % locals())
    resp = webob.Response()
    resp.status = 400
    resp.headers['Content-Type'] = 'text/xml'
    resp.body = str('<?xml version="1.0"?>\n'
                     '<Response><Errors><Error><Code>%s</Code>'
                     '<Message>%s</Message></Error></Errors>'
                     '<RequestID>%s</RequestID></Response>' %
                     (utils.xhtml_escape(utils.utf8(code)),
                      utils.xhtml_escape(utils.utf8(message)),
                      utils.xhtml_escape(utils.utf8(request_id))))
    return resp
示例#4
0
文件: faults.py 项目: DrZaarlon/nova
def ec2_error_response(request_id, code, message, status=500):
    """Helper to construct an EC2 compatible error reposne."""
    LOG.debug(_('EC2 error response: %(code)s: %(message)s') %
                {'code': code, 'message': message})
    resp = webob.Response()
    resp.status = status
    resp.headers['Content-Type'] = 'text/xml'
    resp.body = str('<?xml version="1.0"?>\n'
                    '<Response><Errors><Error><Code>%s</Code>'
                    '<Message>%s</Message></Error></Errors>'
                    '<RequestID>%s</RequestID></Response>' %
                    (utils.xhtml_escape(utils.utf8(code)),
                     utils.xhtml_escape(utils.utf8(message)),
                     utils.xhtml_escape(utils.utf8(request_id))))
    return resp
示例#5
0
def ec2_error_response(request_id, code, message, status=500):
    """Helper to construct an EC2 compatible error response."""
    LOG.debug('EC2 error response: %(code)s: %(message)s',
              {'code': code, 'message': message})
    resp = webob.Response()
    resp.status = status
    resp.headers['Content-Type'] = 'text/xml'
    resp.body = str('<?xml version="1.0"?>\n'
                    '<Response><Errors><Error><Code>%s</Code>'
                    '<Message>%s</Message></Error></Errors>'
                    '<RequestID>%s</RequestID></Response>' %
                    (utils.xhtml_escape(utils.utf8(code)),
                     utils.xhtml_escape(utils.utf8(message)),
                     utils.xhtml_escape(utils.utf8(request_id))))
    return resp
示例#6
0
文件: faults.py 项目: jorgevgut/nova
def ec2_error_response(request_id, code, message, status=500):
    """Helper to construct an EC2 compatible error response."""
    LOG.debug("EC2 error response: %(code)s: %(message)s", {"code": code, "message": message})
    resp = webob.Response()
    resp.status = status
    resp.headers["Content-Type"] = "text/xml"
    resp.body = str(
        '<?xml version="1.0"?>\n'
        "<Response><Errors><Error><Code>%s</Code>"
        "<Message>%s</Message></Error></Errors>"
        "<RequestID>%s</RequestID></Response>"
        % (
            utils.xhtml_escape(utils.utf8(code)),
            utils.xhtml_escape(utils.utf8(message)),
            utils.xhtml_escape(utils.utf8(request_id)),
        )
    )
    return resp
示例#7
0
def ec2_error(req, request_id, code, message):
    """Helper to send an ec2_compatible error."""
    LOG.error(_("%(code)s: %(message)s"), {"code": code, "message": message})
    resp = webob.Response()
    resp.status = 400
    resp.headers["Content-Type"] = "text/xml"
    resp.body = str(
        '<?xml version="1.0"?>\n'
        "<Response><Errors><Error><Code>%s</Code>"
        "<Message>%s</Message></Error></Errors>"
        "<RequestID>%s</RequestID></Response>"
        % (
            utils.xhtml_escape(utils.utf8(code)),
            utils.xhtml_escape(utils.utf8(message)),
            utils.xhtml_escape(utils.utf8(request_id)),
        )
    )
    return resp
示例#8
0
 def test_xhtml_escape(self):
     self.assertEqual("&quot;foo&quot;", utils.xhtml_escape('"foo"'))
     self.assertEqual("&apos;foo&apos;", utils.xhtml_escape("'foo'"))
     self.assertEqual("&amp;", utils.xhtml_escape("&"))
     self.assertEqual("&gt;", utils.xhtml_escape(">"))
     self.assertEqual("&lt;", utils.xhtml_escape("<"))
     self.assertEqual("&lt;foo&gt;", utils.xhtml_escape("<foo>"))
示例#9
0
 def test_xhtml_escape(self):
     self.assertEqual('&quot;foo&quot;', utils.xhtml_escape('"foo"'))
     self.assertEqual('&apos;foo&apos;', utils.xhtml_escape("'foo'"))
     self.assertEqual('&amp;', utils.xhtml_escape('&'))
     self.assertEqual('&gt;', utils.xhtml_escape('>'))
     self.assertEqual('&lt;', utils.xhtml_escape('<'))
     self.assertEqual('&lt;foo&gt;', utils.xhtml_escape('<foo>'))
示例#10
0
 def test_xhtml_escape(self):
     self.assertEqual('&quot;foo&quot;', utils.xhtml_escape('"foo"'))
     self.assertEqual('&apos;foo&apos;', utils.xhtml_escape("'foo'"))
     self.assertEqual('&amp;', utils.xhtml_escape('&'))
     self.assertEqual('&gt;', utils.xhtml_escape('>'))
     self.assertEqual('&lt;', utils.xhtml_escape('<'))
     self.assertEqual('&lt;foo&gt;', utils.xhtml_escape('<foo>'))
示例#11
0
 def _render_parts(self, value, parts=[]):
     if isinstance(value, basestring):
         parts.append(utils.xhtml_escape(value))
     elif isinstance(value, int) or isinstance(value, long):
         parts.append(str(value))
     elif isinstance(value, datetime.datetime):
         parts.append(value.strftime("%Y-%m-%dT%H:%M:%S.000Z"))
     elif isinstance(value, dict):
         for name, subvalue in value.iteritems():
             if not isinstance(subvalue, list):
                 subvalue = [subvalue]
             for subsubvalue in subvalue:
                 parts.append('<' + utils.utf8(name) + '>')
                 self._render_parts(subsubvalue, parts)
                 parts.append('</' + utils.utf8(name) + '>')
     else:
         raise Exception("Unknown S3 value type %r", value)
示例#12
0
 def _render_parts(self, value, parts=[]):
     if isinstance(value, basestring):
         parts.append(utils.xhtml_escape(value))
     elif isinstance(value, int) or isinstance(value, long):
         parts.append(str(value))
     elif isinstance(value, datetime.datetime):
         parts.append(value.strftime("%Y-%m-%dT%H:%M:%S.000Z"))
     elif isinstance(value, dict):
         for name, subvalue in value.iteritems():
             if not isinstance(subvalue, list):
                 subvalue = [subvalue]
             for subsubvalue in subvalue:
                 parts.append('<' + utils.utf8(name) + '>')
                 self._render_parts(subsubvalue, parts)
                 parts.append('</' + utils.utf8(name) + '>')
     else:
         raise Exception("Unknown S3 value type %r", value)
示例#13
0
def _render_parts(value, write_cb):
    """Helper method to render different Python objects to XML"""
    if isinstance(value, basestring):
        write_cb(utils.xhtml_escape(value))
    elif isinstance(value, int) or isinstance(value, long):
        write_cb(str(value))
    elif isinstance(value, datetime.datetime):
        write_cb(value.strftime("%Y-%m-%dT%H:%M:%S.000Z"))
    elif isinstance(value, dict):
        for name, subvalue in value.iteritems():
            if not isinstance(subvalue, list):
                subvalue = [subvalue]
            for subsubvalue in subvalue:
                write_cb('<' + utils.utf8(name) + '>')
                _render_parts(subsubvalue, write_cb)
                write_cb('</' + utils.utf8(name) + '>')
    else:
        raise Exception(_("Unknown S3 value type %r"), value)
示例#14
0
def _render_parts(value, write_cb):
    """Helper method to render different Python objects to XML"""
    if isinstance(value, basestring):
        write_cb(utils.xhtml_escape(value))
    elif isinstance(value, int) or isinstance(value, long):
        write_cb(str(value))
    elif isinstance(value, datetime.datetime):
        write_cb(value.strftime("%Y-%m-%dT%H:%M:%S.000Z"))
    elif isinstance(value, dict):
        for name, subvalue in value.iteritems():
            if not isinstance(subvalue, list):
                subvalue = [subvalue]
            for subsubvalue in subvalue:
                write_cb('<' + utils.utf8(name) + '>')
                _render_parts(subsubvalue, write_cb)
                write_cb('</' + utils.utf8(name) + '>')
    else:
        raise Exception(_("Unknown S3 value type %r"), value)
示例#15
0
    def _render_parts(self, value, parts=None):
        if not parts:
            parts = []

        if isinstance(value, six.string_types):
            parts.append(utils.xhtml_escape(value))
        elif type(value) in six.integer_types:
            parts.append(str(value))
        elif isinstance(value, bool):
            parts.append(str(value))
        elif isinstance(value, datetime.datetime):
            parts.append(value.strftime("%Y-%m-%dT%H:%M:%S.000Z"))
        elif isinstance(value, dict):
            for name, subvalue in six.iteritems(value):
                if not isinstance(subvalue, list):
                    subvalue = [subvalue]
                for subsubvalue in subvalue:
                    parts.append("<" + utils.utf8(name) + ">")
                    self._render_parts(subsubvalue, parts)
                    parts.append("</" + utils.utf8(name) + ">")
        else:
            raise Exception("Unknown S3 value type %r", value)
示例#16
0
 def test_xhtml_escape(self):
     self.assertEqual('&quot;foo&quot;', utils.xhtml_escape('"foo"'))
     self.assertEqual('&apos;foo&apos;', utils.xhtml_escape("'foo'"))
示例#17
0
文件: test_utils.py 项目: scpham/nova
 def test_xhtml_escape(self):
     self.assertEqual('&quot;foo&quot;', utils.xhtml_escape('"foo"'))
     self.assertEqual('&apos;foo&apos;', utils.xhtml_escape("'foo'"))