@@ -204,8 +204,14 @@ public function request(string $method, string $url, array $options = []): Respo
204204 if (\extension_loaded ('zlib ' ) && !isset ($ options ['normalized_headers ' ]['accept-encoding ' ])) {
205205 $ options ['headers ' ][] = 'Accept-Encoding: gzip ' ; // Expose only one encoding, some servers mess up when more are provided
206206 }
207+ $ body = $ options ['body ' ];
207208
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+ }
209215 if (': ' === $ header [-2 ] && \strlen ($ header ) - 2 === strpos ($ header , ': ' )) {
210216 // curl requires a special syntax to send empty headers
211217 $ curlopts [\CURLOPT_HTTPHEADER ][] = substr_replace ($ header , '; ' , -2 );
@@ -221,7 +227,7 @@ public function request(string $method, string $url, array $options = []): Respo
221227 }
222228 }
223229
224- if (!\is_string ($ body = $ options [ ' body ' ] )) {
230+ if (!\is_string ($ body )) {
225231 if (\is_resource ($ body )) {
226232 $ curlopts [\CURLOPT_INFILE ] = $ body ;
227233 } else {
@@ -233,15 +239,16 @@ public function request(string $method, string $url, array $options = []): Respo
233239 }
234240
235241 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 ' );
239246 }
240247
241248 if ('POST ' !== $ method ) {
242249 $ curlopts [\CURLOPT_UPLOAD ] = true ;
243250
244- if (!isset ($ options ['normalized_headers ' ]['content-type ' ])) {
251+ if (!isset ($ options ['normalized_headers ' ]['content-type ' ]) && 0 !== ( $ curlopts [\ CURLOPT_INFILESIZE ] ?? null ) ) {
245252 $ curlopts [\CURLOPT_HTTPHEADER ][] = 'Content-Type: application/x-www-form-urlencoded ' ;
246253 }
247254 }
0 commit comments