Browse code

* s3cmd, S3/S3.py, S3/Config.py: Removed --use-old-connect-method again. Autodetect the need for old connect method instead.

git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3cmd/trunk@226 830e0280-6d2a-0410-9c65-932aecc39d9d

Michal Ludvig authored on 2008/09/03 13:42:16
Showing 4 changed files
... ...
@@ -1,5 +1,10 @@
1 1
 2008-09-03  Michal Ludvig  <michal@logix.cz>
2 2
 
3
+	* s3cmd, S3/S3.py, S3/Config.py: Removed --use-old-connect-method
4
+	  again. Autodetect the need for old connect method instead.
5
+
6
+2008-09-03  Michal Ludvig  <michal@logix.cz>
7
+
3 8
 	* s3cmd, S3/S3.py: Make --verbose mode more useful and default 
4 9
 	  mode less verbose.
5 10
 
... ...
@@ -15,7 +15,6 @@ class Config(object):
15 15
 	secret_key = ""
16 16
 	host_base = "s3.amazonaws.com"
17 17
 	host_bucket = "%(bucket)s.s3.amazonaws.com"
18
-	use_old_connect_method = False
19 18
 	simpledb_host = "sdb.amazonaws.com"
20 19
 	verbosity = logging.WARNING
21 20
 	send_chunk = 4096
... ...
@@ -71,7 +71,7 @@ class S3(object):
71 71
 				return httplib.HTTPConnection(self.get_hostname(bucket))
72 72
 
73 73
 	def get_hostname(self, bucket):
74
-		if bucket and not Config().use_old_connect_method:
74
+		if bucket and self.check_bucket_name_dns_conformity(bucket):
75 75
 			if self.redir_map.has_key(bucket):
76 76
 				host = self.redir_map[bucket]
77 77
 			else:
... ...
@@ -85,7 +85,7 @@ class S3(object):
85 85
 		self.redir_map[bucket] = redir_hostname
86 86
 
87 87
 	def format_uri(self, resource):
88
-		if resource['bucket'] and Config().use_old_connect_method:
88
+		if resource['bucket'] and not self.check_bucket_name_dns_conformity(resource['bucket']):
89 89
 			uri = "/%s%s" % (resource['bucket'], resource['uri'])
90 90
 		else:
91 91
 			uri = resource['uri']
... ...
@@ -480,13 +480,35 @@ class S3(object):
480 480
 		debug("SignHeaders: " + repr(h))
481 481
 		return base64.encodestring(hmac.new(self.config.secret_key, h, sha).digest()).strip()
482 482
 
483
-	def check_bucket_name(self, bucket):
484
-		invalid = re.compile("([^a-z0-9\._-])").search(bucket)
485
-		if invalid:
486
-			raise ParameterError("Bucket name '%s' contains disallowed character '%s'. The only supported ones are: lowercase us-ascii letters (a-z), digits (0-9), dot (.), hyphen (-) and underscore (_)." % (bucket, invalid.groups()[0]))
483
+	def check_bucket_name(self, bucket, dns_strict = True):
484
+		if dns_strict:
485
+			invalid = re.search("([^a-z0-9\.-])", bucket)
486
+			if invalid:
487
+				raise ParameterError("Bucket name '%s' contains disallowed character '%s'. The only supported ones are: lowercase us-ascii letters (a-z), digits (0-9), dot (.) and hyphen (-)." % (bucket, invalid.groups()[0]))
488
+		else:
489
+			invalid = re.search("([^A-Za-z0-9\._-])", bucket)
490
+			if invalid:
491
+				raise ParameterError("Bucket name '%s' contains disallowed character '%s'. The only supported ones are: us-ascii letters (a-z, A-Z), digits (0-9), dot (.), hyphen (-) and underscore (_)." % (bucket, invalid.groups()[0]))
492
+
487 493
 		if len(bucket) < 3:
488 494
 			raise ParameterError("Bucket name '%s' is too short (min 3 characters)" % bucket)
489 495
 		if len(bucket) > 255:
490 496
 			raise ParameterError("Bucket name '%s' is too long (max 255 characters)" % bucket)
497
+		if dns_strict:
498
+			if len(bucket) > 63:
499
+				raise ParameterError("Bucket name '%s' is too long (max 63 characters)" % bucket)
500
+			if re.search("-\.", bucket):
501
+				raise ParameterError("Bucket name '%s' must not contain sequence '-.' for DNS compatibility" % bucket)
502
+			if re.search("\.\.", bucket):
503
+				raise ParameterError("Bucket name '%s' must not contain sequence '..' for DNS compatibility" % bucket)
504
+			if not re.search("^[0-9a-z]", bucket):
505
+				raise ParameterError("Bucket name '%s' must start with a letter or a digit" % bucket)
506
+			if not re.search("[0-9a-z]$", bucket):
507
+				raise ParameterError("Bucket name '%s' must end with a letter or a digit" % bucket)
491 508
 		return True
492 509
 
510
+	def check_bucket_name_dns_conformity(self, bucket):
511
+		try:
512
+			return self.check_bucket_name(bucket, dns_strict = True)
513
+		except ParameterError:
514
+			return False
... ...
@@ -942,7 +942,6 @@ def main():
942 942
 	optparser.add_option("-H", "--human-readable-sizes", dest="human_readable_sizes", action="store_true", help="Print sizes in human readable form.")
943 943
 
944 944
 	optparser.add_option("-v", "--verbose", dest="verbosity", action="store_const", const=logging.INFO, help="Enable verbose output.")
945
-	optparser.add_option(      "--use-old-connect-method", dest="use_old_connect_method", action="store_true", help="Use deprecated method for connection to S3. Allows for upper-case bucket names but doesn't allow for buckets in Europe")
946 945
 	optparser.add_option("-d", "--debug", dest="verbosity", action="store_const", const=logging.DEBUG, help="Enable debug output.")
947 946
 	optparser.add_option(      "--version", dest="show_version", action="store_true", help="Show s3cmd version (%s) and exit." % (PkgInfo.version))
948 947