ClientService.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**
  38. * Class ClientService
  39. *
  40. * @package OC\Http
  41. */
  42. class ClientService implements IClientService {
  43. /** @var IConfig */
  44. private $config;
  45. /** @var ICertificateManager */
  46. private $certificateManager;
  47. /** @var DnsPinMiddleware */
  48. private $dnsPinMiddleware;
  49. private IRemoteHostValidator $remoteHostValidator;
  50. private IEventLogger $eventLogger;
  51. public function __construct(
  52. IConfig $config,
  53. ICertificateManager $certificateManager,
  54. DnsPinMiddleware $dnsPinMiddleware,
  55. IRemoteHostValidator $remoteHostValidator,
  56. IEventLogger $eventLogger,
  57. ) {
  58. $this->config = $config;
  59. $this->certificateManager = $certificateManager;
  60. $this->dnsPinMiddleware = $dnsPinMiddleware;
  61. $this->remoteHostValidator = $remoteHostValidator;
  62. $this->eventLogger = $eventLogger;
  63. }
  64. /**
  65. * @return Client
  66. */
  67. public function newClient(): IClient {
  68. $handler = new CurlHandler();
  69. $stack = HandlerStack::create($handler);
  70. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  71. $stack->push(Middleware::tap(function (RequestInterface $request) {
  72. $this->eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
  73. }, function () {
  74. $this->eventLogger->end('http:request');
  75. }), 'event logger');
  76. $client = new GuzzleClient(['handler' => $stack]);
  77. return new Client(
  78. $this->config,
  79. $this->certificateManager,
  80. $client,
  81. $this->remoteHostValidator,
  82. );
  83. }
  84. }