2525namespace OC \Http \Client ;
2626
2727use GuzzleHttp \Client as GuzzleClient ;
28- use GuzzleHttp \HandlerStack ;
29- use GuzzleHttp \Middleware ;
28+ use GuzzleHttp \RequestOptions ;
3029use OCP \Http \Client \IClient ;
3130use OCP \Http \Client \IResponse ;
3231use OCP \ICertificateManager ;
3332use OCP \IConfig ;
34- use Psr \Http \Message \RequestInterface ;
3533
3634/**
3735 * Class Client
@@ -45,9 +43,6 @@ class Client implements IClient {
4543 private $ config ;
4644 /** @var ICertificateManager */
4745 private $ certificateManager ;
48- private $ configured = false ;
49- /** @var HandlerStack */
50- private $ stack ;
5146
5247 /**
5348 * @param IConfig $config
@@ -57,74 +52,62 @@ class Client implements IClient {
5752 public function __construct (
5853 IConfig $ config ,
5954 ICertificateManager $ certificateManager ,
60- GuzzleClient $ client ,
61- HandlerStack $ stack
55+ GuzzleClient $ client
6256 ) {
6357 $ this ->config = $ config ;
6458 $ this ->client = $ client ;
65- $ this ->stack = $ stack ;
6659 $ this ->certificateManager = $ certificateManager ;
6760 }
6861
69- /**
70- * Sets the default options to the client
71- */
72- private function setDefaultOptions () {
73- if ($ this ->configured ) {
74- return ;
75- }
76- $ this ->configured = true ;
62+ private function buildRequestOptions (array $ options ): array {
63+ $ defaults = [
64+ RequestOptions::PROXY => $ this ->getProxyUri (),
65+ RequestOptions::VERIFY => $ this ->getCertBundle (),
66+ ];
7767
78- $ this ->stack ->push (Middleware::mapRequest (function (RequestInterface $ request ) {
79- return $ request
80- ->withHeader ('User-Agent ' , 'Nextcloud Server Crawler ' );
81- }));
82- }
68+ $ options = array_merge ($ defaults , $ options );
8369
84- private function getRequestOptions () {
85- $ options = [
86- 'verify ' => $ this ->getCertBundle (),
87- ];
88- $ proxyUri = $ this ->getProxyUri ();
89- if ($ proxyUri !== '' ) {
90- $ options ['proxy ' ] = $ proxyUri ;
70+ if (!isset ($ options [RequestOptions::HEADERS ]['User-Agent ' ])) {
71+ $ options [RequestOptions::HEADERS ]['User-Agent ' ] = 'Nextcloud Server Crawler ' ;
9172 }
73+
9274 return $ options ;
9375 }
9476
95- private function getCertBundle () {
77+ private function getCertBundle (): string {
9678 if ($ this ->certificateManager ->listCertificates () !== []) {
9779 return $ this ->certificateManager ->getAbsoluteBundlePath ();
98- } else {
99- // If the instance is not yet setup we need to use the static path as
100- // $this->certificateManager->getAbsoluteBundlePath() tries to instantiiate
101- // a view
102- if ($ this ->config ->getSystemValue ('installed ' , false )) {
103- return $ this ->certificateManager ->getAbsoluteBundlePath (null );
104- } else {
105- return \OC ::$ SERVERROOT . '/resources/config/ca-bundle.crt ' ;
106- }
10780 }
81+
82+ // If the instance is not yet setup we need to use the static path as
83+ // $this->certificateManager->getAbsoluteBundlePath() tries to instantiiate
84+ // a view
85+ if ($ this ->config ->getSystemValue ('installed ' , false )) {
86+ return $ this ->certificateManager ->getAbsoluteBundlePath (null );
87+ }
88+
89+ return \OC ::$ SERVERROOT . '/resources/config/ca-bundle.crt ' ;
10890 }
10991
11092 /**
11193 * Get the proxy URI
11294 *
113- * @return string
95+ * @return string|null
11496 */
115- private function getProxyUri (): string {
97+ private function getProxyUri (): ? string {
11698 $ proxyHost = $ this ->config ->getSystemValue ('proxy ' , null );
117- $ proxyUserPwd = $ this ->config ->getSystemValue ('proxyuserpwd ' , null );
118- $ proxyUri = '' ;
11999
120- if ($ proxyUserPwd ! == null ) {
121- $ proxyUri .= $ proxyUserPwd . ' @ ' ;
100+ if ($ proxyHost = == null ) {
101+ return null ;
122102 }
123- if ($ proxyHost !== null ) {
124- $ proxyUri .= $ proxyHost ;
103+
104+ $ proxyUserPwd = $ this ->config ->getSystemValue ('proxyuserpwd ' , null );
105+
106+ if ($ proxyUserPwd === null ) {
107+ return $ proxyHost ;
125108 }
126109
127- return $ proxyUri ;
110+ return $ proxyUserPwd . ' @ ' . $ proxyHost ;
128111 }
129112
130113 /**
@@ -157,8 +140,7 @@ private function getProxyUri(): string {
157140 * @throws \Exception If the request could not get completed
158141 */
159142 public function get (string $ uri , array $ options = []): IResponse {
160- $ this ->setDefaultOptions ();
161- $ response = $ this ->client ->request ('get ' , $ uri , array_merge ($ this ->getRequestOptions (), $ options ));
143+ $ response = $ this ->client ->request ('get ' , $ uri , $ this ->buildRequestOptions ($ options ));
162144 $ isStream = isset ($ options ['stream ' ]) && $ options ['stream ' ];
163145 return new Response ($ response , $ isStream );
164146 }
@@ -188,8 +170,7 @@ public function get(string $uri, array $options = []): IResponse {
188170 * @throws \Exception If the request could not get completed
189171 */
190172 public function head (string $ uri , array $ options = []): IResponse {
191- $ this ->setDefaultOptions ();
192- $ response = $ this ->client ->request ('head ' , $ uri , array_merge ($ this ->getRequestOptions (), $ options ));
173+ $ response = $ this ->client ->request ('head ' , $ uri , $ this ->buildRequestOptions ($ options ));
193174 return new Response ($ response );
194175 }
195176
@@ -223,12 +204,11 @@ public function head(string $uri, array $options = []): IResponse {
223204 * @throws \Exception If the request could not get completed
224205 */
225206 public function post (string $ uri , array $ options = []): IResponse {
226- $ this ->setDefaultOptions ();
227207 if (isset ($ options ['body ' ]) && is_array ($ options ['body ' ])) {
228208 $ options ['form_params ' ] = $ options ['body ' ];
229209 unset($ options ['body ' ]);
230210 }
231- $ response = $ this ->client ->request ('post ' , $ uri , array_merge ( $ this ->getRequestOptions (), $ options ));
211+ $ response = $ this ->client ->request ('post ' , $ uri , $ this ->buildRequestOptions ( $ options ));
232212 return new Response ($ response );
233213 }
234214
@@ -262,8 +242,7 @@ public function post(string $uri, array $options = []): IResponse {
262242 * @throws \Exception If the request could not get completed
263243 */
264244 public function put (string $ uri , array $ options = []): IResponse {
265- $ this ->setDefaultOptions ();
266- $ response = $ this ->client ->request ('put ' , $ uri , array_merge ($ this ->getRequestOptions (), $ options ));
245+ $ response = $ this ->client ->request ('put ' , $ uri , $ this ->buildRequestOptions ($ options ));
267246 return new Response ($ response );
268247 }
269248
@@ -297,12 +276,10 @@ public function put(string $uri, array $options = []): IResponse {
297276 * @throws \Exception If the request could not get completed
298277 */
299278 public function delete (string $ uri , array $ options = []): IResponse {
300- $ this ->setDefaultOptions ();
301- $ response = $ this ->client ->request ('delete ' , $ uri , array_merge ($ this ->getRequestOptions (), $ options ));
279+ $ response = $ this ->client ->request ('delete ' , $ uri , $ this ->buildRequestOptions ($ options ));
302280 return new Response ($ response );
303281 }
304282
305-
306283 /**
307284 * Sends a options request
308285 *
@@ -333,8 +310,7 @@ public function delete(string $uri, array $options = []): IResponse {
333310 * @throws \Exception If the request could not get completed
334311 */
335312 public function options (string $ uri , array $ options = []): IResponse {
336- $ this ->setDefaultOptions ();
337- $ response = $ this ->client ->request ('options ' , $ uri , array_merge ($ this ->getRequestOptions (), $ options ));
313+ $ response = $ this ->client ->request ('options ' , $ uri , $ this ->buildRequestOptions ($ options ));
338314 return new Response ($ response );
339315 }
340316}
0 commit comments