Description
Symfony version(s) affected
7.0
Description
When sending a FormData
request (and possibly other chunkable body types), CurlHttpClient
will set a Transfer-Encoding: chunked
header:
symfony/src/Symfony/Component/HttpClient/CurlHttpClient.php
Lines 254 to 256 in 524c703
- The HTTP/2 protocol does not support chunked transfer encoding.
- Normally curl would handle the correct format itself based on the HTTP protocol version of the server.
- But when the
Transfer-Encoding
header is set tochunked
explicitly, curl always uses chunked transfer encoding for the body. - This results in requests to HTTP/2 server with invalid content.
For example, given a request with FormData
['foo' => 'bar']
, an HTTP/1 server receives this:
Headers {
accept: "*/*",
"accept-encoding": "gzip",
"content-type": "multipart/form-data; boundary=1It362u0",
host: "localhost:8000",
"transfer-encoding": "chunked",
"user-agent": "Symfony HttpClient (Curl)"
}
FormData { foo: "bar" }
While an HTTP/2 server sees this:
Headers {
accept: "*/*",
"accept-encoding": "gzip",
"content-type": "multipart/form-data; boundary=Mm4P-FZG",
"user-agent": "Symfony HttpClient (Curl)"
}
FormData { foo: "2\r\n\r\n\r\n3\r\nbar\r\n2\r\n\r\n\r\ne" }
curl does omit the
transfer-encoding
header, but keeps the encoding for the body.
How to reproduce
Repo for reproducing the issue:
https://github.com/michaelhue/symfony-curl-http2-chunked-issue
The test source code can be found here:
https://github.com/michaelhue/symfony-curl-http2-chunked-issue/blob/main/test.php
Possible Solution
It seems to me that the following lines can be removed completely, which will fix the issue:
symfony/src/Symfony/Component/HttpClient/CurlHttpClient.php
Lines 254 to 256 in 524c703
However my understanding of curl
and the Symfony
internals is limited and I can't anticipate the potential fallout from this change.
I'd be happy to create a pull request with this solution, if deemed appropriate.
Additional Context
This problem was discovered while trying to debug this issue: #54491