Skip to content

fix: skip interim 1xx responses in the Curl client - #48

Draft
ruflin wants to merge 2 commits into
elastic:mainfrom
ruflin:fix/curl-interim-1xx-responses
Draft

ruflin wants to merge 2 commits into
elastic:mainfrom
ruflin:fix/curl-interim-1xx-responses

Conversation

@ruflin

@ruflin ruflin commented Oct 28, 2026

Copy link
Copy Markdown

Description

Elastic\Transport\Client\Curl runs curl with CURLOPT_HEADER => true, so curl returns every response block it received. For a request body over 1 MB libcurl adds Expect: 100-continue on its own, the server answers HTTP/1.1 100 Continue and then the real response. parseResponse() reads the first status line and the first header block, so it returns status 100 with no headers and the body HTTP/1.1 200 OK. In elasticsearch-php this surfaces as UnknownContentTypeException: No Content-Type specified in the response, because Elasticsearch::setResponse() only throws for 4xx/5xx and only runs the product check for 2xx.

Changes in src/Client/Curl.php:

  • Skip interim 1xx responses (except 101 Switching Protocols, which is final) before parsing.
  • Keep everything after the first blank line as the body. The old code used the single line after it, so a body containing \r\n was truncated at the first one.

Tests

New tests/Client/CurlInterimResponseTest.php with a small raw-socket server (interim_server.php), since the built-in PHP server cannot send a 100 Continue:

  • explicit Expect: 100-continue header, final 200 with headers and body expected
  • 1.2 MB body with no explicit header (libcurl adds Expect itself)
  • body containing CRLF is not truncated

All three fail on main (PHP 8.1, libcurl): Failed asserting that 100 is identical to 200. and, for the body test, -'first\r\nsecond' +'first'. They pass with the change. Full suite 135 tests OK and PHPStan clean.

Relation to ruflin/Elastica

  • Reported as ruflin/Elastica#2321; every bulk request over 1 MB fails with the Curl client.
  • ruflin/Elastica#2322 works around it inside Elastica by setting an empty Expect header when the Curl client is in use. It stays valid for users on older elastic/transport versions.
  • This PR is the root-cause fix. Once it is released, Elastica can raise the minimum elastic/transport (via elasticsearch/elasticsearch) and drop the workaround. The two are compatible, so no ordering is required.
  • No change is needed in elasticsearch-php, it only requires elastic/transport.

Not covered: proxy CONNECT responses and redirect chains (CURLOPT_FOLLOWLOCATION) still produce the first block, as before.

With CURLOPT_HEADER curl returns the interim "100 Continue" response in
front of the final one. For a request body over 1 MB libcurl sends
"Expect: 100-continue" on its own, so parseResponse() returned status 100
with no headers and the body "HTTP/1.1 200 OK".

Skip interim 1xx responses (except 101) before parsing, and keep CRLF
sequences inside the body instead of truncating it at the first one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant