@@ -51,9 +51,6 @@ final class CurlHttpClient implements HttpClientInterface, LoggerAwareInterface,
5151
5252 private ?LoggerInterface $ logger = null ;
5353
54- private int $ maxHostConnections ;
55- private int $ maxPendingPushes ;
56-
5754 /**
5855 * An internal object to share state between the client and its responses.
5956 */
@@ -72,31 +69,25 @@ public function __construct(array $defaultOptions = [], int $maxHostConnections
7269 throw new \LogicException ('You cannot use the "Symfony\Component\HttpClient\CurlHttpClient" as the "curl" extension is not installed. ' );
7370 }
7471
75- $ this ->maxHostConnections = $ maxHostConnections ;
76- $ this ->maxPendingPushes = $ maxPendingPushes ;
77-
7872 $ this ->defaultOptions ['buffer ' ] ??= self ::shouldBuffer (...);
7973
8074 if ($ defaultOptions ) {
8175 [, $ this ->defaultOptions ] = self ::prepareRequest (null , null , $ defaultOptions , $ this ->defaultOptions );
8276 }
77+
78+ $ this ->multi = new CurlClientState ($ maxHostConnections , $ maxPendingPushes );
8379 }
8480
8581 public function setLogger (LoggerInterface $ logger ): void
8682 {
87- $ this ->logger = $ logger ;
88- if (isset ($ this ->multi )) {
89- $ this ->multi ->logger = $ logger ;
90- }
83+ $ this ->logger = $ this ->multi ->logger = $ logger ;
9184 }
9285
9386 /**
9487 * @see HttpClientInterface::OPTIONS_DEFAULTS for available options
9588 */
9689 public function request (string $ method , string $ url , array $ options = []): ResponseInterface
9790 {
98- $ multi = $ this ->ensureState ();
99-
10091 [$ url , $ options ] = self ::prepareRequest ($ method , $ url , $ options , $ this ->defaultOptions );
10192 $ scheme = $ url ['scheme ' ];
10293 $ authority = $ url ['authority ' ];
@@ -176,24 +167,24 @@ public function request(string $method, string $url, array $options = []): Respo
176167 }
177168
178169 // curl's resolve feature varies by host:port but ours varies by host only, let's handle this with our own DNS map
179- if (isset ($ multi ->dnsCache ->hostnames [$ host ])) {
180- $ options ['resolve ' ] += [$ host => $ multi ->dnsCache ->hostnames [$ host ]];
170+ if (isset ($ this -> multi ->dnsCache ->hostnames [$ host ])) {
171+ $ options ['resolve ' ] += [$ host => $ this -> multi ->dnsCache ->hostnames [$ host ]];
181172 }
182173
183- if ($ options ['resolve ' ] || $ multi ->dnsCache ->evictions ) {
174+ if ($ options ['resolve ' ] || $ this -> multi ->dnsCache ->evictions ) {
184175 // First reset any old DNS cache entries then add the new ones
185- $ resolve = $ multi ->dnsCache ->evictions ;
186- $ multi ->dnsCache ->evictions = [];
176+ $ resolve = $ this -> multi ->dnsCache ->evictions ;
177+ $ this -> multi ->dnsCache ->evictions = [];
187178
188179 if ($ resolve && 0x072A00 > CurlClientState::$ curlVersion ['version_number ' ]) {
189180 // DNS cache removals require curl 7.42 or higher
190- $ multi ->reset ();
181+ $ this -> multi ->reset ();
191182 }
192183
193184 foreach ($ options ['resolve ' ] as $ resolveHost => $ ip ) {
194185 $ resolve [] = null === $ ip ? "- $ resolveHost: $ port " : "$ resolveHost: $ port: $ ip " ;
195- $ multi ->dnsCache ->hostnames [$ resolveHost ] = $ ip ;
196- $ multi ->dnsCache ->removals ["- $ resolveHost: $ port " ] = "- $ resolveHost: $ port " ;
186+ $ this -> multi ->dnsCache ->hostnames [$ resolveHost ] = $ ip ;
187+ $ this -> multi ->dnsCache ->removals ["- $ resolveHost: $ port " ] = "- $ resolveHost: $ port " ;
197188 }
198189
199190 $ curlopts [\CURLOPT_RESOLVE ] = $ resolve ;
@@ -299,16 +290,16 @@ public function request(string $method, string $url, array $options = []): Respo
299290 $ curlopts += $ options ['extra ' ]['curl ' ];
300291 }
301292
302- if ($ pushedResponse = $ multi ->pushedResponses [$ url ] ?? null ) {
303- unset($ multi ->pushedResponses [$ url ]);
293+ if ($ pushedResponse = $ this -> multi ->pushedResponses [$ url ] ?? null ) {
294+ unset($ this -> multi ->pushedResponses [$ url ]);
304295
305296 if (self ::acceptPushForRequest ($ method , $ options , $ pushedResponse )) {
306297 $ this ->logger ?->debug(\sprintf ('Accepting pushed response: "%s %s" ' , $ method , $ url ));
307298
308299 // Reinitialize the pushed response with request's options
309300 $ ch = $ pushedResponse ->handle ;
310301 $ pushedResponse = $ pushedResponse ->response ;
311- $ pushedResponse ->__construct ($ multi , $ url , $ options , $ this ->logger );
302+ $ pushedResponse ->__construct ($ this -> multi , $ url , $ options , $ this ->logger );
312303 } else {
313304 $ this ->logger ?->debug(\sprintf ('Rejecting pushed response: "%s" ' , $ url ));
314305 $ pushedResponse = null ;
@@ -318,7 +309,7 @@ public function request(string $method, string $url, array $options = []): Respo
318309 if (!$ pushedResponse ) {
319310 $ ch = curl_init ();
320311 $ this ->logger ?->info(\sprintf ('Request: "%s %s" ' , $ method , $ url ));
321- $ curlopts += [\CURLOPT_SHARE => $ multi ->share ];
312+ $ curlopts += [\CURLOPT_SHARE => $ this -> multi ->share ];
322313 }
323314
324315 foreach ($ curlopts as $ opt => $ value ) {
@@ -331,7 +322,7 @@ public function request(string $method, string $url, array $options = []): Respo
331322 }
332323 }
333324
334- return $ pushedResponse ?? new CurlResponse ($ multi , $ ch , $ options , $ this ->logger , $ method , self ::createRedirectResolver ($ options , $ authority ), CurlClientState::$ curlVersion ['version_number ' ], $ url );
325+ return $ pushedResponse ?? new CurlResponse ($ this -> multi , $ ch , $ options , $ this ->logger , $ method , self ::createRedirectResolver ($ options , $ authority ), CurlClientState::$ curlVersion ['version_number ' ], $ url );
335326 }
336327
337328 public function stream (ResponseInterface |iterable $ responses , ?float $ timeout = null ): ResponseStreamInterface
@@ -340,11 +331,9 @@ public function stream(ResponseInterface|iterable $responses, ?float $timeout =
340331 $ responses = [$ responses ];
341332 }
342333
343- $ multi = $ this ->ensureState ();
344-
345- if ($ multi ->handle instanceof \CurlMultiHandle) {
334+ if ($ this ->multi ->handle instanceof \CurlMultiHandle) {
346335 $ active = 0 ;
347- while (\CURLM_CALL_MULTI_PERFORM === curl_multi_exec ($ multi ->handle , $ active )) {
336+ while (\CURLM_CALL_MULTI_PERFORM === curl_multi_exec ($ this -> multi ->handle , $ active )) {
348337 }
349338 }
350339
@@ -353,9 +342,7 @@ public function stream(ResponseInterface|iterable $responses, ?float $timeout =
353342
354343 public function reset (): void
355344 {
356- if (isset ($ this ->multi )) {
357- $ this ->multi ->reset ();
358- }
345+ $ this ->multi ->reset ();
359346 }
360347
361348 /**
@@ -454,16 +441,6 @@ private static function createRedirectResolver(array $options, string $authority
454441 };
455442 }
456443
457- private function ensureState (): CurlClientState
458- {
459- if (!isset ($ this ->multi )) {
460- $ this ->multi = new CurlClientState ($ this ->maxHostConnections , $ this ->maxPendingPushes );
461- $ this ->multi ->logger = $ this ->logger ;
462- }
463-
464- return $ this ->multi ;
465- }
466-
467444 private function findConstantName (int $ opt ): ?string
468445 {
469446 $ constants = array_filter (get_defined_constants (), static fn ($ v , $ k ) => $ v === $ opt && 'C ' === $ k [0 ] && (str_starts_with ($ k , 'CURLOPT_ ' ) || str_starts_with ($ k , 'CURLINFO_ ' )), \ARRAY_FILTER_USE_BOTH );
0 commit comments