Skip to content

Commit

Permalink
Adding GZip support to urllib3
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Gil committed Feb 27, 2018
1 parent 4438ea7 commit 6d92572
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion elasticsearch/connection/http_urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import urllib3
from urllib3.exceptions import ReadTimeoutError, SSLError as UrllibSSLError
import warnings
import gzip

CA_CERTS = None

Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions test_elasticsearch/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6d92572

Please sign in to comment.