1414
1515use chillerlan \HTTP \Utils \HeaderUtil ;
1616use Psr \Http \Message \{RequestInterface , ResponseInterface };
17- use function explode , file_get_contents , get_headers , in_array , intval , restore_error_handler ,
18- set_error_handler , stream_context_create , strtolower , str_starts_with , trim ;
17+ use Exception , Throwable ;
18+ use function explode , file_get_contents , get_headers , in_array , intval , is_file , restore_error_handler ,
19+ set_error_handler , sprintf , stream_context_create , strtolower , str_starts_with , trim ;
1920
2021/**
22+ * A http client via PHP streams
2123 *
24+ * (I'm not exactly sure why I'm keeping this - use CurlClient in production)
25+ *
26+ * @see \file_get_contents()
27+ * @see \stream_context_create()
2228 */
2329class StreamClient extends HTTPClientAbstract{
2430
2531 /**
2632 * @inheritDoc
33+ * @throws \Exception|\chillerlan\HTTP\ClientException
2734 */
2835 public function sendRequest (RequestInterface $ request ):ResponseInterface {
2936
3037 $ errorHandler = function (int $ errno , string $ errstr ):bool {
31- $ this ->logger ->error ('StreamClient error # ' . $ errno. ' : ' . $ errstr );
38+ $ this ->logger ->error (sprintf ( 'StreamClient error #%s: %s ' , $ errno, $ errstr) );
3239
33- throw new ClientException ($ errstr , $ errno );
40+ throw new Exception ($ errstr , $ errno );
3441 };
3542
3643 set_error_handler ($ errorHandler );
3744
38- $ context = stream_context_create ($ this ->getContextOptions ($ request ));
39- $ requestUri = (string )$ request ->getUri ()->withFragment ('' );
40- $ responseBody = file_get_contents ($ requestUri , false , $ context );
41- $ response = $ this ->createResponse (get_headers ($ requestUri , true , $ context ));
45+ $ exception = null ;
46+
47+ try {
48+ $ context = stream_context_create ($ this ->getContextOptions ($ request ));
49+ $ requestUri = (string )$ request ->getUri ()->withFragment ('' );
50+ $ responseBody = file_get_contents ($ requestUri , false , $ context );
51+ $ response = $ this ->createResponse (get_headers ($ requestUri , true , $ context ));
52+ }
53+ catch (Throwable $ e ){
54+ $ exception = $ e ;
55+ }
4256
4357 restore_error_handler ();
4458
59+ if ($ exception !== null ){
60+ throw new ClientException ($ exception ->getMessage ());
61+ }
62+
4563 $ body = $ this ->streamFactory !== null
4664 ? $ this ->streamFactory ->createStream ()
4765 : $ response ->getBody ()
@@ -58,9 +76,11 @@ public function sendRequest(RequestInterface $request):ResponseInterface{
5876 */
5977 protected function getContextOptions (RequestInterface $ request ):array {
6078 $ method = $ request ->getMethod ();
61- $ body = in_array ($ method , ['DELETE ' , 'PATCH ' , 'POST ' , 'PUT ' ], true )
62- ? $ request ->getBody ()->getContents ()
63- : null ;
79+ $ body = null ;
80+
81+ if (in_array ($ method , ['DELETE ' , 'PATCH ' , 'POST ' , 'PUT ' ], true )){
82+ $ body = $ request ->getBody ()->getContents ();
83+ }
6484
6585 $ options = [
6686 'http ' => [
@@ -81,8 +101,10 @@ protected function getContextOptions(RequestInterface $request):array{
81101 ],
82102 ];
83103
84- $ ca = ($ this ->options ->ca_info_is_path ) ? 'capath ' : 'cafile ' ;
85- $ options ['ssl ' ][$ ca ] = $ this ->options ->ca_info ;
104+ if ($ this ->options ->ca_info ){
105+ $ ca = (is_file ($ this ->options ->ca_info )) ? 'capath ' : 'cafile ' ;
106+ $ options ['ssl ' ][$ ca ] = $ this ->options ->ca_info ;
107+ }
86108
87109 return $ options ;
88110 }
0 commit comments