X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FParseUrl.php;h=4611cf8efe5f25378caa699044543af6269bab4c;hb=fe442683efb8b75394d7df041716c47c81b05fbc;hp=9f0fd8a9bca82be9681597314bdd52324a341e68;hpb=78bf7f187c9fd0effdcfb6bbc2390b8ff74dcb76;p=friendica.git diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index 9f0fd8a9bc..4611cf8efe 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -57,15 +57,22 @@ class ParseUrl * 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): 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()) { @@ -180,7 +187,7 @@ class ParseUrl * * @endverbatim */ - public static function getSiteinfo(string $url, bool $do_oembed = true, int $count = 1) + public static function getSiteinfo(string $url, bool $do_oembed = true, int $count = 1): array { if (empty($url)) { return [ @@ -207,7 +214,7 @@ class ParseUrl ]; if ($count > 10) { - Logger::notice('Endless loop detected', ['url' => $url]); + Logger::warning('Endless loop detected', ['url' => $url]); return $siteinfo; } @@ -280,22 +287,22 @@ class ParseUrl // Expected form: Content-Type: text/html; charset=ISO-8859-4 if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $curlResult->getContentType(), $matches)) { $charset = trim(trim(trim(array_pop($matches)), ';,')); + } else { + // Then in body that gets precedence + // Expected forms: + // - + // - + // - + // - + // We escape