14
14
15
15
use chillerlan \HTTP \Utils \HeaderUtil ;
16
16
use 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 ;
19
20
20
21
/**
22
+ * A http client via PHP streams
21
23
*
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()
22
28
*/
23
29
class StreamClient extends HTTPClientAbstract{
24
30
25
31
/**
26
32
* @inheritDoc
33
+ * @throws \Exception|\chillerlan\HTTP\ClientException
27
34
*/
28
35
public function sendRequest (RequestInterface $ request ):ResponseInterface {
29
36
30
37
$ 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) );
32
39
33
- throw new ClientException ($ errstr , $ errno );
40
+ throw new Exception ($ errstr , $ errno );
34
41
};
35
42
36
43
set_error_handler ($ errorHandler );
37
44
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
+ }
42
56
43
57
restore_error_handler ();
44
58
59
+ if ($ exception !== null ){
60
+ throw new ClientException ($ exception ->getMessage ());
61
+ }
62
+
45
63
$ body = $ this ->streamFactory !== null
46
64
? $ this ->streamFactory ->createStream ()
47
65
: $ response ->getBody ()
@@ -58,9 +76,11 @@ public function sendRequest(RequestInterface $request):ResponseInterface{
58
76
*/
59
77
protected function getContextOptions (RequestInterface $ request ):array {
60
78
$ 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
+ }
64
84
65
85
$ options = [
66
86
'http ' => [
@@ -81,8 +101,10 @@ protected function getContextOptions(RequestInterface $request):array{
81
101
],
82
102
];
83
103
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
+ }
86
108
87
109
return $ options ;
88
110
}
0 commit comments