]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/ParseUrl.php
Merge pull request #11783 from Quix0r/logging-unsupported
[friendica.git] / src / Util / ParseUrl.php
index 9f0fd8a9bca82be9681597314bdd52324a341e68..0a12e628298b9398924d56f37347237958a62a08 100644 (file)
@@ -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()) {
@@ -418,6 +425,8 @@ class ParseUrl
                                case 'news_keywords':
                                        $keywords = explode(',', $meta_tag['content']);
                                        break;
+                               default:
+                                       Logger::debug('Unsupported meta-tag found', ['meta-tag' => $meta_tag]);
                        }
                }
 
@@ -543,12 +552,15 @@ class ParseUrl
        {
                if (!empty($siteinfo['images'])) {
                        array_walk($siteinfo['images'], function (&$image) use ($page_url) {
-                               // According to the specifications someone could place a picture url into the content field as well.
-                               // But this doesn't seem to happen in the wild, so we don't cover it here.
+                               /*
+                                * According to the specifications someone could place a picture
+                                * URL into the content field as well. But this doesn't seem to
+                                * happen in the wild, so we don't cover it here.
+                                */
                                if (!empty($image['url'])) {
                                        $image['url'] = self::completeUrl($image['url'], $page_url);
                                        $photodata = Images::getInfoFromURLCached($image['url']);
-                                       if (!empty($photodata) && ($photodata[0] > 50) && ($photodata[1] > 50)) {
+                                       if (($photodata) && ($photodata[0] > 50) && ($photodata[1] > 50)) {
                                                $image['src'] = $image['url'];
                                                $image['width'] = $photodata[0];
                                                $image['height'] = $photodata[1];