Skip to content
This repository
Browse code

Add proxy support

  • Loading branch information...
commit 8a3e46c2cfe7776d15c1e35d0e6d548b28898c43 1 parent e93378f
Michal Ludvig authored

Showing 2 changed files with 27 additions and 14 deletions. Show diff stats Hide diff stats

  1. 24  S3/ConnMan.py
  2. 17  S3/S3.py
24  S3/ConnMan.py
@@ -4,16 +4,19 @@
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 28
 
26 29
     @staticmethod
27 30
     def get(hostname, ssl = None):
  31
+        cfg = Config()
28 32
         if ssl == None:
29  
-            ssl = Config().use_https
  33
+            ssl = cfg.use_https
30 34
         conn = None
31  
-        conn_id = "http%s://%s" % (ssl and "s" or "", hostname)
  35
+        if cfg.proxy_host != "":
  36
+            if ssl:
  37
+                raise ParameterError("use_ssl=True can't be used with proxy")
  38
+            conn_id = "proxy://%s:%s" % (cfg.proxy_host, cfg.proxy_port)
  39
+        else:
  40
+            conn_id = "http%s://%s" % (ssl and "s" or "", hostname)
32 41
         ConnMan.conn_pool_sem.acquire()
33 42
         if not ConnMan.conn_pool.has_key(conn_id):
34 43
             ConnMan.conn_pool[conn_id] = []
@@ -38,13 +47,18 @@ def get(hostname, ssl = None):
38 47
         ConnMan.conn_pool_sem.release()
39 48
         if not conn:
40 49
             debug("ConnMan.get(): creating new connection: %s" % conn_id)
41  
-            conn = http_connection(conn_id, hostname, ssl)
  50
+            conn = http_connection(conn_id, hostname, ssl, cfg)
42 51
             conn.c.connect()
43 52
         conn.counter += 1
44 53
         return conn
45 54
 
46 55
     @staticmethod
47 56
     def put(conn):
  57
+        if conn.id.startswith("proxy://"):
  58
+            conn.c.close()
  59
+            debug("ConnMan.put(): closing proxy connection (keep-alive not yet supported)")
  60
+            return
  61
+
48 62
         if conn.counter >= ConnMan.conn_max_counter:
49 63
             conn.c.close()
50 64
             debug("ConnMan.put(): closing over-used connection")
17  S3/S3.py
@@ -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 @@ def send_request(self, request, body = None, retries = _max_retries):
681 672
             response["data"] =  http_response.read()
682 673
             debug("Response: " + str(response))
683 674
             ConnMan.put(conn)
  675
+        except ParameterError, e:
  676
+            raise
684 677
         except Exception, e:
685 678
             if retries:
686 679
                 warning("Retrying failed request: %s (%s)" % (resource['uri'], e))
@@ -728,6 +721,8 @@ def send_file(self, request, file, labels, buffer = '', throttle = 0, retries =
728 721
             for header in headers.keys():
729 722
                 conn.c.putheader(header, str(headers[header]))
730 723
             conn.c.endheaders()
  724
+        except ParameterError, e:
  725
+            raise
731 726
         except Exception, e:
732 727
             if self.config.progress_meter:
733 728
                 progress.done("failed")
@@ -766,6 +761,8 @@ def send_file(self, request, file, labels, buffer = '', throttle = 0, retries =
766 761
             response["size"] = size_total
767 762
             ConnMan.put(conn)
768 763
             debug(u"Response: %s" % response)
  764
+        except ParameterError, e:
  765
+            raise
769 766
         except Exception, e:
770 767
             if self.config.progress_meter:
771 768
                 progress.done("failed")
@@ -876,6 +873,8 @@ def recv_file(self, request, stream, labels, start_position = 0, retries = _max_
876 873
             response["reason"] = http_response.reason
877 874
             response["headers"] = convertTupleListToDict(http_response.getheaders())
878 875
             debug("Response: %s" % response)
  876
+        except ParameterError, e:
  877
+            raise
879 878
         except Exception, e:
880 879
             if self.config.progress_meter:
881 880
                 progress.done("failed")

0 notes on commit 8a3e46c

Please sign in to comment.
Something went wrong with that request. Please try again.