@@ -202,14 +202,7 @@ public function request(string $method, string $url, array $options = []): Respo
202
202
$ options ['headers ' ][] = 'Accept-Encoding: gzip ' ; // Expose only one encoding, some servers mess up when more are provided
203
203
}
204
204
205
- $ hasContentLength = isset ($ options ['normalized_headers ' ]['content-length ' ][0 ]);
206
-
207
- foreach ($ options ['headers ' ] as $ i => $ header ) {
208
- if ($ hasContentLength && 0 === stripos ($ header , 'Content-Length: ' )) {
209
- // Let curl handle Content-Length headers
210
- unset($ options ['headers ' ][$ i ]);
211
- continue ;
212
- }
205
+ foreach ($ options ['headers ' ] as $ header ) {
213
206
if (': ' === $ header [-2 ] && \strlen ($ header ) - 2 === strpos ($ header , ': ' )) {
214
207
// curl requires a special syntax to send empty headers
215
208
$ curlopts [\CURLOPT_HTTPHEADER ][] = substr_replace ($ header , '; ' , -2 );
@@ -236,7 +229,7 @@ public function request(string $method, string $url, array $options = []): Respo
236
229
};
237
230
}
238
231
239
- if ($ hasContentLength ) {
232
+ if (isset ( $ options [ ' normalized_headers ' ][ ' content-length ' ][ 0 ]) ) {
240
233
$ curlopts [\CURLOPT_INFILESIZE ] = substr ($ options ['normalized_headers ' ]['content-length ' ][0 ], \strlen ('Content-Length: ' ));
241
234
} elseif (!isset ($ options ['normalized_headers ' ]['transfer-encoding ' ])) {
242
235
$ curlopts [\CURLOPT_HTTPHEADER ][] = 'Transfer-Encoding: chunked ' ; // Enable chunked request bodies
@@ -249,7 +242,7 @@ public function request(string $method, string $url, array $options = []): Respo
249
242
$ curlopts [\CURLOPT_HTTPHEADER ][] = 'Content-Type: application/x-www-form-urlencoded ' ;
250
243
}
251
244
}
252
- } elseif ('' !== $ body || 'POST ' === $ method || $ hasContentLength ) {
245
+ } elseif ('' !== $ body || 'POST ' === $ method ) {
253
246
$ curlopts [\CURLOPT_POSTFIELDS ] = $ body ;
254
247
}
255
248
@@ -406,16 +399,26 @@ private static function createRedirectResolver(array $options, string $host): \C
406
399
}
407
400
}
408
401
409
- return static function ($ ch , string $ location ) use ($ redirectHeaders ) {
402
+ return static function ($ ch , string $ location, bool $ noContent ) use (& $ redirectHeaders ) {
410
403
try {
411
404
$ location = self ::parseUrl ($ location );
412
405
} catch (InvalidArgumentException $ e ) {
413
406
return null ;
414
407
}
415
408
409
+ if ($ noContent && $ redirectHeaders ) {
410
+ $ filterContentHeaders = static function ($ h ) {
411
+ return 0 !== stripos ($ h , 'Content-Length: ' ) && 0 !== stripos ($ h , 'Content-Type: ' ) && 0 !== stripos ($ h , 'Transfer-Encoding: ' );
412
+ };
413
+ $ redirectHeaders ['no_auth ' ] = array_filter ($ redirectHeaders ['no_auth ' ], $ filterContentHeaders );
414
+ $ redirectHeaders ['with_auth ' ] = array_filter ($ redirectHeaders ['with_auth ' ], $ filterContentHeaders );
415
+ }
416
+
416
417
if ($ redirectHeaders && $ host = parse_url ('http: ' .$ location ['authority ' ], \PHP_URL_HOST )) {
417
418
$ requestHeaders = $ redirectHeaders ['host ' ] === $ host ? $ redirectHeaders ['with_auth ' ] : $ redirectHeaders ['no_auth ' ];
418
419
curl_setopt ($ ch , \CURLOPT_HTTPHEADER , $ requestHeaders );
420
+ } elseif ($ noContent && $ redirectHeaders ) {
421
+ curl_setopt ($ ch , \CURLOPT_HTTPHEADER , $ redirectHeaders ['with_auth ' ]);
419
422
}
420
423
421
424
$ url = self ::parseUrl (curl_getinfo ($ ch , \CURLINFO_EFFECTIVE_URL ));
0 commit comments