ClientService.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Http\Client;
  26. use GuzzleHttp\Client as GuzzleClient;
  27. use GuzzleHttp\HandlerStack;
  28. use GuzzleHttp\Handler\CurlHandler;
  29. use OCP\Http\Client\IClient;
  30. use OCP\Http\Client\IClientService;
  31. use OCP\ICertificateManager;
  32. use OCP\IConfig;
  33. use OCP\Security\IRemoteHostValidator;
  34. /**
  35. * Class ClientService
  36. *
  37. * @package OC\Http
  38. */
  39. class ClientService implements IClientService {
  40. /** @var IConfig */
  41. private $config;
  42. /** @var ICertificateManager */
  43. private $certificateManager;
  44. /** @var DnsPinMiddleware */
  45. private $dnsPinMiddleware;
  46. private IRemoteHostValidator $remoteHostValidator;
  47. public function __construct(IConfig $config,
  48. ICertificateManager $certificateManager,
  49. DnsPinMiddleware $dnsPinMiddleware,
  50. IRemoteHostValidator $remoteHostValidator) {
  51. $this->config = $config;
  52. $this->certificateManager = $certificateManager;
  53. $this->dnsPinMiddleware = $dnsPinMiddleware;
  54. $this->remoteHostValidator = $remoteHostValidator;
  55. }
  56. /**
  57. * @return Client
  58. */
  59. public function newClient(): IClient {
  60. $handler = new CurlHandler();
  61. $stack = HandlerStack::create($handler);
  62. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  63. $client = new GuzzleClient(['handler' => $stack]);
  64. return new Client(
  65. $this->config,
  66. $this->certificateManager,
  67. $client,
  68. $this->remoteHostValidator,
  69. );
  70. }
  71. }