From 361d8d0944f46ba6cd509d16dcee950d19cd8ff6 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Fri, 15 Jan 2021 20:19:19 +0100 Subject: [PATCH] new: Support brotli compression --- pymisp/api.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pymisp/api.py b/pymisp/api.py index a7811a4..0c23ad5 100644 --- a/pymisp/api.py +++ b/pymisp/api.py @@ -15,6 +15,7 @@ from uuid import UUID import warnings import sys import copy +import urllib3 from io import BytesIO, StringIO from . import __version__, everything_broken @@ -87,6 +88,27 @@ def register_user(misp_url: str, email: str, return r.json() +def brotli_supported() -> bool: + """ + Returns whether Brotli compression is supported + """ + + # urllib >= 1.25.1 includes brotli support + major, minor, patch = urllib3.__version__.split('.') # noqa: F811 + major, minor, patch = int(major), int(minor), int(patch) + urllib3_with_brotli = (major == 1 and ((minor == 25 and patch >= 1) or (minor >= 26))) or major >= 2 + + if not urllib3_with_brotli: + return False + + # pybrotli is an extra package required by urllib3 for brotli support + try: + import brotli + return True + except ImportError: + return False + + class PyMISP: """Python API for MISP @@ -117,6 +139,8 @@ class PyMISP: self.tool: str = tool self.timeout: Optional[Union[float, Tuple[float, float]]] = timeout self.__session = requests.Session() # use one session to keep connection between requests + if brotli_supported(): + self.__session.headers['Accept-Encoding'] = ', '.join(('br', 'gzip', 'deflate')) self.global_pythonify = False