Browse code

Add proxy support

Michal Ludvig authored on 2013/03/10 20:46:33
Showing 2 changed files
... ...
@@ -4,16 +4,19 @@ from threading import Semaphore
4 4
 from logging import debug, info, warning, error
5 5
 
6 6
 from Config import Config
7
+from Exceptions import ParameterError
7 8
 
8 9
 __all__ = [ "ConnMan" ]
9 10
 
10 11
 class http_connection(object):
11
-    def __init__(self, id, hostname, ssl):
12
+    def __init__(self, id, hostname, ssl, cfg):
12 13
         self.hostname = hostname
13 14
         self.ssl = ssl
14 15
         self.id = id
15 16
         self.counter = 0
16
-        if not ssl:
17
+        if cfg.proxy_host != "":
18
+            self.c = httplib.HTTPConnection(cfg.proxy_host, cfg.proxy_port)
19
+        elif not ssl:
17 20
             self.c = httplib.HTTPConnection(hostname)
18 21
         else:
19 22
             self.c = httplib.HTTPSConnection(hostname)
... ...
@@ -25,10 +28,16 @@ class ConnMan(object):
25 25
 
26 26
     @staticmethod
27 27
     def get(hostname, ssl = None):
28
+        cfg = Config()
28 29
         if ssl == None:
29
-            ssl = Config().use_https
30
+            ssl = cfg.use_https
30 31
         conn = None
31
-        conn_id = "http%s://%s" % (ssl and "s" or "", hostname)
32
+        if cfg.proxy_host != "":
33
+            if ssl:
34
+                raise ParameterError("use_ssl=True can't be used with proxy")
35
+            conn_id = "proxy://%s:%s" % (cfg.proxy_host, cfg.proxy_port)
36
+        else:
37
+            conn_id = "http%s://%s" % (ssl and "s" or "", hostname)
32 38
         ConnMan.conn_pool_sem.acquire()
33 39
         if not ConnMan.conn_pool.has_key(conn_id):
34 40
             ConnMan.conn_pool[conn_id] = []
... ...
@@ -38,13 +47,18 @@ class ConnMan(object):
38 38
         ConnMan.conn_pool_sem.release()
39 39
         if not conn:
40 40
             debug("ConnMan.get(): creating new connection: %s" % conn_id)
41
-            conn = http_connection(conn_id, hostname, ssl)
41
+            conn = http_connection(conn_id, hostname, ssl, cfg)
42 42
             conn.c.connect()
43 43
         conn.counter += 1
44 44
         return conn
45 45
 
46 46
     @staticmethod
47 47
     def put(conn):
48
+        if conn.id.startswith("proxy://"):
49
+            conn.c.close()
50
+            debug("ConnMan.put(): closing proxy connection (keep-alive not yet supported)")
51
+            return
52
+
48 53
         if conn.counter >= ConnMan.conn_max_counter:
49 54
             conn.c.close()
50 55
             debug("ConnMan.put(): closing over-used connection")
... ...
@@ -191,15 +191,6 @@ class S3(object):
191 191
     def __init__(self, config):
192 192
         self.config = config
193 193
 
194
-    def get_connection(self, bucket):
195
-        if self.config.proxy_host != "":
196
-            return httplib.HTTPConnection(self.config.proxy_host, self.config.proxy_port)
197
-        else:
198
-            if self.config.use_https:
199
-                return httplib.HTTPSConnection(self.get_hostname(bucket))
200
-            else:
201
-                return httplib.HTTPConnection(self.get_hostname(bucket))
202
-
203 194
     def get_hostname(self, bucket):
204 195
         if bucket and check_bucket_name_dns_conformity(bucket):
205 196
             if self.redir_map.has_key(bucket):
... ...
@@ -681,6 +672,8 @@ class S3(object):
681 681
             response["data"] =  http_response.read()
682 682
             debug("Response: " + str(response))
683 683
             ConnMan.put(conn)
684
+        except ParameterError, e:
685
+            raise
684 686
         except Exception, e:
685 687
             if retries:
686 688
                 warning("Retrying failed request: %s (%s)" % (resource['uri'], e))
... ...
@@ -728,6 +721,8 @@ class S3(object):
728 728
             for header in headers.keys():
729 729
                 conn.c.putheader(header, str(headers[header]))
730 730
             conn.c.endheaders()
731
+        except ParameterError, e:
732
+            raise
731 733
         except Exception, e:
732 734
             if self.config.progress_meter:
733 735
                 progress.done("failed")
... ...
@@ -766,6 +761,8 @@ class S3(object):
766 766
             response["size"] = size_total
767 767
             ConnMan.put(conn)
768 768
             debug(u"Response: %s" % response)
769
+        except ParameterError, e:
770
+            raise
769 771
         except Exception, e:
770 772
             if self.config.progress_meter:
771 773
                 progress.done("failed")
... ...
@@ -876,6 +873,8 @@ class S3(object):
876 876
             response["reason"] = http_response.reason
877 877
             response["headers"] = convertTupleListToDict(http_response.getheaders())
878 878
             debug("Response: %s" % response)
879
+        except ParameterError, e:
880
+            raise
879 881
         except Exception, e:
880 882
             if self.config.progress_meter:
881 883
                 progress.done("failed")