6
6
use Http \Client \Socket \Exception \ConnectionException ;
7
7
use Http \Client \Socket \Exception \InvalidRequestException ;
8
8
use Http \Client \Socket \Exception \SSLConnectionException ;
9
- use Http \Discovery \MessageFactoryDiscovery ;
10
- use Http \Message \ResponseFactory ;
9
+ use Http \Client \Socket \Exception \TimeoutException ;
11
10
use Psr \Http \Message \RequestInterface ;
11
+ use Psr \Http \Message \ResponseInterface ;
12
12
use Symfony \Component \OptionsResolver \Options ;
13
13
use Symfony \Component \OptionsResolver \OptionsResolver ;
14
14
@@ -31,38 +31,44 @@ class Client implements HttpClient
31
31
'stream_context_param ' => [],
32
32
'ssl ' => null ,
33
33
'write_buffer_size ' => 8192 ,
34
- 'ssl_method ' => STREAM_CRYPTO_METHOD_TLS_CLIENT ,
34
+ 'ssl_method ' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT ,
35
35
];
36
36
37
+ private $ hasAsync ;
38
+
37
39
/**
38
40
* Constructor.
39
41
*
40
- * @param ResponseFactory $responseFactory Response factory for creating response
41
- * @param array $config {
42
+ * @param array $config {
42
43
*
43
44
* @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address)
44
45
* @var int $timeout Timeout before canceling request
45
46
* @var array $stream_context_options Context options as defined in the PHP documentation
46
47
* @var array $stream_context_param Context params as defined in the PHP documentation
47
48
* @var bool $ssl Use ssl, default to scheme from request, false if not present
48
49
* @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
50
51
* }
51
52
*/
52
- public function __construct (ResponseFactory $ responseFactory = null , array $ config = [])
53
+ public function __construct ($ config1 = [], $ config2 = null , array $ config = [])
53
54
{
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 ;
56
61
}
57
62
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
+
59
65
$ this ->config = $ this ->configure ($ config );
60
66
}
61
67
62
68
/**
63
69
* {@inheritdoc}
64
70
*/
65
- public function sendRequest (RequestInterface $ request )
71
+ public function sendRequest (RequestInterface $ request ): ResponseInterface
66
72
{
67
73
$ remote = $ this ->config ['remote_socket ' ];
68
74
$ useSsl = $ this ->config ['ssl ' ];
@@ -104,13 +110,17 @@ public function sendRequest(RequestInterface $request)
104
110
*
105
111
* @return resource Socket resource
106
112
*/
107
- protected function createSocket (RequestInterface $ request , $ remote , $ useSsl )
113
+ protected function createSocket (RequestInterface $ request , string $ remote , bool $ useSsl )
108
114
{
109
115
$ errNo = null ;
110
116
$ errMsg = null ;
111
117
$ socket = @stream_socket_client ($ remote , $ errNo , $ errMsg , floor ($ this ->config ['timeout ' ] / 1000 ), STREAM_CLIENT_CONNECT , $ this ->config ['stream_context ' ]);
112
118
113
119
if (false === $ socket ) {
120
+ if (110 === $ errNo ) {
121
+ throw new TimeoutException ($ errMsg , $ request );
122
+ }
123
+
114
124
throw new ConnectionException ($ errMsg , $ request );
115
125
}
116
126
@@ -161,7 +171,6 @@ protected function configure(array $config = [])
161
171
/**
162
172
* Return remote socket from the request.
163
173
*
164
- * @param RequestInterface $request
165
174
*
166
175
* @throws InvalidRequestException When no remote can be determined from the request
167
176
*
@@ -182,6 +191,10 @@ private function determineRemoteFromRequest(RequestInterface $request)
182
191
$ endpoint = $ request ->getHeaderLine ('Host ' );
183
192
}
184
193
194
+ if ($ this ->hasAsync ) {
195
+ return sprintf ('async-tcp://%s ' , $ endpoint );
196
+ }
197
+
185
198
return sprintf ('tcp://%s ' , $ endpoint );
186
199
}
187
200
}
0 commit comments