From 5aa840e848a80442e8f2e242d014ccdc543d59a6 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 22 May 2012 14:14:40 +0900 Subject: [PATCH] handle proxy config in libanki --- anki/sync.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/anki/sync.py b/anki/sync.py index bc13b5b2f..4ca511487 100644 --- a/anki/sync.py +++ b/anki/sync.py @@ -7,7 +7,7 @@ from cStringIO import StringIO from datetime import date from anki.db import DB from anki.errors import * -from anki.utils import ids2str, checksum, intTime, json +from anki.utils import ids2str, checksum, intTime, json, isWin, isMac from anki.consts import * from anki.lang import _ from hooks import runHook @@ -17,6 +17,9 @@ HTTP_CERTS = os.path.join(os.path.dirname(__file__), "ankiweb.certs") HTTP_TIMEOUT = 30 HTTP_PROXY = None +# Httplib2 connection object +###################################################################### + def httpCon(): return httplib2.Http( timeout=HTTP_TIMEOUT, ca_certs=HTTP_CERTS, @@ -24,6 +27,40 @@ def httpCon(): # python2 doesn't support SNI disable_ssl_certificate_validation="beta" in SYNC_URL) +# Proxy handling +###################################################################### + +def _setupProxy(): + global HTTP_PROXY + # set in env? + p = httplib2.ProxyInfo.from_environment() + if not p: + # platform-specific fetch + url = None + if isWin: + r = urllib.getproxies_registry() + if 'https' in r: + url = r['https'] + elif 'http' in r: + url = r['http'] + elif isMac: + r = urllib.getproxies_macosx_sysconf() + if 'https' in r: + url = r['https'] + elif 'http' in r: + url = r['http'] + if url: + p = ProxyInfo.from_url(url, _proxyMethod(url)) + HTTP_PROXY = p + +def _proxyMethod(url): + if url.lower().startswith("https"): + return "https" + else: + return "http" + +_setupProxy() + # Incremental syncing ##########################################################################