Client.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Http\Client;
  25. use GuzzleHttp\Client as GuzzleClient;
  26. use GuzzleHttp\HandlerStack;
  27. use GuzzleHttp\Middleware;
  28. use OCP\Http\Client\IClient;
  29. use OCP\Http\Client\IResponse;
  30. use OCP\ICertificateManager;
  31. use OCP\IConfig;
  32. use Psr\Http\Message\RequestInterface;
  33. /**
  34. * Class Client
  35. *
  36. * @package OC\Http
  37. */
  38. class Client implements IClient {
  39. /** @var GuzzleClient */
  40. private $client;
  41. /** @var IConfig */
  42. private $config;
  43. /** @var ICertificateManager */
  44. private $certificateManager;
  45. private $configured = false;
  46. /** @var HandlerStack */
  47. private $stack;
  48. /**
  49. * @param IConfig $config
  50. * @param ICertificateManager $certificateManager
  51. * @param GuzzleClient $client
  52. */
  53. public function __construct(
  54. IConfig $config,
  55. ICertificateManager $certificateManager,
  56. GuzzleClient $client,
  57. HandlerStack $stack
  58. ) {
  59. $this->config = $config;
  60. $this->client = $client;
  61. $this->stack = $stack;
  62. $this->certificateManager = $certificateManager;
  63. }
  64. /**
  65. * Sets the default options to the client
  66. */
  67. private function setDefaultOptions() {
  68. if ($this->configured) {
  69. return;
  70. }
  71. $this->configured = true;
  72. $this->stack->push(Middleware::mapRequest(function (RequestInterface $request) {
  73. return $request
  74. ->withHeader('User-Agent', 'Nextcloud Server Crawler');
  75. }));
  76. }
  77. private function getRequestOptions() {
  78. $options = [
  79. 'verify' => $this->getCertBundle(),
  80. ];
  81. $proxyUri = $this->getProxyUri();
  82. if ($proxyUri !== '') {
  83. $options['proxy'] = $proxyUri;
  84. }
  85. return $options;
  86. }
  87. private function getCertBundle() {
  88. if ($this->certificateManager->listCertificates() !== []) {
  89. return $this->certificateManager->getAbsoluteBundlePath();
  90. } else {
  91. // If the instance is not yet setup we need to use the static path as
  92. // $this->certificateManager->getAbsoluteBundlePath() tries to instantiiate
  93. // a view
  94. if ($this->config->getSystemValue('installed', false)) {
  95. return $this->certificateManager->getAbsoluteBundlePath(null);
  96. } else {
  97. return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
  98. }
  99. }
  100. }
  101. /**
  102. * Get the proxy URI
  103. *
  104. * @return string
  105. */
  106. private function getProxyUri(): string {
  107. $proxyHost = $this->config->getSystemValue('proxy', null);
  108. $proxyUserPwd = $this->config->getSystemValue('proxyuserpwd', null);
  109. $proxyUri = '';
  110. if ($proxyUserPwd !== null) {
  111. $proxyUri .= $proxyUserPwd . '@';
  112. }
  113. if ($proxyHost !== null) {
  114. $proxyUri .= $proxyHost;
  115. }
  116. return $proxyUri;
  117. }
  118. /**
  119. * Sends a GET request
  120. *
  121. * @param string $uri
  122. * @param array $options Array such as
  123. * 'query' => [
  124. * 'field' => 'abc',
  125. * 'other_field' => '123',
  126. * 'file_name' => fopen('/path/to/file', 'r'),
  127. * ],
  128. * 'headers' => [
  129. * 'foo' => 'bar',
  130. * ],
  131. * 'cookies' => ['
  132. * 'foo' => 'bar',
  133. * ],
  134. * 'allow_redirects' => [
  135. * 'max' => 10, // allow at most 10 redirects.
  136. * 'strict' => true, // use "strict" RFC compliant redirects.
  137. * 'referer' => true, // add a Referer header
  138. * 'protocols' => ['https'] // only allow https URLs
  139. * ],
  140. * 'save_to' => '/path/to/file', // save to a file or a stream
  141. * 'verify' => true, // bool or string to CA file
  142. * 'debug' => true,
  143. * 'timeout' => 5,
  144. * @return IResponse
  145. * @throws \Exception If the request could not get completed
  146. */
  147. public function get(string $uri, array $options = []): IResponse {
  148. $this->setDefaultOptions();
  149. $response = $this->client->request('get', $uri, array_merge($this->getRequestOptions(), $options));
  150. $isStream = isset($options['stream']) && $options['stream'];
  151. return new Response($response, $isStream);
  152. }
  153. /**
  154. * Sends a HEAD request
  155. *
  156. * @param string $uri
  157. * @param array $options Array such as
  158. * 'headers' => [
  159. * 'foo' => 'bar',
  160. * ],
  161. * 'cookies' => ['
  162. * 'foo' => 'bar',
  163. * ],
  164. * 'allow_redirects' => [
  165. * 'max' => 10, // allow at most 10 redirects.
  166. * 'strict' => true, // use "strict" RFC compliant redirects.
  167. * 'referer' => true, // add a Referer header
  168. * 'protocols' => ['https'] // only allow https URLs
  169. * ],
  170. * 'save_to' => '/path/to/file', // save to a file or a stream
  171. * 'verify' => true, // bool or string to CA file
  172. * 'debug' => true,
  173. * 'timeout' => 5,
  174. * @return IResponse
  175. * @throws \Exception If the request could not get completed
  176. */
  177. public function head(string $uri, array $options = []): IResponse {
  178. $this->setDefaultOptions();
  179. $response = $this->client->request('head', $uri, array_merge($this->getRequestOptions(), $options));
  180. return new Response($response);
  181. }
  182. /**
  183. * Sends a POST request
  184. *
  185. * @param string $uri
  186. * @param array $options Array such as
  187. * 'body' => [
  188. * 'field' => 'abc',
  189. * 'other_field' => '123',
  190. * 'file_name' => fopen('/path/to/file', 'r'),
  191. * ],
  192. * 'headers' => [
  193. * 'foo' => 'bar',
  194. * ],
  195. * 'cookies' => ['
  196. * 'foo' => 'bar',
  197. * ],
  198. * 'allow_redirects' => [
  199. * 'max' => 10, // allow at most 10 redirects.
  200. * 'strict' => true, // use "strict" RFC compliant redirects.
  201. * 'referer' => true, // add a Referer header
  202. * 'protocols' => ['https'] // only allow https URLs
  203. * ],
  204. * 'save_to' => '/path/to/file', // save to a file or a stream
  205. * 'verify' => true, // bool or string to CA file
  206. * 'debug' => true,
  207. * 'timeout' => 5,
  208. * @return IResponse
  209. * @throws \Exception If the request could not get completed
  210. */
  211. public function post(string $uri, array $options = []): IResponse {
  212. $this->setDefaultOptions();
  213. if (isset($options['body']) && is_array($options['body'])) {
  214. $options['form_params'] = $options['body'];
  215. unset($options['body']);
  216. }
  217. $response = $this->client->request('post', $uri, array_merge($this->getRequestOptions(), $options));
  218. return new Response($response);
  219. }
  220. /**
  221. * Sends a PUT request
  222. *
  223. * @param string $uri
  224. * @param array $options Array such as
  225. * 'body' => [
  226. * 'field' => 'abc',
  227. * 'other_field' => '123',
  228. * 'file_name' => fopen('/path/to/file', 'r'),
  229. * ],
  230. * 'headers' => [
  231. * 'foo' => 'bar',
  232. * ],
  233. * 'cookies' => ['
  234. * 'foo' => 'bar',
  235. * ],
  236. * 'allow_redirects' => [
  237. * 'max' => 10, // allow at most 10 redirects.
  238. * 'strict' => true, // use "strict" RFC compliant redirects.
  239. * 'referer' => true, // add a Referer header
  240. * 'protocols' => ['https'] // only allow https URLs
  241. * ],
  242. * 'save_to' => '/path/to/file', // save to a file or a stream
  243. * 'verify' => true, // bool or string to CA file
  244. * 'debug' => true,
  245. * 'timeout' => 5,
  246. * @return IResponse
  247. * @throws \Exception If the request could not get completed
  248. */
  249. public function put(string $uri, array $options = []): IResponse {
  250. $this->setDefaultOptions();
  251. $response = $this->client->request('put', $uri, array_merge($this->getRequestOptions(), $options));
  252. return new Response($response);
  253. }
  254. /**
  255. * Sends a DELETE request
  256. *
  257. * @param string $uri
  258. * @param array $options Array such as
  259. * 'body' => [
  260. * 'field' => 'abc',
  261. * 'other_field' => '123',
  262. * 'file_name' => fopen('/path/to/file', 'r'),
  263. * ],
  264. * 'headers' => [
  265. * 'foo' => 'bar',
  266. * ],
  267. * 'cookies' => ['
  268. * 'foo' => 'bar',
  269. * ],
  270. * 'allow_redirects' => [
  271. * 'max' => 10, // allow at most 10 redirects.
  272. * 'strict' => true, // use "strict" RFC compliant redirects.
  273. * 'referer' => true, // add a Referer header
  274. * 'protocols' => ['https'] // only allow https URLs
  275. * ],
  276. * 'save_to' => '/path/to/file', // save to a file or a stream
  277. * 'verify' => true, // bool or string to CA file
  278. * 'debug' => true,
  279. * 'timeout' => 5,
  280. * @return IResponse
  281. * @throws \Exception If the request could not get completed
  282. */
  283. public function delete(string $uri, array $options = []): IResponse {
  284. $this->setDefaultOptions();
  285. $response = $this->client->request('delete', $uri, array_merge($this->getRequestOptions(), $options));
  286. return new Response($response);
  287. }
  288. /**
  289. * Sends a options request
  290. *
  291. * @param string $uri
  292. * @param array $options Array such as
  293. * 'body' => [
  294. * 'field' => 'abc',
  295. * 'other_field' => '123',
  296. * 'file_name' => fopen('/path/to/file', 'r'),
  297. * ],
  298. * 'headers' => [
  299. * 'foo' => 'bar',
  300. * ],
  301. * 'cookies' => ['
  302. * 'foo' => 'bar',
  303. * ],
  304. * 'allow_redirects' => [
  305. * 'max' => 10, // allow at most 10 redirects.
  306. * 'strict' => true, // use "strict" RFC compliant redirects.
  307. * 'referer' => true, // add a Referer header
  308. * 'protocols' => ['https'] // only allow https URLs
  309. * ],
  310. * 'save_to' => '/path/to/file', // save to a file or a stream
  311. * 'verify' => true, // bool or string to CA file
  312. * 'debug' => true,
  313. * 'timeout' => 5,
  314. * @return IResponse
  315. * @throws \Exception If the request could not get completed
  316. */
  317. public function options(string $uri, array $options = []): IResponse {
  318. $this->setDefaultOptions();
  319. $response = $this->client->request('options', $uri, array_merge($this->getRequestOptions(), $options));
  320. return new Response($response);
  321. }
  322. }