From 60a9d81cccece11338536a23fa4e9bc2df66f603 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Mon, 31 Oct 2011 20:55:22 +0000
Subject: [PATCH] throttle HTTPS connections too (doh!)

---
 client/djrandom_client/throttle.py | 26 ++++++++++++++++++++++----
 client/djrandom_client/upload.py   |  3 ++-
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/client/djrandom_client/throttle.py b/client/djrandom_client/throttle.py
index 78f0fb7..1aea61f 100644
--- a/client/djrandom_client/throttle.py
+++ b/client/djrandom_client/throttle.py
@@ -63,19 +63,17 @@ def set_rate_limit(kbps):
     _bucket = TokenBucket(2 * bps, bps)
 
 
-class ThrottledHTTPConnection(httplib.HTTPConnection):
+class ThrottledHTTPConnectionMixin(object):
 
     BLOCK_SIZE = 4096
 
-    def send(self, str):
+    def throttled_send(self, str):
         if self.sock is None:
             if self.auto_open:
                 self.connect()
             else:
                 raise httplib.NotConnected()
 
-        if self.debuglevel > 0:
-            print "send:", repr(str)
         try:
             tot = len(str)
             for off in xrange(0, tot, self.BLOCK_SIZE):
@@ -93,9 +91,29 @@ class ThrottledHTTPConnection(httplib.HTTPConnection):
             raise
 
 
+class ThrottledHTTPSConnection(httplib.HTTPSConnection,
+                               ThrottledHTTPConnectionMixin):
+
+    def send(self, str):
+        return self.throttled_send(str)
+
+
+class ThrottledHTTPConnection(httplib.HTTPConnection,
+                              ThrottledHTTPConnectionMixin):
+
+    def send(self, str):
+        return self.throttled_send(str)
+
+
 class ThrottledHTTPHandler(urllib2.HTTPHandler):
 
     def http_open(self, req):
         return self.do_open(ThrottledHTTPConnection, req)
 
 
+class ThrottledHTTPSHandler(urllib2.HTTPSHandler):
+
+    def https_open(self, req):
+        return self.do_open(ThrottledHTTPSConnection, req)
+
+
diff --git a/client/djrandom_client/upload.py b/client/djrandom_client/upload.py
index 3b37bfd..44a6513 100644
--- a/client/djrandom_client/upload.py
+++ b/client/djrandom_client/upload.py
@@ -68,7 +68,8 @@ class Uploader(threading.Thread):
         self.queue = Queue.Queue(100)
         self.db_path = db_path
         self.stats = stats.Stats(state_path)
-        self.opener = urllib2.build_opener(throttle.ThrottledHTTPHandler)
+        self.opener = urllib2.build_opener(throttle.ThrottledHTTPHandler,
+                                           throttle.ThrottledHTTPSHandler)
 
         user_agent = 'djrandom_client/%s (%s %s Python/%s)' % (
             version.VERSION, platform.system(), platform.machine(),
-- 
GitLab