@@ -197,8 +197,14 @@ public function request(string $method, string $url, array $options = []): Respo
197
197
if (\extension_loaded ('zlib ' ) && !isset ($ options ['normalized_headers ' ]['accept-encoding ' ])) {
198
198
$ options ['headers ' ][] = 'Accept-Encoding: gzip ' ; // Expose only one encoding, some servers mess up when more are provided
199
199
}
200
+ $ body = $ options ['body ' ];
200
201
201
- foreach ($ options ['headers ' ] as $ header ) {
202
+ foreach ($ options ['headers ' ] as $ i => $ header ) {
203
+ if (\is_string ($ body ) && '' !== $ body && 0 === stripos ($ header , 'Content-Length: ' )) {
204
+ // Let curl handle Content-Length headers
205
+ unset($ options ['headers ' ][$ i ]);
206
+ continue ;
207
+ }
202
208
if (': ' === $ header [-2 ] && \strlen ($ header ) - 2 === strpos ($ header , ': ' )) {
203
209
// curl requires a special syntax to send empty headers
204
210
$ curlopts [\CURLOPT_HTTPHEADER ][] = substr_replace ($ header , '; ' , -2 );
@@ -214,7 +220,7 @@ public function request(string $method, string $url, array $options = []): Respo
214
220
}
215
221
}
216
222
217
- if (!\is_string ($ body = $ options [ ' body ' ] )) {
223
+ if (!\is_string ($ body )) {
218
224
if (\is_resource ($ body )) {
219
225
$ curlopts [\CURLOPT_INFILE ] = $ body ;
220
226
} else {
@@ -226,15 +232,16 @@ public function request(string $method, string $url, array $options = []): Respo
226
232
}
227
233
228
234
if (isset ($ options ['normalized_headers ' ]['content-length ' ][0 ])) {
229
- $ curlopts [\CURLOPT_INFILESIZE ] = substr ($ options ['normalized_headers ' ]['content-length ' ][0 ], \strlen ('Content-Length: ' ));
230
- } elseif (!isset ($ options ['normalized_headers ' ]['transfer-encoding ' ])) {
231
- $ curlopts [\CURLOPT_HTTPHEADER ][] = 'Transfer-Encoding: chunked ' ; // Enable chunked request bodies
235
+ $ curlopts [\CURLOPT_INFILESIZE ] = (int ) substr ($ options ['normalized_headers ' ]['content-length ' ][0 ], \strlen ('Content-Length: ' ));
236
+ }
237
+ if (!isset ($ options ['normalized_headers ' ]['transfer-encoding ' ])) {
238
+ $ curlopts [\CURLOPT_HTTPHEADER ][] = 'Transfer-Encoding: ' .(isset ($ curlopts [\CURLOPT_INFILESIZE ]) ? '' : ' chunked ' );
232
239
}
233
240
234
241
if ('POST ' !== $ method ) {
235
242
$ curlopts [\CURLOPT_UPLOAD ] = true ;
236
243
237
- if (!isset ($ options ['normalized_headers ' ]['content-type ' ])) {
244
+ if (!isset ($ options ['normalized_headers ' ]['content-type ' ]) && 0 !== ( $ curlopts [\ CURLOPT_INFILESIZE ] ?? null ) ) {
238
245
$ curlopts [\CURLOPT_HTTPHEADER ][] = 'Content-Type: application/x-www-form-urlencoded ' ;
239
246
}
240
247
}
0 commit comments