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
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
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
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
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
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
def test_xhtml_escape(self): self.assertEqual(""foo"", utils.xhtml_escape('"foo"')) self.assertEqual("'foo'", utils.xhtml_escape("'foo'")) self.assertEqual("&", utils.xhtml_escape("&")) self.assertEqual(">", utils.xhtml_escape(">")) self.assertEqual("<", utils.xhtml_escape("<")) self.assertEqual("<foo>", utils.xhtml_escape("<foo>"))
def test_xhtml_escape(self): self.assertEqual('"foo"', utils.xhtml_escape('"foo"')) self.assertEqual(''foo'', utils.xhtml_escape("'foo'")) self.assertEqual('&', utils.xhtml_escape('&')) self.assertEqual('>', utils.xhtml_escape('>')) self.assertEqual('<', utils.xhtml_escape('<')) self.assertEqual('<foo>', utils.xhtml_escape('<foo>'))
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)
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)
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)
def test_xhtml_escape(self): self.assertEqual('"foo"', utils.xhtml_escape('"foo"')) self.assertEqual(''foo'', utils.xhtml_escape("'foo'"))