1
0

CheckServerResponseTrait.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
  5. *
  6. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\SetupChecks;
  25. use Generator;
  26. use OCP\Http\Client\IClientService;
  27. use OCP\Http\Client\IResponse;
  28. use OCP\IConfig;
  29. use OCP\IL10N;
  30. use OCP\IURLGenerator;
  31. /**
  32. * Common trait for setup checks that need to use requests to the same server and check the response
  33. */
  34. trait CheckServerResponseTrait {
  35. protected IConfig $config;
  36. protected IURLGenerator $urlGenerator;
  37. protected IClientService $clientService;
  38. protected IL10N $l10n;
  39. /**
  40. * Common helper string in case a check could not fetch any results
  41. */
  42. protected function serverConfigHelp(): string {
  43. return $this->l10n->t('To allow this check to run you have to make sure that your webserver can connect to itself. Therefor it must be able to resolve and connect to at least one its `trusted_domains` or the `overwrite.cli.url`.');
  44. }
  45. /**
  46. * Get all possible URLs that need to be checked for a local request test.
  47. * This takes all `trusted_domains` and the CLI overwrite URL into account.
  48. *
  49. * @param string $url The relative URL to test
  50. * @return string[] List of possible absolute URLs
  51. */
  52. protected function getTestUrls(string $url): array {
  53. $hosts = $this->config->getSystemValue('trusted_domains', []);
  54. $cliUrl = $this->config->getSystemValue('overwrite.cli.url', '');
  55. if ($cliUrl !== '') {
  56. $hosts[] = $cliUrl;
  57. }
  58. $testUrls = array_merge(
  59. [$this->urlGenerator->getAbsoluteURL($url)],
  60. array_map(fn (string $host): string => $host . $url, $hosts),
  61. );
  62. return $testUrls;
  63. }
  64. /**
  65. * Run a HEAD request to check header
  66. * @param string $url The relative URL to check
  67. * @param bool $ignoreSSL Ignore SSL certificates
  68. * @return Generator<int, IResponse>
  69. */
  70. protected function runHEAD(string $url, bool $ignoreSSL = true): Generator {
  71. $client = $this->clientService->newClient();
  72. $requestOptions = $this->getRequestOptions($ignoreSSL);
  73. foreach ($this->getTestUrls($url) as $testURL) {
  74. try {
  75. yield $client->head($testURL, $requestOptions);
  76. } catch (\Throwable $e) {
  77. $this->logger->debug('Can not connect to local server for running setup checks', ['exception' => $e, 'url' => $testURL]);
  78. }
  79. }
  80. }
  81. protected function getRequestOptions(bool $ignoreSSL): array {
  82. $requestOptions = [
  83. 'connect_timeout' => 10,
  84. 'nextcloud' => [
  85. 'allow_local_address' => true,
  86. ],
  87. ];
  88. if ($ignoreSSL) {
  89. $requestOptions['verify'] = false;
  90. }
  91. return $requestOptions;
  92. }
  93. }