summaryrefslogtreecommitdiff
path: root/src/leap
diff options
context:
space:
mode:
authorTomás Touceda <chiiph@leap.se>2013-03-13 09:43:13 -0300
committerTomás Touceda <chiiph@leap.se>2013-03-13 09:43:13 -0300
commit70c402fe170ca4e01159b03739b7cacda7b0dfd0 (patch)
treef4e552d59a2e8b651ae754e80c308c4c12984857 /src/leap
parenta3d01f3285c7432675649e26aa95e31a64b73cbf (diff)
Add mtime check for existing json definitions before download
Also, wait for threads to finish when quitting
Diffstat (limited to 'src/leap')
-rw-r--r--src/leap/gui/mainwindow.py8
-rw-r--r--src/leap/services/eip/eipbootstrapper.py44
-rw-r--r--src/leap/services/eip/providerbootstrapper.py59
-rw-r--r--src/leap/util/checkerthread.py1
-rw-r--r--src/leap/util/files.py18
5 files changed, 76 insertions, 54 deletions
diff --git a/src/leap/gui/mainwindow.py b/src/leap/gui/mainwindow.py
index f8ca4c35..6f7f646e 100644
--- a/src/leap/gui/mainwindow.py
+++ b/src/leap/gui/mainwindow.py
@@ -139,7 +139,15 @@ class MainWindow(QtGui.QMainWindow):
QtCore.QCoreApplication.instance().connect(
QtCore.QCoreApplication.instance(),
QtCore.SIGNAL("aboutToQuit()"),
+ self._vpn.wait)
+ QtCore.QCoreApplication.instance().connect(
+ QtCore.QCoreApplication.instance(),
+ QtCore.SIGNAL("aboutToQuit()"),
self._checker_thread.set_should_quit)
+ QtCore.QCoreApplication.instance().connect(
+ QtCore.QCoreApplication.instance(),
+ QtCore.SIGNAL("aboutToQuit()"),
+ self._checker_thread.wait)
self.ui.chkRemember.stateChanged.connect(
self._remember_state_changed)
diff --git a/src/leap/services/eip/eipbootstrapper.py b/src/leap/services/eip/eipbootstrapper.py
index 6fbb98b9..fdf54bbb 100644
--- a/src/leap/services/eip/eipbootstrapper.py
+++ b/src/leap/services/eip/eipbootstrapper.py
@@ -31,7 +31,7 @@ from leap.config.providerconfig import ProviderConfig
from leap.services.eip.eipconfig import EIPConfig
from leap.util.check import leap_assert, leap_assert_type
from leap.util.checkerthread import CheckerThread
-from leap.util.files import check_and_fix_urw_only
+from leap.util.files import check_and_fix_urw_only, get_mtime
logger = logging.getLogger(__name__)
@@ -88,33 +88,39 @@ class EIPBootstrapper(QtCore.QObject):
self._eip_config = EIPConfig()
- if self._download_if_needed and \
- os.path.exists(os.path.join(self._eip_config.get_path_prefix(),
- "leap",
- "providers",
- self._provider_config.get_domain(),
- "eip-service.json")):
- download_config_data[self.PASSED_KEY] = True
- self.download_config.emit(download_config_data)
- return True
-
try:
+ headers = {}
+ mtime = get_mtime(os.path.join(self._eip_config
+ .get_path_prefix(),
+ "leap",
+ "providers",
+ self._provider_config.get_domain(),
+ "eip-service.json"))
+
+ if self._download_if_needed and mtime:
+ headers['if-modified-since'] = mtime
+
res = self._session.get("%s/%s/%s/%s" %
(self._provider_config.get_api_uri(),
self._provider_config.get_api_version(),
"config",
"eip-service.json"),
verify=self._provider_config
- .get_ca_cert_path())
+ .get_ca_cert_path(),
+ headers=headers)
res.raise_for_status()
- eip_definition = res.content
-
- self._eip_config.load(data=eip_definition)
- self._eip_config.save(["leap",
- "providers",
- self._provider_config.get_domain(),
- "eip-service.json"])
+ # Not modified
+ if res.status_code == 304:
+ logger.debug("EIP definition has not been modified")
+ else:
+ eip_definition = res.content
+
+ self._eip_config.load(data=eip_definition)
+ self._eip_config.save(["leap",
+ "providers",
+ self._provider_config.get_domain(),
+ "eip-service.json"])
download_config_data[self.PASSED_KEY] = True
except Exception as e:
diff --git a/src/leap/services/eip/providerbootstrapper.py b/src/leap/services/eip/providerbootstrapper.py
index 0e9f8563..f1a917f0 100644
--- a/src/leap/services/eip/providerbootstrapper.py
+++ b/src/leap/services/eip/providerbootstrapper.py
@@ -31,7 +31,7 @@ from PySide import QtGui, QtCore
from leap.config.providerconfig import ProviderConfig
from leap.util.check import leap_assert, leap_assert_type
from leap.util.checkerthread import CheckerThread
-from leap.util.files import check_and_fix_urw_only
+from leap.util.files import check_and_fix_urw_only, get_mtime
logger = logging.getLogger(__name__)
@@ -72,25 +72,6 @@ class ProviderBootstrapper(QtCore.QObject):
self._provider_config = None
self._download_if_needed = False
- def _should_proceed_provider(self):
- """
- Returns False if provider.json already exists for the given
- domain. True otherwise
-
- @rtype: bool
- """
- if not self._download_if_needed:
- return True
-
- # We don't really need a provider config at this stage, just
- # the path prefix
- return not os.path.exists(os.path.join(ProviderConfig()
- .get_path_prefix(),
- "leap",
- "providers",
- self._domain,
- "provider.json"))
-
def _check_name_resolution(self):
"""
Checks that the name resolution for the provider name works
@@ -171,24 +152,34 @@ class ProviderBootstrapper(QtCore.QObject):
self.ERROR_KEY: ""
}
- if not self._should_proceed_provider():
- download_data[self.PASSED_KEY] = True
- self.download_provider_info.emit(download_data)
- return True
-
try:
+ headers = {}
+ mtime = get_mtime(os.path.join(ProviderConfig()
+ .get_path_prefix(),
+ "leap",
+ "providers",
+ self._domain,
+ "provider.json"))
+ if self._download_if_needed and mtime:
+ headers['if-modified-since'] = mtime
+
res = self._session.get("https://%s/%s" % (self._domain,
- "provider.json"))
+ "provider.json"),
+ headers=headers)
res.raise_for_status()
- provider_definition = res.content
-
- provider_config = ProviderConfig()
- provider_config.load(data=provider_definition)
- provider_config.save(["leap",
- "providers",
- self._domain,
- "provider.json"])
+ # Not modified
+ if res.status_code == 304:
+ logger.debug("Provider definition has not been modified")
+ else:
+ provider_definition = res.content
+
+ provider_config = ProviderConfig()
+ provider_config.load(data=provider_definition)
+ provider_config.save(["leap",
+ "providers",
+ self._domain,
+ "provider.json"])
download_data[self.PASSED_KEY] = True
except Exception as e:
diff --git a/src/leap/util/checkerthread.py b/src/leap/util/checkerthread.py
index 681c33e1..3430a450 100644
--- a/src/leap/util/checkerthread.py
+++ b/src/leap/util/checkerthread.py
@@ -64,7 +64,6 @@ class CheckerThread(QtCore.QThread):
"""
QtCore.QMutexLocker(self._should_quit_lock)
self._should_quit = True
- self.wait()
def start(self):
"""
diff --git a/src/leap/util/files.py b/src/leap/util/files.py
index f7fda39e..8c7a5af3 100644
--- a/src/leap/util/files.py
+++ b/src/leap/util/files.py
@@ -1,6 +1,7 @@
import os
import stat
import logging
+import time
logger = logging.getLogger(__name__)
@@ -25,3 +26,20 @@ def check_and_fix_urw_only(cert):
logger.error('Error while trying to chmod 600 %s' %
cert)
raise
+
+
+def get_mtime(filename):
+ """
+ Returns the modified time or None if the file doesn't exist
+
+ @param filename: path to check
+ @type filename: str
+
+ @rtype: str
+ """
+ try:
+ _mtime = os.stat(filename)[8]
+ mtime = time.strftime("%c GMT", time.gmtime(_mtime))
+ return mtime
+ except OSError:
+ return None