From f734bf298ff662ab31e94faae904281322f82bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20A=2E=20Alto=C3=A9=20Falqueto?= Date: Wed, 16 Nov 2016 22:11:08 -0200 Subject: [PATCH] Support http proxy environment variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If defined, the variables http_proxy, HTTP_PROXY, https_proxy or HTTPS_PROXY will be used to create a proxied pool manager. If the prefix https:// is used by the url, it will try to use the corresponding proxy variables. If not defined, it will fallback to http_proxy and HTTP_PROXY. Signed-off-by: Denis A. AltoƩ Falqueto --- buku.1 | 14 ++++++++++++++ buku.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/buku.1 b/buku.1 index 75412b2..0de6658 100644 --- a/buku.1 +++ b/buku.1 @@ -222,6 +222,20 @@ Exit buku. .BI BROWSER Overrides the default browser. Ref: .I http://docs.python.org/library/webbrowser.html +.TP +.BR http_proxy ", " HTTP_PROXY ", " https_proxy " or " HTTPS_PROXY" + +If defined, will be used to access http and https resources through the configured proxy. +For https requests, if https proxy is not defined but http is, the latter will be used +instead. + +They should be in the following format: + + http[s]://proxyhost:proxyport + +Buku uses urllib3 for proxy support, but that library doesn't support authenticated +proxies. So, if that's your need, you should use a local intermediate proxy, such as +cntlm, TinyProxy or Polipo. .SH EXAMPLES .PP .IP 1. 4 diff --git a/buku.py b/buku.py index de77633..acee4fc 100755 --- a/buku.py +++ b/buku.py @@ -1492,6 +1492,37 @@ def get_page_title(resp): return htmlparser.parsed_title +def create_poolmanager(url): + '''Creates a pool manager with proxy support, if needed. + + Checks to see if there are proxy variables defined and creates + a ProxyManager. If not, creates a PoolManager. + ''' + + # Check if http_proxy or HTTP_PROXY is defined + http_proxy = os.environ.get('http_proxy') + if not http_proxy: + http_proxy = os.environ.get('HTTP_PROXY') + + # Check if https_proxy or HTTPS_PROXY is defined + https_proxy = os.environ.get('https_proxy') + if not https_proxy: + https_proxy = os.environ.get('HTTPS_PROXY') + + # If not, falls back to http_proxy, which is more common. + if not https_proxy: + https_proxy = http_proxy + + # Create a pool manager with or without proxy support as needed. + if url.startswith('http://') and http_proxy: + manager = urllib3.ProxyManager(http_proxy) + elif url.startswith('https://') and https_proxy: + manager = urllib3.ProxyManager(https_proxy) + else: + manager = urllib3.PoolManager() + return manager; + + def network_handler(url): '''Handle server connection and redirections @@ -1513,7 +1544,7 @@ def network_handler(url): if not http_handler: urllib3.disable_warnings() - http_handler = urllib3.PoolManager() + http_handler = create_poolmanager(url) try: while True: