diff --git a/elasticsearch/connection/http_urllib3.py b/elasticsearch/connection/http_urllib3.py index 163ff0ccbb..6995ecce89 100644 --- a/elasticsearch/connection/http_urllib3.py +++ b/elasticsearch/connection/http_urllib3.py @@ -3,6 +3,7 @@ import urllib3 from urllib3.exceptions import ReadTimeoutError, SSLError as UrllibSSLError import warnings +import gzip CA_CERTS = None @@ -57,13 +58,15 @@ class Urllib3HttpConnection(Connection): host. See https://urllib3.readthedocs.io/en/1.4/pools.html#api for more information. :arg headers: any custom http headers to be add to requests + :arg http_compress: Use gzip compression """ def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, verify_certs=True, ca_certs=None, client_cert=None, client_key=None, ssl_version=None, ssl_assert_hostname=None, - ssl_assert_fingerprint=None, maxsize=10, headers=None, ssl_context=None, **kwargs): + ssl_assert_fingerprint=None, maxsize=10, headers=None, ssl_context=None, http_compress=False, **kwargs): super(Urllib3HttpConnection, self).__init__(host=host, port=port, use_ssl=use_ssl, **kwargs) + self.http_compress = http_compress self.headers = urllib3.make_headers(keep_alive=True) if http_auth is not None: if isinstance(http_auth, (tuple, list)): @@ -144,6 +147,10 @@ def perform_request(self, method, url, params=None, body=None, timeout=None, ign request_headers = self.headers if headers: + if self.http_compress == True: + headers.update(urllib3.make_headers(accept_encoding=True)) + headers.update({'Content-Encoding': 'gzip'}) + body = gzip.compress(body) request_headers = request_headers.copy() request_headers.update(headers) response = self.pool.urlopen(method, url, body, retries=False, headers=request_headers, **kw) diff --git a/test_elasticsearch/test_connection.py b/test_elasticsearch/test_connection.py index 66ac2afaa1..c520f41c60 100644 --- a/test_elasticsearch/test_connection.py +++ b/test_elasticsearch/test_connection.py @@ -15,6 +15,10 @@ class TestUrllib3Connection(TestCase): + def test_http_compression(self): + con = Urllib3HttpConnection(http_compress=True) + self.assertTrue(con.http_compress) + def test_timeout_set(self): con = Urllib3HttpConnection(timeout=42) self.assertEquals(42, con.timeout)