1
0

ClientService.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Handler\CurlHandler;
  28. use GuzzleHttp\HandlerStack;
  29. use GuzzleHttp\Middleware;
  30. use OCP\Diagnostics\IEventLogger;
  31. use OCP\Http\Client\IClient;
  32. use OCP\Http\Client\IClientService;
  33. use OCP\ICertificateManager;
  34. use OCP\IConfig;
  35. use OCP\Security\IRemoteHostValidator;
  36. use Psr\Http\Message\RequestInterface;
  37. use Psr\Log\LoggerInterface;
  38. /**
  39. * Class ClientService
  40. *
  41. * @package OC\Http
  42. */
  43. class ClientService implements IClientService {
  44. /** @var IConfig */
  45. private $config;
  46. /** @var ICertificateManager */
  47. private $certificateManager;
  48. /** @var DnsPinMiddleware */
  49. private $dnsPinMiddleware;
  50. private IRemoteHostValidator $remoteHostValidator;
  51. private IEventLogger $eventLogger;
  52. public function __construct(
  53. IConfig $config,
  54. ICertificateManager $certificateManager,
  55. DnsPinMiddleware $dnsPinMiddleware,
  56. IRemoteHostValidator $remoteHostValidator,
  57. IEventLogger $eventLogger,
  58. protected LoggerInterface $logger,
  59. ) {
  60. $this->config = $config;
  61. $this->certificateManager = $certificateManager;
  62. $this->dnsPinMiddleware = $dnsPinMiddleware;
  63. $this->remoteHostValidator = $remoteHostValidator;
  64. $this->eventLogger = $eventLogger;
  65. }
  66. /**
  67. * @return Client
  68. */
  69. public function newClient(): IClient {
  70. $handler = new CurlHandler();
  71. $stack = HandlerStack::create($handler);
  72. if ($this->config->getSystemValueBool('dns_pinning', true)) {
  73. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  74. }
  75. $stack->push(Middleware::tap(function (RequestInterface $request) {
  76. $this->eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
  77. }, function () {
  78. $this->eventLogger->end('http:request');
  79. }), 'event logger');
  80. $client = new GuzzleClient(['handler' => $stack]);
  81. return new Client(
  82. $this->config,
  83. $this->certificateManager,
  84. $client,
  85. $this->remoteHostValidator,
  86. $this->logger,
  87. );
  88. }
  89. }