Permalink
Browse files

Add proxy support

  • Loading branch information...
1 parent e93378f commit 8a3e46c2cfe7776d15c1e35d0e6d548b28898c43 @mludvig mludvig committed Mar 10, 2013
Showing with 27 additions and 14 deletions.
  1. +19 −5 S3/ConnMan.py
  2. +8 −9 S3/S3.py
View
@@ -4,16 +4,19 @@
from logging import debug, info, warning, error
from Config import Config
+from Exceptions import ParameterError
__all__ = [ "ConnMan" ]
class http_connection(object):
- def __init__(self, id, hostname, ssl):
+ def __init__(self, id, hostname, ssl, cfg):
self.hostname = hostname
self.ssl = ssl
self.id = id
self.counter = 0
- if not ssl:
+ if cfg.proxy_host != "":
+ self.c = httplib.HTTPConnection(cfg.proxy_host, cfg.proxy_port)
+ elif not ssl:
self.c = httplib.HTTPConnection(hostname)
else:
self.c = httplib.HTTPSConnection(hostname)
@@ -25,10 +28,16 @@ class ConnMan(object):
@staticmethod
def get(hostname, ssl = None):
+ cfg = Config()
if ssl == None:
- ssl = Config().use_https
+ ssl = cfg.use_https
conn = None
- conn_id = "http%s://%s" % (ssl and "s" or "", hostname)
+ if cfg.proxy_host != "":
+ if ssl:
+ raise ParameterError("use_ssl=True can't be used with proxy")
+ conn_id = "proxy://%s:%s" % (cfg.proxy_host, cfg.proxy_port)
+ else:
+ conn_id = "http%s://%s" % (ssl and "s" or "", hostname)
ConnMan.conn_pool_sem.acquire()
if not ConnMan.conn_pool.has_key(conn_id):
ConnMan.conn_pool[conn_id] = []
@@ -38,13 +47,18 @@ def get(hostname, ssl = None):
ConnMan.conn_pool_sem.release()
if not conn:
debug("ConnMan.get(): creating new connection: %s" % conn_id)
- conn = http_connection(conn_id, hostname, ssl)
+ conn = http_connection(conn_id, hostname, ssl, cfg)
conn.c.connect()
conn.counter += 1
return conn
@staticmethod
def put(conn):
+ if conn.id.startswith("proxy://"):
+ conn.c.close()
+ debug("ConnMan.put(): closing proxy connection (keep-alive not yet supported)")
+ return
+
if conn.counter >= ConnMan.conn_max_counter:
conn.c.close()
debug("ConnMan.put(): closing over-used connection")
View
@@ -191,15 +191,6 @@ class S3(object):
def __init__(self, config):
self.config = config
- def get_connection(self, bucket):
- if self.config.proxy_host != "":
- return httplib.HTTPConnection(self.config.proxy_host, self.config.proxy_port)
- else:
- if self.config.use_https:
- return httplib.HTTPSConnection(self.get_hostname(bucket))
- else:
- return httplib.HTTPConnection(self.get_hostname(bucket))
-
def get_hostname(self, bucket):
if bucket and check_bucket_name_dns_conformity(bucket):
if self.redir_map.has_key(bucket):
@@ -681,6 +672,8 @@ def send_request(self, request, body = None, retries = _max_retries):
response["data"] = http_response.read()
debug("Response: " + str(response))
ConnMan.put(conn)
+ except ParameterError, e:
+ raise
except Exception, e:
if retries:
warning("Retrying failed request: %s (%s)" % (resource['uri'], e))
@@ -728,6 +721,8 @@ def send_file(self, request, file, labels, buffer = '', throttle = 0, retries =
for header in headers.keys():
conn.c.putheader(header, str(headers[header]))
conn.c.endheaders()
+ except ParameterError, e:
+ raise
except Exception, e:
if self.config.progress_meter:
progress.done("failed")
@@ -766,6 +761,8 @@ def send_file(self, request, file, labels, buffer = '', throttle = 0, retries =
response["size"] = size_total
ConnMan.put(conn)
debug(u"Response: %s" % response)
+ except ParameterError, e:
+ raise
except Exception, e:
if self.config.progress_meter:
progress.done("failed")
@@ -876,6 +873,8 @@ def recv_file(self, request, stream, labels, start_position = 0, retries = _max_
response["reason"] = http_response.reason
response["headers"] = convertTupleListToDict(http_response.getheaders())
debug("Response: %s" % response)
+ except ParameterError, e:
+ raise
except Exception, e:
if self.config.progress_meter:
progress.done("failed")

0 comments on commit 8a3e46c

Please sign in to comment.