Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated
### Removed
### Fixed
* Fixed requests with a body over 1 MB failing with the `Elastic\Transport\Client\Curl` HTTP client because of the `Expect: 100-continue` header [#2322](https://github.com/ruflin/Elastica/pull/2322)
### Security


Expand Down
17 changes: 17 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Elastic\Elasticsearch\Traits\NamespaceTrait;
use Elastic\Elasticsearch\Transport\Adapter\AdapterInterface;
use Elastic\Elasticsearch\Transport\Adapter\AdapterOptions;
use Elastic\Transport\Client\Curl;
use Elastic\Transport\Exception\NoAsyncClientException;
use Elastic\Transport\Exception\NoNodeAvailableException;
use Elastic\Transport\Transport;
Expand Down Expand Up @@ -626,6 +627,14 @@ protected function _buildTransport(array $config): Transport
$transport->setHeader('Accept-Encoding', 'gzip');
}

/*
* Curl sends "Expect: 100-continue" for a body over 1 MB
* and the Curl client of elastic/transport takes "100 Continue" for the response
*/
if ($this->isCurlHttpClient($transport)) {
$transport->setHeader('Expect', '');

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means if a user set their own Expect header, it would be reset? Likely a rare edge case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprisely yes. And it's real project where this happens.

}

return $transport;
}

Expand All @@ -645,6 +654,14 @@ protected function isSymfonyHttpClient(Transport $transport): bool
}
}

/**
* Returns true if the transport HTTP client is the Curl client of elastic/transport.
*/
protected function isCurlHttpClient(Transport $transport): bool
{
return $transport->getClient() instanceof Curl;
}

protected function setTransportClientOptions(HttpClientInterface $client, array $config, array $clientOptions = []): HttpClientInterface
{
if (empty($config) && empty($clientOptions)) {
Expand Down
22 changes: 22 additions & 0 deletions tests/ClientFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

use Elastic\Elasticsearch\Response\Elasticsearch;
use Elastic\Elasticsearch\Transport\Adapter\AdapterOptions;
use Elastic\Transport\Client\Curl;
use Elastic\Transport\Exception\NoNodeAvailableException;
use Elastica\Bulk;
use Elastica\Document;
use Elastica\Exception\NotFoundException;
use Elastica\Query;
use Elastica\Query\Terms;
use Elastica\Script\Script;
use Elastica\Test\Base as BaseTest;
use Elastica\Test\Transport\NodePool\TraceableSimpleNodePool;
Expand Down Expand Up @@ -908,6 +911,25 @@ public static function endpointQueryRequestDataProvider(): array
];
}

/**
* Test search with a body over 1 MB, for which curl sends `Expect: 100-continue`.
*/
public function testSearchWithLargeBodyUsingCurlClient(): void
{
$client = $this->_getClient([
'transport_config' => [
'http_client' => new Curl(),
],
]);
$index = $client->getIndex($this->_createIndex()->getName());

// 12000 terms of 100 chars make a body of about 1.2 MB
$terms = \array_map(static fn (int $i): string => \str_pad((string) $i, 100, 'x'), \range(1, 12000));

$resultSet = $index->search(new Query(new Terms('name', $terms)));
$this->assertSame(0, $resultSet->getTotalHits());
}

protected function setHttpClientOptions(HttpClientInterface $client, array $config, array $clientOptions = []): HttpClientInterface
{
if (empty($config) && empty($clientOptions)) {
Expand Down
16 changes: 16 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Elastica\Test;

use Elastic\Transport\Client\Curl;
use Elastica\Client;
use Elastica\ClientConfiguration;
use Elastica\Exception\InvalidException;
use Elastica\Test\Base as BaseTest;
use GuzzleHttp\Client as GuzzleClient;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down Expand Up @@ -56,6 +58,20 @@ public function testItAddsHosts(): void
$this->assertEquals('https://my-host.com:9300', $client->getTransport()->getNodePool()->nextNode()->getUri());
}

public function testItDisablesExpectHeaderForCurlClient(): void
{
$client = new Client(['transport_config' => ['http_client' => new Curl()]]);

$this->assertSame(['Expect' => ''], $client->getTransport()->getHeaders());
}

public function testItDoesNotSetExpectHeaderForGuzzleClient(): void
{
$client = new Client(['transport_config' => ['http_client' => new GuzzleClient()]]);

$this->assertArrayNotHasKey('Expect', $client->getTransport()->getHeaders());
}

public function testAddDocumentsEmpty(): void
{
$this->expectException(InvalidException::class);
Expand Down