X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FParseUrl.php;h=4611cf8efe5f25378caa699044543af6269bab4c;hb=fe442683efb8b75394d7df041716c47c81b05fbc;hp=79c3928d7de5703e749f7691912b2f8b1dcda5fa;hpb=803c1d71de9e6d13da9576660c9ab8b8bb2c19f3;p=friendica.git diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index 79c3928d7d..4611cf8efe 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -1,6 +1,6 @@ head($url); + 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. 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()) { + Logger::debug('Got HTTP Error', ['http error' => $curlResult->getReturnCode(), 'url' => $url]); return []; } - $contenttype = $curlResult->getHeader('Content-Type'); + $contenttype = $curlResult->getHeader('Content-Type')[0] ?? ''; if (empty($contenttype)) { - return []; + return ['application', 'octet-stream']; } return explode('/', current(explode(';', $contenttype))); @@ -91,7 +108,7 @@ class ParseUrl * @see ParseUrl::getSiteinfo() for more information about scraping * embeddable content */ - public static function getSiteinfoCached($url, $do_oembed = true): array + public static function getSiteinfoCached(string $url, bool $do_oembed = true): array { if (empty($url)) { return [ @@ -170,7 +187,7 @@ class ParseUrl * * @endverbatim */ - public static function getSiteinfo($url, $do_oembed = true, $count = 1) + public static function getSiteinfo(string $url, bool $do_oembed = true, int $count = 1): array { if (empty($url)) { return [ @@ -197,7 +214,7 @@ class ParseUrl ]; if ($count > 10) { - Logger::notice('Endless loop detected', ['url' => $url]); + Logger::warning('Endless loop detected', ['url' => $url]); return $siteinfo; } @@ -213,19 +230,15 @@ class ParseUrl return $siteinfo; } - $curlResult = DI::httpRequest()->get($url, false, ['content_length' => 1000000]); + $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::CONTENT_LENGTH => 1000000]); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { + Logger::info('Empty body or error when fetching', ['url' => $url, 'success' => $curlResult->isSuccess(), 'code' => $curlResult->getReturnCode()]); return $siteinfo; } $siteinfo['expires'] = DateTimeFormat::utc(self::DEFAULT_EXPIRATION_SUCCESS); - // If the file is too large then exit - if (($curlResult->getInfo()['download_content_length'] ?? 0) > 1000000) { - return $siteinfo; - } - - if ($cacheControlHeader = $curlResult->getHeader('Cache-Control')) { + if ($cacheControlHeader = $curlResult->getHeader('Cache-Control')[0] ?? '') { if (preg_match('/max-age=([0-9]+)/i', $cacheControlHeader, $matches)) { $maxAge = max(86400, (int)array_pop($matches)); $siteinfo['expires'] = DateTimeFormat::utc("now + $maxAge seconds"); @@ -272,26 +285,24 @@ class ParseUrl $charset = ''; // Look for a charset, first in headers // Expected form: Content-Type: text/html; charset=ISO-8859-4 - foreach ($curlResult->getContentType() as $type) { - if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $type, $matches)) { - $charset = trim(trim(trim(array_pop($matches)), ';,')); - } + 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