From: Michael Date: Sat, 16 Jul 2022 12:44:21 +0000 (+0000) Subject: Merge remote-tracking branch 'upstream/develop' into server-detection X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8b7cb5d9efeab580c2592e0fbe301f7142b73a3d;p=friendica.git Merge remote-tracking branch 'upstream/develop' into server-detection --- 8b7cb5d9efeab580c2592e0fbe301f7142b73a3d diff --cc src/Model/GServer.php index 3ea07dbc6f,a48f6e4ca6..1986e47831 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@@ -30,7 -30,7 +30,6 @@@ use Friendica\Core\System use Friendica\Core\Worker; use Friendica\Database\Database; use Friendica\Database\DBA; --use Friendica\Database\DBStructure; use Friendica\DI; use Friendica\Module\Register; use Friendica\Network\HTTPClient\Client\HttpClientAccept; @@@ -283,8 -269,8 +282,8 @@@ class GServe $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]); if (DBA::isResult($gserver)) { $next_update = self::getNextUpdateDate(false, $gserver['created'], $gserver['last_contact']); - DBA::update('gserver', ['url' => $url, 'failed' => true, 'last_failure' => DateTimeFormat::utcNow(), - self::update(['failed' => true, 'last_failure' => DateTimeFormat::utcNow(), - 'next_contact' => $next_update, 'detection-method' => null], ++ self::update(['url' => $url, 'failed' => true, 'last_failure' => DateTimeFormat::utcNow(), + 'next_contact' => $next_update, 'network' => Protocol::PHANTOM, 'detection-method' => null], ['nurl' => Strings::normaliseLink($url)]); Logger::info('Set failed status for existing server', ['url' => $url]); return; @@@ -324,9 -323,10 +323,9 @@@ * * @return boolean 'true' if server could be detected */ - public static function detect(string $url, string $network = '', bool $only_nodeinfo = false) + public static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool { Logger::info('Detect server type', ['server' => $url]); - $serverdata = ['detection-method' => self::DETECT_MANUAL]; $original_url = $url; @@@ -559,10 -536,7 +558,7 @@@ $serverdata['last_contact'] = DateTimeFormat::utcNow(); $serverdata['failed'] = false; - // Limit the length on incoming fields - $serverdata = DBStructure::getFieldsForTable('gserver', $serverdata); - - $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => $serverdata['nurl']]); + $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]); if (!DBA::isResult($gserver)) { $serverdata['created'] = DateTimeFormat::utcNow(); $ret = DBA::insert('gserver', $serverdata); @@@ -589,10 -563,10 +585,10 @@@ $contacts = DBA::count('contact', ["`uid` = ? AND `gsid` = ? AND NOT `failed` AND `last-item` > ?", 0, $id, DateTimeFormat::utc('now - 30 days')]); if ($contacts > 0) { Logger::info('Update monthly users', ['id' => $id, 'url' => $serverdata['nurl'], 'monthly-users' => $contacts]); - DBA::update('gserver', ['active-month-users' => $contacts], ['id' => $id]); + self::update(['active-month-users' => $contacts], ['id' => $id]); } } - + if (empty($serverdata['active-halfyear-users'])) { $contacts = DBA::count('contact', ["`uid` = ? AND `gsid` = ? AND NOT `failed` AND `last-item` > ?", 0, $id, DateTimeFormat::utc('now - 180 days')]); if ($contacts > 0) { @@@ -714,10 -684,9 +711,9 @@@ * Fetch server data from '/statistics.json' on the given server * * @param string $url URL of the given server - * * @return array server data */ - private static function fetchStatistics(string $url): array + private static function fetchStatistics(string $url, array $serverdata) { $curlResult = DI::httpClient()->get($url . '/statistics.json', HttpClientAccept::JSON); if (!$curlResult->isSuccess()) { @@@ -1846,10 -1714,10 +1843,10 @@@ * * @param object $curlResult result of curl execution * @param array $serverdata array with server data - * @param string $url Server URL + * * @return array server data */ - private static function analyseRootBody($curlResult, array $serverdata) - private static function analyseRootBody($curlResult, array $serverdata, string $url): array ++ private static function analyseRootBody($curlResult, array $serverdata): array { if (empty($curlResult->getBody())) { return $serverdata; diff --cc src/Util/ParseUrl.php index 8d83296758,ee171ea047..0183d6b14a --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@@ -57,22 -57,15 +57,22 @@@ class ParseUr * Fetch the content type of the given url * @param string $url URL of the page * @param string $accept content-type to accept + * @param int $timeout * @return array content type */ - public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT, int $timeout = 0) - public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT): array ++ public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT, int $timeout = 0): array { - $curlResult = DI::httpClient()->head($url, [HttpClientOptions::ACCEPT_CONTENT => $accept]); + if (!empty($timeout)) { + $options = [HttpClientOptions::TIMEOUT => $timeout]; + } else { + $options = []; + } + + $curlResult = DI::httpClient()->head($url, array_merge([HttpClientOptions::ACCEPT_CONTENT => $accept], $options)); - // Workaround for systems that can't handle a HEAD request - if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) { - $curlResult = DI::httpClient()->get($url, $accept, [HttpClientOptions::CONTENT_LENGTH => 1000000]); + // Workaround for systems that can't handle a HEAD request. Don't retry on timeouts. + if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() >= 400) && !in_array($curlResult->getReturnCode(), [408, 504])) { + $curlResult = DI::httpClient()->get($url, $accept, array_merge([HttpClientOptions::CONTENT_LENGTH => 1000000], $options)); } if (!$curlResult->isSuccess()) {