44
55use Http \Client \Exception \NetworkException ;
66use Http \Client \HttpClient ;
7+ use Http \Client \Socket \Exception \ConnectionException ;
8+ use Http \Client \Socket \Exception \InvalidRequestException ;
9+ use Http \Client \Socket \Exception \SSLConnectionException ;
710use Http \Discovery \MessageFactoryDiscovery ;
811use Http \Message \ResponseFactory ;
912use Psr \Http \Message \RequestInterface ;
@@ -98,7 +101,7 @@ public function sendRequest(RequestInterface $request)
98101 * @param string $remote Entrypoint for the connection
99102 * @param bool $useSsl Whether to use ssl or not
100103 *
101- * @throws NetworkException When the connection fail
104+ * @throws ConnectionException|SSLConnectionException When the connection fail
102105 *
103106 * @return resource Socket resource
104107 */
@@ -109,15 +112,13 @@ protected function createSocket(RequestInterface $request, $remote, $useSsl)
109112 $ socket = @stream_socket_client ($ remote , $ errNo , $ errMsg , floor ($ this ->config ['timeout ' ] / 1000 ), STREAM_CLIENT_CONNECT , $ this ->config ['stream_context ' ]);
110113
111114 if (false === $ socket ) {
112- throw new NetworkException ($ errMsg , $ request );
115+ throw new ConnectionException ($ errMsg , $ request );
113116 }
114117
115118 stream_set_timeout ($ socket , floor ($ this ->config ['timeout ' ] / 1000 ), $ this ->config ['timeout ' ] % 1000 );
116119
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- }
120+ if ($ useSsl && false === @stream_socket_enable_crypto ($ socket , true , $ this ->config ['ssl_method ' ])) {
121+ throw new SSLConnectionException (sprintf ('Cannot enable tls: %s ' , error_get_last ()['message ' ]), $ request );
121122 }
122123
123124 return $ socket ;
@@ -163,18 +164,18 @@ protected function configure(array $config = [])
163164 *
164165 * @param RequestInterface $request
165166 *
166- * @throws NetworkException When no remote can be determined from the request
167+ * @throws InvalidRequestException When no remote can be determined from the request
167168 *
168169 * @return string
169170 */
170171 private function determineRemoteFromRequest (RequestInterface $ request )
171172 {
172- if ($ request ->getUri ()->getHost () == '' && ! $ request -> hasHeader ( ' Host ' ) ) {
173- throw new NetworkException ( ' Cannot find connection endpoint for this request ' , $ request );
173+ if (! $ request ->hasHeader ( ' Host ' ) && $ request -> getUri ()->getHost () === '' ) {
174+ throw new InvalidRequestException ( ' Remote is not defined and we cannot determine a connection endpoint for this request (no Host header) ' , $ request );
174175 }
175176
176177 $ host = $ request ->getUri ()->getHost ();
177- $ port = $ request ->getUri ()->getPort () ?: ($ request ->getUri ()->getScheme () == 'https ' ? 443 : 80 );
178+ $ port = $ request ->getUri ()->getPort () ?: ($ request ->getUri ()->getScheme () === 'https ' ? 443 : 80 );
178179 $ endpoint = sprintf ('%s:%s ' , $ host , $ port );
179180
180181 // If use the host header if present for the endpoint
0 commit comments