@@ -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 ' ];
@@ -174,24 +165,24 @@ public function request(string $method, string $url, array $options = []): Respo
174165 }
175166
176167 // curl's resolve feature varies by host:port but ours varies by host only, let's handle this with our own DNS map
177- if (isset ($ multi ->dnsCache ->hostnames [$ host ])) {
178- $ options ['resolve ' ] += [$ host => $ multi ->dnsCache ->hostnames [$ host ]];
168+ if (isset ($ this -> multi ->dnsCache ->hostnames [$ host ])) {
169+ $ options ['resolve ' ] += [$ host => $ this -> multi ->dnsCache ->hostnames [$ host ]];
179170 }
180171
181- if ($ options ['resolve ' ] || $ multi ->dnsCache ->evictions ) {
172+ if ($ options ['resolve ' ] || $ this -> multi ->dnsCache ->evictions ) {
182173 // First reset any old DNS cache entries then add the new ones
183- $ resolve = $ multi ->dnsCache ->evictions ;
184- $ multi ->dnsCache ->evictions = [];
174+ $ resolve = $ this -> multi ->dnsCache ->evictions ;
175+ $ this -> multi ->dnsCache ->evictions = [];
185176
186177 if ($ resolve && 0x072A00 > CurlClientState::$ curlVersion ['version_number ' ]) {
187178 // DNS cache removals require curl 7.42 or higher
188- $ multi ->reset ();
179+ $ this -> multi ->reset ();
189180 }
190181
191182 foreach ($ options ['resolve ' ] as $ resolveHost => $ ip ) {
192183 $ resolve [] = null === $ ip ? "- $ resolveHost: $ port " : "$ resolveHost: $ port: $ ip " ;
193- $ multi ->dnsCache ->hostnames [$ resolveHost ] = $ ip ;
194- $ multi ->dnsCache ->removals ["- $ resolveHost: $ port " ] = "- $ resolveHost: $ port " ;
184+ $ this -> multi ->dnsCache ->hostnames [$ resolveHost ] = $ ip ;
185+ $ this -> multi ->dnsCache ->removals ["- $ resolveHost: $ port " ] = "- $ resolveHost: $ port " ;
195186 }
196187
197188 $ curlopts [\CURLOPT_RESOLVE ] = $ resolve ;
@@ -293,16 +284,16 @@ public function request(string $method, string $url, array $options = []): Respo
293284 $ curlopts += $ options ['extra ' ]['curl ' ];
294285 }
295286
296- if ($ pushedResponse = $ multi ->pushedResponses [$ url ] ?? null ) {
297- unset($ multi ->pushedResponses [$ url ]);
287+ if ($ pushedResponse = $ this -> multi ->pushedResponses [$ url ] ?? null ) {
288+ unset($ this -> multi ->pushedResponses [$ url ]);
298289
299290 if (self ::acceptPushForRequest ($ method , $ options , $ pushedResponse )) {
300291 $ this ->logger ?->debug(\sprintf ('Accepting pushed response: "%s %s" ' , $ method , $ url ));
301292
302293 // Reinitialize the pushed response with request's options
303294 $ ch = $ pushedResponse ->handle ;
304295 $ pushedResponse = $ pushedResponse ->response ;
305- $ pushedResponse ->__construct ($ multi , $ url , $ options , $ this ->logger );
296+ $ pushedResponse ->__construct ($ this -> multi , $ url , $ options , $ this ->logger );
306297 } else {
307298 $ this ->logger ?->debug(\sprintf ('Rejecting pushed response: "%s" ' , $ url ));
308299 $ pushedResponse = null ;
@@ -312,7 +303,7 @@ public function request(string $method, string $url, array $options = []): Respo
312303 if (!$ pushedResponse ) {
313304 $ ch = curl_init ();
314305 $ this ->logger ?->info(\sprintf ('Request: "%s %s" ' , $ method , $ url ));
315- $ curlopts += [\CURLOPT_SHARE => $ multi ->share ];
306+ $ curlopts += [\CURLOPT_SHARE => $ this -> multi ->share ];
316307 }
317308
318309 foreach ($ curlopts as $ opt => $ value ) {
@@ -325,7 +316,7 @@ public function request(string $method, string $url, array $options = []): Respo
325316 }
326317 }
327318
328- return $ pushedResponse ?? new CurlResponse ($ multi , $ ch , $ options , $ this ->logger , $ method , self ::createRedirectResolver ($ options , $ authority ), CurlClientState::$ curlVersion ['version_number ' ], $ url );
319+ return $ pushedResponse ?? new CurlResponse ($ this -> multi , $ ch , $ options , $ this ->logger , $ method , self ::createRedirectResolver ($ options , $ authority ), CurlClientState::$ curlVersion ['version_number ' ], $ url );
329320 }
330321
331322 public function stream (ResponseInterface |iterable $ responses , ?float $ timeout = null ): ResponseStreamInterface
@@ -334,11 +325,9 @@ public function stream(ResponseInterface|iterable $responses, ?float $timeout =
334325 $ responses = [$ responses ];
335326 }
336327
337- $ multi = $ this ->ensureState ();
338-
339- if ($ multi ->handle instanceof \CurlMultiHandle) {
328+ if ($ this ->multi ->handle instanceof \CurlMultiHandle) {
340329 $ active = 0 ;
341- while (\CURLM_CALL_MULTI_PERFORM === curl_multi_exec ($ multi ->handle , $ active )) {
330+ while (\CURLM_CALL_MULTI_PERFORM === curl_multi_exec ($ this -> multi ->handle , $ active )) {
342331 }
343332 }
344333
@@ -347,9 +336,7 @@ public function stream(ResponseInterface|iterable $responses, ?float $timeout =
347336
348337 public function reset (): void
349338 {
350- if (isset ($ this ->multi )) {
351- $ this ->multi ->reset ();
352- }
339+ $ this ->multi ->reset ();
353340 }
354341
355342 /**
@@ -448,16 +435,6 @@ private static function createRedirectResolver(array $options, string $authority
448435 };
449436 }
450437
451- private function ensureState (): CurlClientState
452- {
453- if (!isset ($ this ->multi )) {
454- $ this ->multi = new CurlClientState ($ this ->maxHostConnections , $ this ->maxPendingPushes );
455- $ this ->multi ->logger = $ this ->logger ;
456- }
457-
458- return $ this ->multi ;
459- }
460-
461438 private function findConstantName (int $ opt ): ?string
462439 {
463440 $ 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