]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/ParseUrl.php
Merge pull request #11786 from Quix0r/revert-11783-logging-unsupported
[friendica.git] / src / Util / ParseUrl.php
index f86a6480b7b9e56e6c2eddf71ac10ae0eb9a4c55..0183d6b14ae5be6785f90eaf5c16af01e6974de9 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()) {
@@ -551,7 +558,7 @@ class ParseUrl
                                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];