1
0

ClientService.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 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. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  73. $stack->push(Middleware::tap(function (RequestInterface $request) {
  74. $this->eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
  75. }, function () {
  76. $this->eventLogger->end('http:request');
  77. }), 'event logger');
  78. $client = new GuzzleClient(['handler' => $stack]);
  79. return new Client(
  80. $this->config,
  81. $this->certificateManager,
  82. $client,
  83. $this->remoteHostValidator,
  84. $this->logger,
  85. );
  86. }
  87. }