]> git.mxchange.org Git - friendica.git/commitdiff
Catch all errors thrown by "fetchRaw"
authorMichael <heluecht@pirati.ca>
Sun, 26 Feb 2023 14:08:33 +0000 (14:08 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 26 Feb 2023 14:08:33 +0000 (14:08 +0000)
src/Contact/Avatar.php
src/Core/Storage/Type/ExternalResource.php
src/Model/APContact.php
src/Model/Contact.php
src/Model/Post/Link.php
src/Module/Proxy.php
src/Protocol/ActivityPub/Processor.php
src/Util/HTTPSignature.php

index e039a52799fc524e98f605cbb00ad51e198307a1..4843e5d37c835ed0e105c797e6d2876262e06342 100644 (file)
@@ -73,7 +73,12 @@ class Avatar
                        return $fields;
                }
 
-               $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
+               try {
+                       $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
+               } catch (\Throwable $th) {
+                       Logger::notice('Avatar is invalid', ['avatar' => $avatar, 'error' => $th]);
+                       return $fields;
+               }
 
                $img_str = $fetchResult->getBody();
                if (empty($img_str)) {
index 055db0deaa9f4e02edb2dff81a3cf0d6f3064f09..54d47e5bf447b351b36a45db24d13e351d9d60f0 100644 (file)
@@ -57,6 +57,7 @@ class ExternalResource implements ICanReadFromStorage
                try {
                        $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
                } catch (Exception $exception) {
+                       Logger::notice('URL is invalid', ['url' => $data->url, 'error' => $exception]);
                        throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception);
                }
                if (!empty($fetchResult) && $fetchResult->isSuccess()) {
index 215d7e317dcf4a78b973942c87e1b7acc4e07607..a429a7a5d4a1303d90eac739c7b9bc9a6f7e02a2 100644 (file)
@@ -189,17 +189,22 @@ class APContact
                if (empty($data)) {
                        $local_owner = [];
 
-                       $curlResult = HTTPSignature::fetchRaw($url);
-                       $failed = empty($curlResult) || empty($curlResult->getBody()) ||
-                               (!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410));
-
-                       if (!$failed) {
-                               $data = json_decode($curlResult->getBody(), true);
-                               $failed = empty($data) || !is_array($data);
-                       }
+                       try {
+                               $curlResult = HTTPSignature::fetchRaw($url);
+                               $failed = empty($curlResult) || empty($curlResult->getBody()) ||
+                                       (!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410));
+       
+                               if (!$failed) {
+                                       $data = json_decode($curlResult->getBody(), true);
+                                       $failed = empty($data) || !is_array($data);
+                               }
 
-                       if (!$failed && ($curlResult->getReturnCode() == 410)) {
-                               $data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone'];
+                               if (!$failed && ($curlResult->getReturnCode() == 410)) {
+                                       $data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone'];
+                               }
+                       } catch (\Throwable $th) {
+                               Logger::notice('Error fetching url', ['url' => $url, 'error' => $th]);
+                               $failed = true;
                        }
 
                        if ($failed) {
index 571253f42588bde9c58fe97ad1e8d29c21d53277..64509b5d227b72e0c598eeeb4e1ab392420cd09d 100644 (file)
@@ -2216,16 +2216,21 @@ class Contact
                        if (($contact['avatar'] != $avatar) || empty($contact['blurhash'])) {
                                $update_fields = ['avatar' => $avatar];
                                if (!Network::isLocalLink($avatar)) {
-                                       $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
-
-                                       $img_str = $fetchResult->getBody();
-                                       if (!empty($img_str)) {
-                                               $image = new Image($img_str, Images::getMimeTypeByData($img_str));
-                                               if ($image->isValid()) {
-                                                       $update_fields['blurhash'] = $image->getBlurHash();
-                                               } else {
-                                                       return;
+                                       try {
+                                               $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
+
+                                               $img_str = $fetchResult->getBody();
+                                               if (!empty($img_str)) {
+                                                       $image = new Image($img_str, Images::getMimeTypeByData($img_str));
+                                                       if ($image->isValid()) {
+                                                               $update_fields['blurhash'] = $image->getBlurHash();
+                                                       } else {
+                                                               return;
+                                                       }
                                                }
+                                       } catch (\Throwable $th) {
+                                               Logger::notice('Error fetching avatar', ['avatar' => $avatar, 'error' => $th]);
+                                               return;
                                        }
                                } elseif (!empty($contact['blurhash'])) {
                                        $update_fields['blurhash'] = null;
index 343ad815c39783192ec7a03b448aaca8102f710c..4ac8173799390c28f4e05ee2cbf6ca833e4d764d 100644 (file)
@@ -125,8 +125,13 @@ class Link
        {
                $timeout = DI::config()->get('system', 'xrd_timeout');
 
-               $curlResult = HTTPSignature::fetchRaw($url, 0, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
-               if (empty($curlResult) || !$curlResult->isSuccess()) {
+               try {
+                       $curlResult = HTTPSignature::fetchRaw($url, 0, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
+                       if (empty($curlResult) || !$curlResult->isSuccess()) {
+                               return [];
+                       }
+               } catch (\Throwable $th) {
+                       Logger::notice('Error fetching url', ['url' => $url, 'error' => $th]);
                        return [];
                }
                $fields = ['mimetype' => $curlResult->getHeader('Content-Type')[0]];
index ab129a82c9243934d1a9b10222ab524d8bdc6a0f..3f1e4b2b80ed5caa70968028261e5a3d5bdf3881 100644 (file)
@@ -83,13 +83,18 @@ class Proxy extends BaseModule
                $request['url'] = str_replace(' ', '+', $request['url']);
 
                // Fetch the content with the local user
-               $fetchResult = HTTPSignature::fetchRaw($request['url'], DI::userSession()->getLocalUserId(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]);
-               $img_str = $fetchResult->getBody();
-
-               if (!$fetchResult->isSuccess() || empty($img_str)) {
-                       Logger::notice('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]);
+               try {
+                       $fetchResult = HTTPSignature::fetchRaw($request['url'], DI::userSession()->getLocalUserId(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]);
+                       $img_str = $fetchResult->getBody();
+
+                       if (!$fetchResult->isSuccess() || empty($img_str)) {
+                               Logger::notice('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]);
+                               self::responseError();
+                               // stop.
+                       }
+               } catch (\Throwable $th) {
+                       Logger::notice('Error fetching image', ['image' => $request['url'], 'error' => $th]);
                        self::responseError();
-                       // stop.
                }
 
                Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => DI::userSession()->getLocalUserId(), 'image' => $request['url']]);
index 11267cfaf5435e026e3cb61458d27f0039351807..20736504efeeaf8cebab5b9846c8fdc5e6036374 100644 (file)
@@ -570,7 +570,12 @@ class Processor
         */
        public static function isActivityGone(string $url): bool
        {
-               $curlResult = HTTPSignature::fetchRaw($url, 0);
+               try {
+                       $curlResult = HTTPSignature::fetchRaw($url, 0);
+               } catch (\Throwable $th) {
+                       Logger::notice('Error fetching url', ['url' => $url, 'error' => $th]);
+                       return true;
+               }
 
                if (Network::isUrlBlocked($url)) {
                        return true;
index f082fe32f79f7eb3f9f905a2f6bca2d22050b28e..fe623f2968a0756b17e8b7d635bc94c400c96d04 100644 (file)
@@ -422,7 +422,12 @@ class HTTPSignature
         */
        public static function fetch(string $request, int $uid): array
        {
-               $curlResult = self::fetchRaw($request, $uid);
+               try {
+                       $curlResult = self::fetchRaw($request, $uid);
+               } catch (\Throwable $th) {
+                       Logger::notice('Error fetching url', ['url' => $request, 'error' => $th]);
+                       return [];
+               }
 
                if (empty($curlResult)) {
                        return [];