diff options
author | Ruben Pollan <meskio@sindominio.net> | 2016-06-01 18:46:17 +0200 |
---|---|---|
committer | Ruben Pollan <meskio@sindominio.net> | 2016-06-01 18:46:17 +0200 |
commit | d4c232d197ef307a2c7c0da5a283d87f619626f0 (patch) | |
tree | aa76e5fbe3d1f0b8e950d7d332d47888af2033e2 /src/leap/keymanager/__init__.py | |
parent | 756159dc0f7715cac70b79d5a72fce190a380e58 (diff) | |
parent | 269ea445b5a1a0533c92a4f98e2c1aa4d74d2ae7 (diff) |
Merge remote-tracking branch 'pixelated/7987_Invalid_JSON_data_from_key' into develop
Diffstat (limited to 'src/leap/keymanager/__init__.py')
-rw-r--r-- | src/leap/keymanager/__init__.py | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/leap/keymanager/__init__.py b/src/leap/keymanager/__init__.py index 194a4ee..aa0a9ac 100644 --- a/src/leap/keymanager/__init__.py +++ b/src/leap/keymanager/__init__.py @@ -26,6 +26,8 @@ import json import urllib from leap.common import ca_bundle +from twisted.web import client +from twisted.web._responses import NOT_FOUND from ._version import get_versions @@ -209,12 +211,12 @@ class KeyManager(object): """ try: uri = self._nickserver_uri + '?address=' + address - content = yield self._async_client_pinned.request(str(uri), 'GET') + content = yield self._fetch_and_handle_404_from_nicknym(uri, address) json_content = json.loads(content) + + except KeyNotFound: + raise except IOError as e: - # FIXME: 404 doesnt raise today, but it wont produce json anyway - # if e.response.status_code == 404: - # raise KeyNotFound(address) logger.warning("HTTP error retrieving key: %r" % (e,)) logger.warning("%s" % (content,)) raise KeyNotFound(e.message), None, sys.exc_info()[2] @@ -232,6 +234,29 @@ class KeyManager(object): # 'Content-type is not JSON.') defer.returnValue(json_content) + def _fetch_and_handle_404_from_nicknym(self, uri, address): + """ + Send a GET request to C{uri} containing C{data}. + + :param uri: The URI of the request. + :type uri: str + :param address: The email corresponding to the key. + :type address: str + + :return: A deferred that will be fired with GET content as json (dict) + :rtype: Deferred + """ + def check_404(response): + if response.code == NOT_FOUND: + message = '%s: %s key not found.' % (response.code, address) + logger.warning(message) + raise KeyNotFound(message), None, sys.exc_info()[2] + return response + + d = self._async_client_pinned.request(str(uri), 'GET', callback=check_404) + d.addCallback(client.readBody) + return d + @defer.inlineCallbacks def _get_with_combined_ca_bundle(self, uri, data=None): """ |