def RunCommand(self): cors_arg = self.args[0] uri_args = self.args[1:] # Disallow multi-provider setcors requests. storage_uri = self.UrisAreForSingleProvider(uri_args) if not storage_uri: raise CommandException( '"%s" command spanning providers not allowed.' % self.command_name) # Open, read and parse file containing XML document. cors_file = open(cors_arg, 'r') cors_txt = cors_file.read() cors_file.close() cors_obj = Cors() # Parse XML document and convert into Cors object. h = handler.XmlHandler(cors_obj, None) try: xml.sax.parseString(cors_txt, h) except xml.sax._exceptions.SAXParseException, e: raise CommandException( 'Requested CORS is invalid: %s at line %s, ' 'column %s' % (e.getMessage(), e.getLineNumber(), e.getColumnNumber()))
def get_cors(self, headers=None): """returns a bucket's CORS XML""" response = self.connection.make_request('GET', self.name, query_args=CORS_ARG, headers=headers) body = response.read() if response.status == 200: # Success - parse XML and return Cors object. cors = Cors() h = handler.XmlHandler(cors, self) xml.sax.parseString(body, h) return cors else: raise self.connection.provider.storage_response_error( response.status, response.reason, body)
def test_cors_xml_storage_uri(self): """Test setting and getting of CORS XML documents with storage_uri.""" # create a new bucket bucket = self._MakeBucket() bucket_name = bucket.name uri = storage_uri('gs://' + bucket_name) # get new bucket cors and make sure it's empty cors = re.sub(r'\s', '', uri.get_cors().to_xml()) self.assertEqual(cors, CORS_EMPTY) # set cors document on new bucket cors_obj = Cors() h = handler.XmlHandler(cors_obj, None) xml.sax.parseString(CORS_DOC, h) uri.set_cors(cors_obj) cors = re.sub(r'\s', '', uri.get_cors().to_xml()) self.assertEqual(cors, CORS_DOC)
def test_4_cors_xml(self): """test setting and getting of CORS XML documents""" # regexp for matching project-private default object ACL cors_empty = '<CorsConfig></CorsConfig>' cors_doc = ('<CorsConfig><Cors><Origins><Origin>origin1.example.com' '</Origin><Origin>origin2.example.com</Origin></Origins>' '<Methods><Method>GET</Method><Method>PUT</Method>' '<Method>POST</Method></Methods><ResponseHeaders>' '<ResponseHeader>foo</ResponseHeader>' '<ResponseHeader>bar</ResponseHeader></ResponseHeaders>' '</Cors></CorsConfig>') c = GSConnection() # create a new bucket bucket_name = 'test-%d' % int(time.time()) bucket = c.create_bucket(bucket_name) # now call get_bucket to see if it's really there bucket = c.get_bucket(bucket_name) # get new bucket cors and make sure it's empty cors = re.sub(r'\s', '', bucket.get_cors().to_xml()) assert cors == cors_empty # set cors document on new bucket bucket.set_cors(cors_doc) cors = re.sub(r'\s', '', bucket.get_cors().to_xml()) assert cors == cors_doc # delete bucket c.delete_bucket(bucket) # repeat cors tests using boto's storage_uri interface # create a new bucket bucket_name = 'test-%d' % int(time.time()) uri = storage_uri('gs://' + bucket_name) uri.create_bucket() # get new bucket cors and make sure it's empty cors = re.sub(r'\s', '', uri.get_cors().to_xml()) assert cors == cors_empty # set cors document on new bucket cors_obj = Cors() h = handler.XmlHandler(cors_obj, None) xml.sax.parseString(cors_doc, h) uri.set_cors(cors_obj) cors = re.sub(r'\s', '', uri.get_cors().to_xml()) assert cors == cors_doc # delete bucket uri.delete_bucket() print '--- tests completed ---'
def get_cors(self, headers=None): """Returns a bucket's CORS XML document. :param dict headers: Additional headers to send with the request. :rtype: :class:`~.cors.Cors` """ response = self.connection.make_request('GET', self.name, query_args=CORS_ARG, headers=headers) body = response.read() if response.status == 200: # Success - parse XML and return Cors object. cors = Cors() h = handler.XmlHandler(cors, self) xml.sax.parseString(body, h) return cors else: raise self.connection.provider.storage_response_error( response.status, response.reason, body)