Index: juju/lib/http_client.py |
=== added file 'juju/lib/http_client.py' |
--- juju/lib/http_client.py 1970-01-01 00:00:00 +0000 |
+++ juju/lib/http_client.py 2012-11-23 19:22:19 +0000 |
@@ -0,0 +1,74 @@ |
+# -*- coding: utf-8 -*- |
+import os |
+import urlparse |
+ |
+from twisted.internet import reactor |
+from twisted.internet.defer import Deferred |
+from twisted.internet.endpoints import TCP4ClientEndpoint |
+from twisted.internet.protocol import connectionDone, Protocol |
+from twisted.web.client import downloadPage, getPage, ProxyAgent, ResponseDone |
+from twisted.web.http import PotentialDataLoss |
+ |
+ |
+class Receiver(Protocol): |
+ finished = None |
+ _filename = None |
+ |
+ def __init__(self, filename=None): |
+ self.received = "" |
+ if filename: |
+ self._filename = filename |
+ |
+ def dataReceived(self, data): |
+ self.received += data |
+ |
+ def connectionLost(self, reason=connectionDone): |
+ reason.trap(ResponseDone, PotentialDataLoss, connectionDone) |
+ d = self.finished |
+ self.finished = None |
+ if self._filename: |
+ file = open(self._filename, "w") |
+ try: |
+ file.write(self.received) |
+ finally: |
+ file.close() |
+ d.callback(self.received) |
+ |
+ |
+def response_saver(path): |
+ def save_response(response): |
+ receiver = Receiver(path) |
+ receiver.finished = d = Deferred() |
+ response.deliverBody(receiver) |
+ return d |
+ return save_response |
+ |
+ |
+def _deferred_get(url, proxy_endpoint): |
+ proxy_url = urlparse.urlparse(proxy_endpoint) |
+ endpoint = TCP4ClientEndpoint(reactor, proxy_url.hostname, proxy_url.port) |
+ agent = ProxyAgent(endpoint) |
+ return agent.request("GET", url) |
+ |
+ |
+def download_page(url, path, *args, **kwargs): |
+ host = urlparse.urlparse(url).hostname |
+ proxy_var = url.startswith("https://") and "https_proxy" or "http_proxy" |
+ proxy_endpoint = os.environ.get(proxy_var) |
+ if proxy_endpoint and host not in ("localhost", "127.0.0.1", "169.254.169.254"): |
+ d = _deferred_get(url, proxy_endpoint) |
+ d.addCallback(response_saver(path)) |
+ return d |
+ return downloadPage(url, path, *args, **kwargs) |
+ |
+ |
+def get_page(url, *args, **kwargs): |
+ host = urlparse.urlparse(url).hostname |
+ proxy_var = url.startswith("https://") and "https_proxy" or "http_proxy" |
+ proxy_endpoint = os.environ.get(proxy_var) |
+ if proxy_endpoint and host not in ("localhost", "127.0.0.1", "169.254.169.254"): |
+ d = _deferred_get(url, proxy_endpoint) |
+ d.addCallback(response_saver(None)) |
+ return d |
+ else: |
+ return getPage(url, *args, **kwargs) |