Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It's me again :) http.compression more effective ! #932

Merged
merged 2 commits into from
Dec 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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