Merge pull request #47774 from nextcloud/backport/46255/stable29

[stable29] fix: add option to remove the webroot for setup checks and don't chec…
This commit is contained in:
Andy Scherzinger 2024-09-05 20:31:38 +02:00 committed by GitHub
commit 98a0a8cdcb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 12 deletions

View file

@ -54,24 +54,63 @@ trait CheckServerResponseTrait {
* Get all possible URLs that need to be checked for a local request test.
* This takes all `trusted_domains` and the CLI overwrite URL into account.
*
* @param string $url The relative URL to test
* @param string $url The relative URL to test starting with a /
* @return string[] List of possible absolute URLs
*/
protected function getTestUrls(string $url): array {
$hosts = $this->config->getSystemValue('trusted_domains', []);
$cliUrl = $this->config->getSystemValue('overwrite.cli.url', '');
protected function getTestUrls(string $url, bool $removeWebroot): array {
$testUrls = [];
$webroot = rtrim($this->urlGenerator->getWebroot(), '/');
/* Try overwrite.cli.url first, its supposed to be how the server contacts itself */
$cliUrl = $this->config->getSystemValueString('overwrite.cli.url', '');
if ($cliUrl !== '') {
$hosts[] = $cliUrl;
$cliUrl = $this->normalizeUrl(
$cliUrl,
$webroot,
$removeWebroot
);
$testUrls[] = $cliUrl . $url;
}
$testUrls = array_merge(
[$this->urlGenerator->getAbsoluteURL($url)],
array_map(fn (string $host): string => $host . $url, $hosts),
/* Try URL generator second */
$baseUrl = $this->normalizeUrl(
$this->urlGenerator->getBaseUrl(),
$webroot,
$removeWebroot
);
if ($baseUrl !== $cliUrl) {
$testUrls[] = $baseUrl . $url;
}
/* Last resort: trusted domains */
$hosts = $this->config->getSystemValue('trusted_domains', []);
foreach ($hosts as $host) {
if (str_contains($host, '*')) {
/* Ignore domains with a wildcard */
continue;
}
$hosts[] = 'https://' . $host . $url;
$hosts[] = 'http://' . $host . $url;
}
return $testUrls;
}
/**
* Strip a trailing slash and remove the webroot if requested.
*/
protected function normalizeUrl(string $url, string $webroot, bool $removeWebroot): string {
$url = rtrim($url, '/');
if ($removeWebroot && str_ends_with($url, $webroot)) {
$url = substr($url, -strlen($webroot));
}
return rtrim($url, '/');
}
/**
* Run a HTTP request to check header
* @param string $method The HTTP method to use
@ -86,14 +125,14 @@ trait CheckServerResponseTrait {
*
* @return Generator<int, IResponse>
*/
protected function runRequest(string $method, string $url, array $options = []): Generator {
protected function runRequest(string $method, string $url, array $options = [], bool $removeWebroot = false): Generator {
$options = array_merge(['ignoreSSL' => true, 'httpErrors' => true], $options);
$client = $this->clientService->newClient();
$requestOptions = $this->getRequestOptions($options['ignoreSSL'], $options['httpErrors']);
$requestOptions = array_merge($requestOptions, $options['options'] ?? []);
foreach ($this->getTestUrls($url) as $testURL) {
foreach ($this->getTestUrls($url, $removeWebroot) as $testURL) {
try {
yield $client->request($method, $testURL, $requestOptions);
} catch (\Throwable $e) {

View file

@ -68,7 +68,7 @@ class OcxProviders implements ISetupCheck {
];
foreach ($providers as $provider) {
foreach ($this->runRequest('HEAD', $this->urlGenerator->getWebroot() . $provider, ['httpErrors' => false]) as $response) {
foreach ($this->runRequest('HEAD', $provider, ['httpErrors' => false]) as $response) {
$testedProviders[$provider] = true;
if ($response->getStatusCode() === 200) {
$workingProviders[] = $provider;

View file

@ -70,7 +70,7 @@ class WellKnownUrls implements ISetupCheck {
foreach ($urls as [$verb,$url,$validStatuses,$checkCustomHeader]) {
$works = null;
foreach ($this->runRequest($verb, $url, ['httpErrors' => false, 'options' => ['allow_redirects' => ['track_redirects' => true]]]) as $response) {
foreach ($this->runRequest($verb, $url, ['httpErrors' => false, 'options' => ['allow_redirects' => ['track_redirects' => true]]], removeWebroot: true) as $response) {
// Check that the response status matches
$works = in_array($response->getStatusCode(), $validStatuses);
// and (if needed) the custom Nextcloud header is set