1
0

ClientService.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Http\Client;
  9. use GuzzleHttp\Client as GuzzleClient;
  10. use GuzzleHttp\Handler\CurlHandler;
  11. use GuzzleHttp\HandlerStack;
  12. use GuzzleHttp\Middleware;
  13. use OCP\Diagnostics\IEventLogger;
  14. use OCP\Http\Client\IClient;
  15. use OCP\Http\Client\IClientService;
  16. use OCP\ICertificateManager;
  17. use OCP\IConfig;
  18. use OCP\Security\IRemoteHostValidator;
  19. use Psr\Http\Message\RequestInterface;
  20. use Psr\Log\LoggerInterface;
  21. /**
  22. * Class ClientService
  23. *
  24. * @package OC\Http
  25. */
  26. class ClientService implements IClientService {
  27. /** @var IConfig */
  28. private $config;
  29. /** @var ICertificateManager */
  30. private $certificateManager;
  31. /** @var DnsPinMiddleware */
  32. private $dnsPinMiddleware;
  33. private IRemoteHostValidator $remoteHostValidator;
  34. private IEventLogger $eventLogger;
  35. public function __construct(
  36. IConfig $config,
  37. ICertificateManager $certificateManager,
  38. DnsPinMiddleware $dnsPinMiddleware,
  39. IRemoteHostValidator $remoteHostValidator,
  40. IEventLogger $eventLogger,
  41. protected LoggerInterface $logger,
  42. ) {
  43. $this->config = $config;
  44. $this->certificateManager = $certificateManager;
  45. $this->dnsPinMiddleware = $dnsPinMiddleware;
  46. $this->remoteHostValidator = $remoteHostValidator;
  47. $this->eventLogger = $eventLogger;
  48. }
  49. /**
  50. * @return Client
  51. */
  52. public function newClient(): IClient {
  53. $handler = new CurlHandler();
  54. $stack = HandlerStack::create($handler);
  55. if ($this->config->getSystemValueBool('dns_pinning', true)) {
  56. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  57. }
  58. $stack->push(Middleware::tap(function (RequestInterface $request) {
  59. $this->eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
  60. }, function () {
  61. $this->eventLogger->end('http:request');
  62. }), 'event logger');
  63. $client = new GuzzleClient(['handler' => $stack]);
  64. return new Client(
  65. $this->config,
  66. $this->certificateManager,
  67. $client,
  68. $this->remoteHostValidator,
  69. $this->logger,
  70. );
  71. }
  72. }