| OLD | NEW |
| (Empty) |
| 1 # Copyright 2012 Google Inc. All Rights Reserved. | |
| 2 # | |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 # you may not use this file except in compliance with the License. | |
| 5 # You may obtain a copy of the License at | |
| 6 # | |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 # | |
| 9 # Unless required by applicable law or agreed to in writing, software | |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 # See the License for the specific language governing permissions and | |
| 13 # limitations under the License. | |
| 14 | |
| 15 import xml.sax | |
| 16 from boto import handler | |
| 17 from boto.gs.cors import Cors | |
| 18 from gslib.command import Command | |
| 19 from gslib.command import COMMAND_NAME | |
| 20 from gslib.command import COMMAND_NAME_ALIASES | |
| 21 from gslib.command import CONFIG_REQUIRED | |
| 22 from gslib.command import FILE_URIS_OK | |
| 23 from gslib.command import MAX_ARGS | |
| 24 from gslib.command import MIN_ARGS | |
| 25 from gslib.command import PROVIDER_URIS_OK | |
| 26 from gslib.command import SUPPORTED_SUB_ARGS | |
| 27 from gslib.command import URIS_START_ARG | |
| 28 from gslib.exception import CommandException | |
| 29 from gslib.help_provider import HELP_NAME | |
| 30 from gslib.help_provider import HELP_NAME_ALIASES | |
| 31 from gslib.help_provider import HELP_ONE_LINE_SUMMARY | |
| 32 from gslib.help_provider import HELP_TEXT | |
| 33 from gslib.help_provider import HelpType | |
| 34 from gslib.help_provider import HELP_TYPE | |
| 35 from gslib.util import NO_MAX | |
| 36 | |
| 37 _detailed_help_text = (""" | |
| 38 <B>SYNOPSIS</B> | |
| 39 gsutil setcors cors-xml-file uri... | |
| 40 | |
| 41 | |
| 42 <B>DESCRIPTION</B> | |
| 43 Sets the Cross-Origin Resource Sharing (CORS) configuration on one or more | |
| 44 buckets. This command is supported for buckets only, not objects. The | |
| 45 cors-xml-file specified on the command line should be a path to a local | |
| 46 file containing an XML document with the following structure: | |
| 47 | |
| 48 <?xml version="1.0" ?> | |
| 49 <CorsConfig> | |
| 50 <Cors> | |
| 51 <Origins> | |
| 52 <Origin>http://origin1.example.com</Origin> | |
| 53 </Origins> | |
| 54 <Methods> | |
| 55 <Method>GET</Method> | |
| 56 </Methods> | |
| 57 <ResponseHeaders> | |
| 58 <ResponseHeader>Content-Type</ResponseHeader> | |
| 59 </ResponseHeaders> | |
| 60 </Cors> | |
| 61 </CorsConfig> | |
| 62 | |
| 63 The above XML document explicitly allows cross-origin GET requests from | |
| 64 http://origin1.example.com and may include the Content-Type response header. | |
| 65 | |
| 66 For more info about CORS, see http://www.w3.org/TR/cors/. | |
| 67 """) | |
| 68 | |
| 69 class SetCorsCommand(Command): | |
| 70 """Implementation of gsutil setcors command.""" | |
| 71 | |
| 72 # Command specification (processed by parent class). | |
| 73 command_spec = { | |
| 74 # Name of command. | |
| 75 COMMAND_NAME : 'setcors', | |
| 76 # List of command name aliases. | |
| 77 COMMAND_NAME_ALIASES : [], | |
| 78 # Min number of args required by this command. | |
| 79 MIN_ARGS : 2, | |
| 80 # Max number of args required by this command, or NO_MAX. | |
| 81 MAX_ARGS : NO_MAX, | |
| 82 # Getopt-style string specifying acceptable sub args. | |
| 83 SUPPORTED_SUB_ARGS : '', | |
| 84 # True if file URIs acceptable for this command. | |
| 85 FILE_URIS_OK : False, | |
| 86 # True if provider-only URIs acceptable for this command. | |
| 87 PROVIDER_URIS_OK : False, | |
| 88 # Index in args of first URI arg. | |
| 89 URIS_START_ARG : 1, | |
| 90 # True if must configure gsutil before running command. | |
| 91 CONFIG_REQUIRED : True, | |
| 92 } | |
| 93 help_spec = { | |
| 94 # Name of command or auxiliary help info for which this help applies. | |
| 95 HELP_NAME : 'setcors', | |
| 96 # List of help name aliases. | |
| 97 HELP_NAME_ALIASES : ['cors', 'cross-origin'], | |
| 98 # Type of help) | |
| 99 HELP_TYPE : HelpType.COMMAND_HELP, | |
| 100 # One line summary of this help. | |
| 101 HELP_ONE_LINE_SUMMARY : 'Set a CORS XML document for one or more buckets', | |
| 102 # The full help text. | |
| 103 HELP_TEXT : _detailed_help_text, | |
| 104 } | |
| 105 | |
| 106 # Command entry point. | |
| 107 def RunCommand(self): | |
| 108 cors_arg = self.args[0] | |
| 109 uri_args = self.args[1:] | |
| 110 # Disallow multi-provider setcors requests. | |
| 111 storage_uri = self.UrisAreForSingleProvider(uri_args) | |
| 112 if not storage_uri: | |
| 113 raise CommandException('"%s" command spanning providers not allowed.' % | |
| 114 self.command_name) | |
| 115 | |
| 116 # Open, read and parse file containing XML document. | |
| 117 cors_file = open(cors_arg, 'r') | |
| 118 cors_txt = cors_file.read() | |
| 119 cors_file.close() | |
| 120 cors_obj = Cors() | |
| 121 | |
| 122 # Parse XML document and convert into Cors object. | |
| 123 h = handler.XmlHandler(cors_obj, None) | |
| 124 try: | |
| 125 xml.sax.parseString(cors_txt, h) | |
| 126 except xml.sax._exceptions.SAXParseException, e: | |
| 127 raise CommandException('Requested CORS is invalid: %s at line %s, ' | |
| 128 'column %s' % (e.getMessage(), e.getLineNumber(), | |
| 129 e.getColumnNumber())) | |
| 130 | |
| 131 # Iterate over URIs, expanding wildcards, and setting the CORS on each. | |
| 132 some_matched = False | |
| 133 for uri_str in uri_args: | |
| 134 for blr in self.WildcardIterator(uri_str): | |
| 135 uri = blr.GetUri() | |
| 136 if not uri.names_bucket(): | |
| 137 raise CommandException('URI %s must name a bucket for the %s command' | |
| 138 % (str(uri), self.command_name)) | |
| 139 some_matched = True | |
| 140 print 'Setting CORS on %s...' % uri | |
| 141 uri.set_cors(cors_obj, False, self.headers) | |
| 142 if not some_matched: | |
| 143 raise CommandException('No URIs matched') | |
| 144 | |
| 145 return 0 | |
| OLD | NEW |