Browse code

2007-07-05 Michal Ludvig <michal@logix.cz>

* S3/S3.py: HTTP proxy support from
John D. Rowell <jdrowell@exerciseyourbrain.com>



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

Michal Ludvig authored on 2007/07/05 11:54:05
Showing 3 changed files
... ...
@@ -1,3 +1,8 @@
1
+2007-07-05  Michal Ludvig  <michal@logix.cz>
2
+
3
+	* S3/S3.py: HTTP proxy support from
4
+	  John D. Rowell <jdrowell@exerciseyourbrain.com>
5
+
1 6
 2007-06-19  Michal Ludvig  <michal@logix.cz>
2 7
 
3 8
 	* setup.py: Check for S3CMD_PACKAGING and don't install
... ...
@@ -19,6 +19,8 @@ class Config(object):
19 19
 	human_readable_sizes = False
20 20
 	force = False
21 21
 	acl_public = False
22
+	proxy_host = ""
23
+	proxy_port = 0
22 24
 	encrypt = False
23 25
 	gpg_passphrase = ""
24 26
 	gpg_command = ""
... ...
@@ -83,6 +83,18 @@ class S3(object):
83 83
 	def __init__(self, config):
84 84
 		self.config = config
85 85
 
86
+	def get_connection(self):
87
+		if self.config.proxy_host != "":
88
+			return httplib.HTTPConnection(self.config.proxy_host, self.config.proxy_port)
89
+		else:
90
+			return httplib.HTTPConnection(self.config.host)
91
+
92
+	def format_resource(self, resource):
93
+		if self.config.proxy_host != "":
94
+			resource = "http://%s%s" % (self.config.host, resource)
95
+
96
+		return resource
97
+
86 98
 	## Commands / Actions
87 99
 	def list_all_buckets(self):
88 100
 		request = self.create_request("LIST_ALL_BUCKETS")
... ...
@@ -100,7 +112,9 @@ class S3(object):
100 100
 
101 101
 	def bucket_create(self, bucket):
102 102
 		self.check_bucket_name(bucket)
103
-		request = self.create_request("BUCKET_CREATE", bucket = bucket)
103
+		headers = SortedDict()
104
+		headers["content-length"] = 0
105
+		request = self.create_request("BUCKET_CREATE", bucket = bucket, headers = headers)
104 106
 		response = self.send_request(request)
105 107
 		return response
106 108
 
... ...
@@ -197,8 +211,8 @@ class S3(object):
197 197
 	def send_request(self, request):
198 198
 		method_string, resource, headers = request
199 199
 		info("Processing request, please wait...")
200
-		conn = httplib.HTTPConnection(self.config.host)
201
-		conn.request(method_string, resource, {}, headers)
200
+ 		conn = self.get_connection()
201
+ 		conn.request(method_string, self.format_resource(resource), {}, headers)
202 202
 		response = {}
203 203
 		http_response = conn.getresponse()
204 204
 		response["status"] = http_response.status
... ...
@@ -213,9 +227,9 @@ class S3(object):
213 213
 	def send_file(self, request, file):
214 214
 		method_string, resource, headers = request
215 215
 		info("Sending file '%s', please wait..." % file.name)
216
-		conn = httplib.HTTPConnection(self.config.host)
216
+		conn = self.get_connection()
217 217
 		conn.connect()
218
-		conn.putrequest(method_string, resource)
218
+		conn.putrequest(method_string, self.format_resource(resource))
219 219
 		for header in headers.keys():
220 220
 			conn.putheader(header, str(headers[header]))
221 221
 		conn.endheaders()
... ...
@@ -244,9 +258,9 @@ class S3(object):
244 244
 	def recv_file(self, request, stream):
245 245
 		method_string, resource, headers = request
246 246
 		info("Receiving file '%s', please wait..." % stream.name)
247
-		conn = httplib.HTTPConnection(self.config.host)
247
+		conn = self.get_connection()
248 248
 		conn.connect()
249
-		conn.putrequest(method_string, resource)
249
+		conn.putrequest(method_string, self.format_resource(resource))
250 250
 		for header in headers.keys():
251 251
 			conn.putheader(header, str(headers[header]))
252 252
 		conn.endheaders()