66use Http \Client \Socket \Exception \ConnectionException ;
77use Http \Client \Socket \Exception \InvalidRequestException ;
88use Http \Client \Socket \Exception \SSLConnectionException ;
9- use Http \Discovery \MessageFactoryDiscovery ;
10- use Http \Message \ResponseFactory ;
9+ use Http \Client \Socket \Exception \TimeoutException ;
1110use Psr \Http \Message \RequestInterface ;
11+ use Psr \Http \Message \ResponseInterface ;
1212use Symfony \Component \OptionsResolver \Options ;
1313use Symfony \Component \OptionsResolver \OptionsResolver ;
1414
@@ -31,38 +31,44 @@ class Client implements HttpClient
3131 'stream_context_param ' => [],
3232 'ssl ' => null ,
3333 'write_buffer_size ' => 8192 ,
34- 'ssl_method ' => STREAM_CRYPTO_METHOD_TLS_CLIENT ,
34+ 'ssl_method ' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT ,
3535 ];
3636
37+ private $ hasAsync ;
38+
3739 /**
3840 * Constructor.
3941 *
40- * @param ResponseFactory $responseFactory Response factory for creating response
41- * @param array $config {
42+ * @param array $config {
4243 *
4344 * @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address)
4445 * @var int $timeout Timeout before canceling request
4546 * @var array $stream_context_options Context options as defined in the PHP documentation
4647 * @var array $stream_context_param Context params as defined in the PHP documentation
4748 * @var bool $ssl Use ssl, default to scheme from request, false if not present
4849 * @var int $write_buffer_size Buffer when writing the request body, defaults to 8192
49- * @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLS_CLIENT
50+ * @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
5051 * }
5152 */
52- public function __construct (ResponseFactory $ responseFactory = null , array $ config = [])
53+ public function __construct ($ config1 = [], $ config2 = null , array $ config = [])
5354 {
54- if (null === $ responseFactory ) {
55- $ responseFactory = MessageFactoryDiscovery::find ();
55+ $ this ->hasAsync = PHP_VERSION_ID >= 70300 && \extension_loaded ('async ' );
56+
57+ if (\is_array ($ config1 )) {
58+ $ this ->config = $ this ->configure ($ config1 );
59+
60+ return ;
5661 }
5762
58- $ this ->responseFactory = $ responseFactory ;
63+ @trigger_error (E_USER_DEPRECATED , 'Passing a Psr\Http\Message\ResponseFactoryInterface and a Psr\Http\Message\StreamFactoryInterface to SocketClient is deprecated, and will be removed in 3.0, you should only pass config options. ' );
64+
5965 $ this ->config = $ this ->configure ($ config );
6066 }
6167
6268 /**
6369 * {@inheritdoc}
6470 */
65- public function sendRequest (RequestInterface $ request )
71+ public function sendRequest (RequestInterface $ request ): ResponseInterface
6672 {
6773 $ remote = $ this ->config ['remote_socket ' ];
6874 $ useSsl = $ this ->config ['ssl ' ];
@@ -104,13 +110,17 @@ public function sendRequest(RequestInterface $request)
104110 *
105111 * @return resource Socket resource
106112 */
107- protected function createSocket (RequestInterface $ request , $ remote , $ useSsl )
113+ protected function createSocket (RequestInterface $ request , string $ remote , bool $ useSsl )
108114 {
109115 $ errNo = null ;
110116 $ errMsg = null ;
111117 $ socket = @stream_socket_client ($ remote , $ errNo , $ errMsg , floor ($ this ->config ['timeout ' ] / 1000 ), STREAM_CLIENT_CONNECT , $ this ->config ['stream_context ' ]);
112118
113119 if (false === $ socket ) {
120+ if (110 === $ errNo ) {
121+ throw new TimeoutException ($ errMsg , $ request );
122+ }
123+
114124 throw new ConnectionException ($ errMsg , $ request );
115125 }
116126
@@ -161,7 +171,6 @@ protected function configure(array $config = [])
161171 /**
162172 * Return remote socket from the request.
163173 *
164- * @param RequestInterface $request
165174 *
166175 * @throws InvalidRequestException When no remote can be determined from the request
167176 *
@@ -182,6 +191,10 @@ private function determineRemoteFromRequest(RequestInterface $request)
182191 $ endpoint = $ request ->getHeaderLine ('Host ' );
183192 }
184193
194+ if ($ this ->hasAsync ) {
195+ return sprintf ('async-tcp://%s ' , $ endpoint );
196+ }
197+
185198 return sprintf ('tcp://%s ' , $ endpoint );
186199 }
187200}
0 commit comments