Skip to content

Commit

Permalink
Support gzip encoding while http requesting
Browse files Browse the repository at this point in the history
  • Loading branch information
ueffel committed Jun 23, 2019
1 parent 0a7e4b4 commit 5e03549
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packagecontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import time
import traceback
import urllib
import gzip


PACKAGE_COMMAND = kp.ItemCategory.USER_BASE + 1

Expand Down Expand Up @@ -416,12 +418,16 @@ def _get_available_packages(self, force=False):
try:
repo = repos[tries % 2]
self.dbg("Try to get list from", repo)
req = urllib.request.Request(repo)
req = urllib.request.Request(repo, headers={"Accept-Encoding": "gzip"})
with self._urlopener.open(req) as response:
repo = json.loads(response.read().decode())
if response.info().get("Content-Encoding") == "gzip":
repo = json.loads(gzip.decompress(response.read()).decode())
else:
repo = json.loads(response.read().decode())
tries = 0
if hasattr(req, "redirect"):
self.info("Request permanently redirected. Changing repository url to:", req.redirect)
self.info("Request permanently redirected. Changing repository url to:",
req.redirect)
if tries % 2 == 0:
self._repo_url = req.redirect
else:
Expand All @@ -447,7 +453,8 @@ def _get_available_packages(self, force=False):
json_package["download_url"],
json_package["filename"],
json_package["owner"] if "owner" in json_package else "",
json_package["homepage"] if "homepage" in json_package else ""))
json_package["homepage"]
if "homepage" in json_package else ""))
self.dbg(self._available_packages)

if write_cache:
Expand All @@ -462,7 +469,7 @@ def _get_available_packages(self, force=False):
self._save_last_run()

return self._available_packages
except Exception as exc:
except Exception:
self.err("Available packages could not be obtained:\n", traceback.format_exc())
finally:
self.__list_updating = False
Expand Down

0 comments on commit 5e03549

Please sign in to comment.