X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FParseUrl.php;h=184a02ae7502db77cd3a787f78ffca9b898b1a21;hb=3bca4fe2a64671d09e08346456cdfa6c12f996e9;hp=abd9154da1cb83ba4adcbf3bd50ec58f00d25d6a;hpb=aa65e3032d4eb394cd21dd4b983deb42f17843ca;p=friendica.git diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index abd9154da1..184a02ae75 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -1,6 +1,6 @@ head($url, [HttpClientOptions::ACCEPT_CONTENT => $accept]); + if (!empty($timeout)) { + $options = [HttpClientOptions::TIMEOUT => $timeout]; + } else { + $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]); + try { + $curlResult = DI::httpClient()->head($url, array_merge([HttpClientOptions::ACCEPT_CONTENT => $accept], $options)); + } catch (\Exception $e) { + DI::logger()->debug('Got exception', ['url' => $url, 'message' => $e->getMessage()]); + return []; + } + + // 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()) { @@ -82,7 +96,7 @@ class ParseUrl } /** - * Search for chached embeddable data of an url otherwise fetch it + * Search for cached embeddable data of an url otherwise fetch it * * @param string $url The url of the page which should be scraped * @param bool $do_oembed The false option is used by the function fetch_oembed() @@ -101,7 +115,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 [ @@ -180,7 +194,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 [ @@ -207,7 +221,7 @@ class ParseUrl ]; if ($count > 10) { - Logger::notice('Endless loop detected', ['url' => $url]); + Logger::warning('Endless loop detected', ['url' => $url]); return $siteinfo; } @@ -224,7 +238,7 @@ class ParseUrl } $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::CONTENT_LENGTH => 1000000]); - if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { + if (!$curlResult->isSuccess() || empty($curlResult->getBodyString())) { Logger::info('Empty body or error when fetching', ['url' => $url, 'success' => $curlResult->isSuccess(), 'code' => $curlResult->getReturnCode()]); return $siteinfo; } @@ -238,7 +252,7 @@ class ParseUrl } } - $body = $curlResult->getBody(); + $body = $curlResult->getBodyString(); if ($do_oembed) { $oembed_data = OEmbed::fetchURL($url, false, false); @@ -276,25 +290,13 @@ class ParseUrl } $charset = ''; - // Look for a charset, first in headers - // 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)), ';,')); - } - - // Then in body that gets precedence - // Expected forms: - // - - // - - // - - // - - // We escape