Skip to content

Commit

Permalink
Merge pull request #932 from yvann/master
Browse files Browse the repository at this point in the history
It's me again :) http.compression more effective !
  • Loading branch information
ruflin committed Dec 13, 2015
2 parents 9fc9edc + 713c6cf commit c000d5d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
15 changes: 8 additions & 7 deletions lib/Elastica/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
31 changes: 23 additions & 8 deletions test/lib/Elastica/Test/Transport/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c000d5d

Please sign in to comment.