diff --git a/CHANGELOG.md b/CHANGELOG.md index 0185c420a..c28a9ca37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Client.php b/src/Client.php index 3a56b41fe..c3c5718ae 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; @@ -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', ''); + } + return $transport; } @@ -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)) { diff --git a/tests/ClientFunctionalTest.php b/tests/ClientFunctionalTest.php index 84ab2fff7..0acd9bdd6 100644 --- a/tests/ClientFunctionalTest.php +++ b/tests/ClientFunctionalTest.php @@ -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; @@ -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)) { diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 3bee250f5..91289ecfd 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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; /** @@ -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);