From a1bed140e421f793bf18efec649bc352fcce8198 Mon Sep 17 00:00:00 2001 From: Yvann Boucher Date: Wed, 16 Sep 2015 22:55:34 +0700 Subject: [PATCH 1/2] Moved the "accept-encoding" part to be always effective, the compression will depend of the ES configuration, we only say that we support it. --- lib/Elastica/Transport/Http.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/Elastica/Transport/Http.php b/lib/Elastica/Transport/Http.php index 772c0b429a..6f03b4bbf1 100644 --- a/lib/Elastica/Transport/Http.php +++ b/lib/Elastica/Transport/Http.php @@ -70,6 +70,11 @@ public function exec(Request $request, array $params) curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout()); curl_setopt($conn, CURLOPT_FORBID_REUSE, 0); + // Tell ES that we support the compressed responses + // An "Accept-Encoding" header containing all supported encoding types is sent + // curl will decode the response automatically if the response is encoded + curl_setopt($conn, CURLOPT_ENCODING, ''); + /* @see Connection::setConnectTimeout() */ $connectTimeout = $connection->getConnectTimeout(); if ($connectTimeout > 0) { @@ -119,15 +124,11 @@ public function exec(Request $request, array $params) $content = str_replace('\/', '/', $content); if ($connection->hasCompression()) { - // An "Accept-Encoding" header containing all supported encoding types is sent - // Curl will decode the response automatically - curl_setopt($conn, CURLOPT_ENCODING, ''); + // Compress the body of the request ... + curl_setopt($conn, CURLOPT_POSTFIELDS, gzencode($content)); - // Let's precise that the request is also compressed + // ... and tell ES that it is compressed curl_setopt($conn, CURLOPT_HTTPHEADER, array('Content-Encoding: gzip')); - - // Let's compress the request body, - curl_setopt($conn, CURLOPT_POSTFIELDS, gzencode($content)); } else { curl_setopt($conn, CURLOPT_POSTFIELDS, $content); } From 713c6cf068ca186257c1a7d75c6ea04b5e1d05b3 Mon Sep 17 00:00:00 2001 From: Yvann Boucher Date: Sat, 12 Dec 2015 13:58:18 +0700 Subject: [PATCH 2/2] Added some tests to show the success of a request with the compression parameter --- test/lib/Elastica/Test/Transport/HttpTest.php | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/test/lib/Elastica/Test/Transport/HttpTest.php b/test/lib/Elastica/Test/Transport/HttpTest.php index a474354943..7d38cedb65 100644 --- a/test/lib/Elastica/Test/Transport/HttpTest.php +++ b/test/lib/Elastica/Test/Transport/HttpTest.php @@ -227,18 +227,33 @@ public function testBodyReuse() /** * @group functional */ - public function testPostWith0Body() + public function testRequestSuccessWithHttpCompressionEnabled() { - $client = $this->_getClient(); + $client = $this->_getClient(array('transport' => array('type' => 'Http', 'compression' => true, 'curl' => array(CURLINFO_HEADER_OUT => true)))); - $index = $client->getIndex('elastica_0_body'); - $index->create(array(), true); - $this->_waitForAllocation($index); - $index->refresh(); + $index = $client->getIndex('elastica_request_with_body_and_http_compression_enabled'); + + $createIndexResponse = $index->create(array(), true); + + $createIndexResponseTransferInfo = $createIndexResponse->getTransferInfo(); + $this->assertRegExp('/Accept-Encoding:\ (gzip|deflate)/', $createIndexResponseTransferInfo['request_header']); + $this->assertArrayHasKey('acknowledged', $createIndexResponse->getData()); + } + + /** + * @group functional + */ + public function testRequestSuccessWithHttpCompressionDisabled() + { + $client = $this->_getClient(array('transport' => array('type' => 'Http', 'compression' => false, 'curl' => array(CURLINFO_HEADER_OUT => true)))); + + $index = $client->getIndex('elastica_request_with_body_and_http_compression_disabled'); - $tokens = $index->analyze('0'); + $createIndexResponse = $index->create(array(), true); - $this->assertNotEmpty($tokens); + $createIndexResponseTransferInfo = $createIndexResponse->getTransferInfo(); + $this->assertRegExp('/Accept-Encoding:\ (gzip|deflate)/', $createIndexResponseTransferInfo['request_header']); + $this->assertArrayHasKey('acknowledged', $createIndexResponse->getData()); } protected function checkProxy($url)