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