22
33namespace Http \Client \Socket ;
44
5- use Http \Client \Exception \NetworkException ;
65use Http \Client \HttpClient ;
6+ use Http \Client \Socket \Exception \ConnectionException ;
7+ use Http \Client \Socket \Exception \InvalidRequestException ;
8+ use Http \Client \Socket \Exception \SSLConnectionException ;
79use Http \Discovery \MessageFactoryDiscovery ;
810use Http \Message \ResponseFactory ;
911use Psr \Http \Message \RequestInterface ;
@@ -98,7 +100,7 @@ public function sendRequest(RequestInterface $request)
98100 * @param string $remote Entrypoint for the connection
99101 * @param bool $useSsl Whether to use ssl or not
100102 *
101- * @throws NetworkException When the connection fail
103+ * @throws ConnectionException|SSLConnectionException When the connection fail
102104 *
103105 * @return resource Socket resource
104106 */
@@ -109,15 +111,13 @@ protected function createSocket(RequestInterface $request, $remote, $useSsl)
109111 $ socket = @stream_socket_client ($ remote , $ errNo , $ errMsg , floor ($ this ->config ['timeout ' ] / 1000 ), STREAM_CLIENT_CONNECT , $ this ->config ['stream_context ' ]);
110112
111113 if (false === $ socket ) {
112- throw new NetworkException ($ errMsg , $ request );
114+ throw new ConnectionException ($ errMsg , $ request );
113115 }
114116
115117 stream_set_timeout ($ socket , floor ($ this ->config ['timeout ' ] / 1000 ), $ this ->config ['timeout ' ] % 1000 );
116118
117- if ($ useSsl ) {
118- if (false === @stream_socket_enable_crypto ($ socket , true , $ this ->config ['ssl_method ' ])) {
119- throw new NetworkException (sprintf ('Cannot enable tls: %s ' , error_get_last ()['message ' ]), $ request );
120- }
119+ if ($ useSsl && false === @stream_socket_enable_crypto ($ socket , true , $ this ->config ['ssl_method ' ])) {
120+ throw new SSLConnectionException (sprintf ('Cannot enable tls: %s ' , error_get_last ()['message ' ]), $ request );
121121 }
122122
123123 return $ socket ;
@@ -163,18 +163,18 @@ protected function configure(array $config = [])
163163 *
164164 * @param RequestInterface $request
165165 *
166- * @throws NetworkException When no remote can be determined from the request
166+ * @throws InvalidRequestException When no remote can be determined from the request
167167 *
168168 * @return string
169169 */
170170 private function determineRemoteFromRequest (RequestInterface $ request )
171171 {
172- if ($ request ->getUri ()->getHost () == '' && ! $ request -> hasHeader ( ' Host ' ) ) {
173- throw new NetworkException ( ' Cannot find connection endpoint for this request ' , $ request );
172+ if (! $ request ->hasHeader ( ' Host ' ) && $ request -> getUri ()->getHost () === '' ) {
173+ throw new InvalidRequestException ( ' Remote is not defined and we cannot determine a connection endpoint for this request (no Host header) ' , $ request );
174174 }
175175
176176 $ host = $ request ->getUri ()->getHost ();
177- $ port = $ request ->getUri ()->getPort () ?: ($ request ->getUri ()->getScheme () == 'https ' ? 443 : 80 );
177+ $ port = $ request ->getUri ()->getPort () ?: ($ request ->getUri ()->getScheme () === 'https ' ? 443 : 80 );
178178 $ endpoint = sprintf ('%s:%s ' , $ host , $ port );
179179
180180 // If use the host header if present for the endpoint
0 commit comments