]> git.mxchange.org Git - friendica.git/commitdiff
Improved http error handling
authorMichael <heluecht@pirati.ca>
Fri, 29 Oct 2021 23:21:07 +0000 (23:21 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 29 Oct 2021 23:21:07 +0000 (23:21 +0000)
55 files changed:
bin/auth_ejabberd.php
bin/console.php
bin/daemon.php
bin/testargs.php
bin/wait-for-connection
bin/worker.php
include/api.php
src/App/Module.php
src/Core/Cache/TraitMemcacheCommand.php
src/Core/Renderer.php
src/Core/System.php
src/Database/Database.php
src/Factory/Api/Mastodon/Application.php
src/Module/Admin/Summary.php
src/Module/Diaspora/Receive.php
src/Module/Friendica.php
src/Module/Photo.php
src/Module/Proxy.php
src/Module/Special/HTTPException.php
src/Network/CurlResult.php
src/Network/HTTPException.php
src/Network/HTTPException/AcceptedException.php
src/Network/HTTPException/BadGatewayException.php
src/Network/HTTPException/BadRequestException.php
src/Network/HTTPException/ConflictException.php
src/Network/HTTPException/ExpectationFailedException.php
src/Network/HTTPException/ForbiddenException.php
src/Network/HTTPException/FoundException.php [new file with mode: 0644]
src/Network/HTTPException/GatewayTimeoutException.php
src/Network/HTTPException/GoneException.php
src/Network/HTTPException/ImATeapotException.php
src/Network/HTTPException/InternalServerErrorException.php
src/Network/HTTPException/LenghtRequiredException.php
src/Network/HTTPException/MethodNotAllowedException.php
src/Network/HTTPException/MovedPermanently.php [new file with mode: 0644]
src/Network/HTTPException/NoContentException.php
src/Network/HTTPException/NonAcceptableException.php
src/Network/HTTPException/NotFoundException.php
src/Network/HTTPException/NotImplementedException.php
src/Network/HTTPException/NotModifiedException.php [new file with mode: 0644]
src/Network/HTTPException/OKException.php
src/Network/HTTPException/PreconditionFailedException.php
src/Network/HTTPException/ServiceUnavailableException.php
src/Network/HTTPException/TemporaryRedirectException.php [new file with mode: 0644]
src/Network/HTTPException/TooManyRequestsException.php
src/Network/HTTPException/UnauthorizedException.php
src/Network/HTTPException/UnprocessableEntityException.php
src/Network/HTTPException/UnsupportedMediaTypeException.php
src/Render/FriendicaSmartyEngine.php
src/Util/EMailer/MailBuilder.php
src/Util/HTTPSignature.php
src/Util/Network.php
src/Worker/Notifier.php
view/theme/frio/style.php
view/theme/vier/style.php

index 88e5d034cb34d86101bf0ebe3bd1fbe0c011c831..fd1b40cc848e0783a0c402735ec2fc1f3e3859d8 100755 (executable)
  *
  */
 
+use Friendica\Network\HTTPException\ForbiddenException;
+
 if (php_sapi_name() !== 'cli') {
-       header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden');
-       exit();
+       throw new ForbiddenException();
 }
 
 use Dice\Dice;
index 35f0b5feefcf3820bfaca4739cca5eaee7e64e49..edfb8cd285f9c9f1194788c4994c4e5edd3170a8 100755 (executable)
  *
  */
 
+use Friendica\Network\HTTPException\ForbiddenException;
+
 if (php_sapi_name() !== 'cli') {
-       header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden');
-       exit();
+       throw new ForbiddenException();
 }
 
 use Dice\Dice;
index 7d4945fe0378771ce1e04406f5d2f6e78d1e3bfb..965c495ecdbc5af9cf0961cb15319ea93c28fa85 100755 (executable)
  * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
  */
 
+use Friendica\Network\HTTPException\ForbiddenException;
+
 if (php_sapi_name() !== 'cli') {
-       header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden');
-       exit();
+       throw new ForbiddenException();
 }
 
 use Dice\Dice;
index 55197f63a3d2641dea57aa6db314b25270a7cd7c..05826f2e4271211e4923249135f5c4ea947ad2b0 100644 (file)
  *
  */
 
+use Friendica\Network\HTTPException\ForbiddenException;
+
 if (php_sapi_name() !== 'cli') {
-       header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden');
-       exit();
+       throw new ForbiddenException();
 }
 
 if (($_SERVER["argc"] > 1) && isset($_SERVER["argv"][1])) {
index f0fd8cc60fd9fc71692490e9c682edc40b523272..35560feb2a8ae8899a455593a67ef07758379153 100755 (executable)
  * Usage: php bin/wait-for-connection {HOST} {PORT} [{TIMEOUT}]
  */
 
+use Friendica\Network\HTTPException\ForbiddenException;
+
 if (php_sapi_name() !== 'cli') {
-       header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden');
-       exit();
+       throw new ForbiddenException();
 }
 
 $timeout = 60;
index 2fe03cb4b215320abab1a78f00ea748ba9c57b75..a39d04963222ff61bd3d61dc5f295a7301f06823 100755 (executable)
  * Starts the background processing
  */
 
+use Friendica\Network\HTTPException\ForbiddenException;
+
 if (php_sapi_name() !== 'cli') {
-       header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden');
-       exit();
+       throw new ForbiddenException();
 }
 
 use Dice\Dice;
index 22a6ee432133e523108ce5cce4d5899af75b2b7e..fc73ae8500b365830350c76d59981daf3682b7c4 100644 (file)
@@ -246,8 +246,6 @@ function api_login(App $a)
        if (!DBA::isResult($record)) {
                Logger::debug(API_LOG_PREFIX . 'failed', ['module' => 'api', 'action' => 'login', 'parameters' => $_SERVER]);
                header('WWW-Authenticate: Basic realm="Friendica"');
-               //header('HTTP/1.0 401 Unauthorized');
-               //die('This api requires login');
                throw new UnauthorizedException("This API requires login");
        }
 
index f15b1236e81042708e97a56e0834026da05e1656..a5fddb20646695f9e11b8433dfaac69b15dece9f 100644 (file)
@@ -29,6 +29,7 @@ use Friendica\Module\Home;
 use Friendica\Module\HTTPException\MethodNotAllowed;
 use Friendica\Module\HTTPException\PageNotFound;
 use Friendica\Network\HTTPException\MethodNotAllowedException;
+use Friendica\Network\HTTPException\NoContentException;
 use Friendica\Network\HTTPException\NotFoundException;
 use Friendica\Util\Profiler;
 use Psr\Log\LoggerInterface;
@@ -291,9 +292,8 @@ class Module
                // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
                // @todo Check allowed methods per requested path
                if ($server['REQUEST_METHOD'] === Router::OPTIONS) {
-                       header('HTTP/1.1 204 No Content');
                        header('Allow: ' . implode(',', Router::ALLOWED_METHODS));
-                       exit();
+                       throw new NoContentException();
                }
 
                $placeholder = '';
index abc41ceeae4a2f9e44f67ae7e1e489dcd3656430..3eaae2c1596a0972e7b9cab3e2296d721f7f5033 100644 (file)
@@ -21,7 +21,7 @@
 
 namespace Friendica\Core\Cache;
 
-use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\ServiceUnavailableException;
 
 /**
  * Trait for Memcache to add a custom version of the
@@ -52,7 +52,7 @@ trait TraitMemcacheCommand
         *
         * @return array All keys of the memcache instance
         *
-        * @throws InternalServerErrorException
+        * @throws ServiceUnavailableException
         */
        protected function getMemcacheKeys()
        {
@@ -88,13 +88,13 @@ trait TraitMemcacheCommand
         *
         * @return string The returned buffer result
         *
-        * @throws InternalServerErrorException In case the memcache server isn't available (anymore)
+        * @throws ServiceUnavailableException In case the memcache server isn't available (anymore)
         */
        protected function sendMemcacheCommand(string $command)
        {
                $s = @fsockopen($this->server, $this->port);
                if (!$s) {
-                       throw new InternalServerErrorException("Cant connect to:" . $this->server . ':' . $this->port);
+                       throw new ServiceUnavailableException("Cant connect to:" . $this->server . ':' . $this->port);
                }
 
                fwrite($s, $command . "\r\n");
index ad1717ae83f422495425179620632d673ed867b7..4eb4084728f462b6cbbd7175a75e3ad720c1b297 100644 (file)
@@ -23,7 +23,7 @@ namespace Friendica\Core;
 
 use Exception;
 use Friendica\DI;
-use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\ServiceUnavailableException;
 use Friendica\Render\TemplateEngine;
 
 /**
@@ -69,7 +69,7 @@ class Renderer
         * @param string $template
         * @param array  $vars
         * @return string
-        * @throws InternalServerErrorException
+        * @throws ServiceUnavailableException
         */
        public static function replaceMacros(string $template, array $vars = [])
        {
@@ -87,7 +87,7 @@ class Renderer
                        $message = is_site_admin() ?
                                $e->getMessage() :
                                DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
-                       throw new InternalServerErrorException($message);
+                       throw new ServiceUnavailableException($message);
                }
 
                DI::profiler()->stopRecording();
@@ -102,7 +102,7 @@ class Renderer
         * @param string $subDir Subdirectory (Optional)
         *
         * @return string template.
-        * @throws InternalServerErrorException
+        * @throws ServiceUnavailableException
         */
        public static function getMarkupTemplate($file, $subDir = '')
        {
@@ -116,7 +116,7 @@ class Renderer
                        $message = is_site_admin() ?
                                $e->getMessage() :
                                DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
-                       throw new InternalServerErrorException($message);
+                       throw new ServiceUnavailableException($message);
                }
 
                DI::profiler()->stopRecording();
@@ -128,7 +128,7 @@ class Renderer
         * Register template engine class
         *
         * @param string $class
-        * @throws InternalServerErrorException
+        * @throws ServiceUnavailableException
         */
        public static function registerTemplateEngine($class)
        {
@@ -143,7 +143,7 @@ class Renderer
                        $message = is_site_admin() ?
                                $admin_message :
                                DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
-                       throw new InternalServerErrorException($message);
+                       throw new ServiceUnavailableException($message);
                }
        }
 
@@ -154,7 +154,7 @@ class Renderer
         * or default
         *
         * @return TemplateEngine Template Engine instance
-        * @throws InternalServerErrorException
+        * @throws ServiceUnavailableException
         */
        public static function getTemplateEngine()
        {
@@ -177,7 +177,7 @@ class Renderer
                $message = is_site_admin() ?
                        $admin_message :
                        DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
-               throw new InternalServerErrorException($message);
+               throw new ServiceUnavailableException($message);
        }
 
        /**
index c61225e482e92b70a54f2e1b644f38d12a57ef27..15f4ba60aacd4e6d847b1d10f543489b3cd632e0 100644 (file)
 namespace Friendica\Core;
 
 use Friendica\DI;
-use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\BadRequestException;
+use Friendica\Network\HTTPException\FoundException;
+use Friendica\Network\HTTPException\MovedPermanentlyException;
+use Friendica\Network\HTTPException\TemporaryRedirectException;
 use Friendica\Util\XML;
 
 /**
@@ -122,7 +125,9 @@ class System
         */
        public static function httpExit($val, $message = '', $content = '')
        {
-               Logger::log('http_status_exit ' . $val);
+               if ($val >= 400) {
+                       Logger::debug('Exit with error', ['code' => $val, 'message' => $message, 'callstack' => System::callstack(20), 'method' => $_SERVER['REQUEST_METHOD'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
+               }
                header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $message);
 
                echo $content;
@@ -132,6 +137,9 @@ class System
 
        public static function jsonError($httpCode, $data, $content_type = 'application/json')
        {
+               if ($httpCode >= 400) {
+                       Logger::debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => $_SERVER['REQUEST_METHOD'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
+               }
                header($_SERVER["SERVER_PROTOCOL"] . ' ' . $httpCode);
                self::jsonExit($data, $content_type);
        }
@@ -222,28 +230,25 @@ class System
         * @param string $url  The new Location to redirect
         * @param int    $code The redirection code, which is used (Default is 302)
         *
-        * @throws InternalServerErrorException If the URL is not fully qualified
+        * @throws BadRequestException If the URL is not fully qualified
         */
        public static function externalRedirect($url, $code = 302)
        {
                if (empty(parse_url($url, PHP_URL_SCHEME))) {
-                       throw new InternalServerErrorException("'$url' is not a fully qualified URL, please use App->internalRedirect() instead");
+                       throw new BadRequestException("'$url' is not a fully qualified URL, please use App->internalRedirect() instead");
                }
 
+               header("Location: $url");
+
                switch ($code) {
                        case 302:
-                               // this is the default code for a REDIRECT
-                               // We don't need a extra header here
-                               break;
+                               throw new FoundException();
                        case 301:
-                               header('HTTP/1.1 301 Moved Permanently');
-                               break;
+                               throw new MovedPermanentlyException();
                        case 307:
-                               header('HTTP/1.1 307 Temporary Redirect');
-                               break;
+                               throw new TemporaryRedirectException();
                }
 
-               header("Location: $url");
                exit();
        }
 
index a654cccc5b8df02730e22022e23cb3d8ee804455..88363cc397a605e72d81663fca6d95fb62df9a4e 100644 (file)
@@ -23,7 +23,7 @@ namespace Friendica\Database;
 
 use Friendica\Core\Config\Cache;
 use Friendica\Core\System;
-use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\ServiceUnavailableException;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Profiler;
 use mysqli;
@@ -520,7 +520,7 @@ class Database
                $called_from_e = ($called_from['function'] == 'e');
 
                if (!isset($this->connection)) {
-                       throw new InternalServerErrorException('The Connection is empty, although connected is set true.');
+                       throw new ServiceUnavailableException('The Connection is empty, although connected is set true.');
                }
 
                switch ($this->driver) {
index f0567f2ebd5e993d8f8e012a02fdc48a99848434..a7c0e6fc695edd1197bf84ecfd7f2841e008ceb2 100644 (file)
@@ -23,7 +23,7 @@ namespace Friendica\Factory\Api\Mastodon;
 
 use Friendica\BaseFactory;
 use Friendica\Database\Database;
-use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\UnprocessableEntityException;
 use Psr\Log\LoggerInterface;
 
 class Application extends BaseFactory
@@ -42,13 +42,13 @@ class Application extends BaseFactory
         *
         * @return \Friendica\Object\Api\Mastodon\Application
         *
-        * @throws InternalServerErrorException
+        * @throws UnprocessableEntityException
         */
        public function createFromApplicationId(int $id): \Friendica\Object\Api\Mastodon\Application
        {
                $application = $this->dba->selectFirst('application', ['client_id', 'client_secret', 'id', 'name', 'redirect_uri', 'website'], ['id' => $id]);
                if (!$this->dba->isResult($application)) {
-                       throw new InternalServerErrorException(sprintf("ID '%s' not found", $id));
+                       throw new UnprocessableEntityException(sprintf("ID '%s' not found", $id));
                }
 
                return new \Friendica\Object\Api\Mastodon\Application(
index 2d081f407752b0c9ef0fa8f83fe92526ad252679..730cf3719db1b34872c76a6e208b9552a56d1071 100644 (file)
@@ -32,8 +32,7 @@ use Friendica\DI;
 use Friendica\Factory\ConfigFactory;
 use Friendica\Model\Register;
 use Friendica\Module\BaseAdmin;
-use Friendica\Network\HTTPException\InternalServerErrorException;
-use Friendica\Util\ConfigFileLoader;
+use Friendica\Network\HTTPException\ServiceUnavailableException;
 use Friendica\Util\DateTimeFormat;
 
 class Summary extends BaseAdmin
@@ -129,7 +128,7 @@ class Summary extends BaseAdmin
                                $stream = $fileSystem->createStream($file);
 
                                if (!isset($stream)) {
-                                       throw new InternalServerErrorException('Stream is null.');
+                                       throw new ServiceUnavailableException('Stream is null.');
                                }
 
                        } catch (\Throwable $exception) {
@@ -143,7 +142,7 @@ class Summary extends BaseAdmin
                                        $stream = $fileSystem->createStream($file);
 
                                        if (!isset($stream)) {
-                                               throw new InternalServerErrorException('Stream is null.');
+                                               throw new ServiceUnavailableException('Stream is null.');
                                        }
                                }
                        } catch (\Throwable $exception) {
index 0860dfd67c50881f8e8d13daf307c6f6f119021e..dc6fb716b75f19c0215b2b57c5cb7b4d1d6a394a 100644 (file)
@@ -96,7 +96,9 @@ class Receive extends BaseModule
                if (Diaspora::dispatch($importer, $msg)) {
                        throw new HTTPException\OKException();
                } else {
-                       throw new HTTPException\InternalServerErrorException();
+                       // We couldn't process the content.
+                       // To avoid the remote system trying again we send the message that we accepted the content.
+                       throw new HTTPException\AcceptedException();
                }
        }
 
index 95a319a41a962ec699da798e249951a3a728d6c4..08e463bd409587e2b334540d1895f6bedb64dfab 100644 (file)
@@ -30,6 +30,7 @@ use Friendica\Database\PostUpdate;
 use Friendica\DI;
 use Friendica\Model\User;
 use Friendica\Network\HTTPException;
+use Friendica\Network\HTTPException\ImATeapotException;
 use Friendica\Protocol\ActivityPub;
 
 /**
index 0b03f4a7995965bbfaa5bbc36988357973b6c24c..58a2eeac56bdf436761c2652eada3f5c8a9f3cbb 100644 (file)
@@ -33,6 +33,7 @@ use Friendica\Model\Storage\ExternalResource;
 use Friendica\Model\Storage\SystemResource;
 use Friendica\Model\User;
 use Friendica\Network\HTTPException;
+use Friendica\Network\HTTPException\NotModifiedException;
 use Friendica\Object\Image;
 use Friendica\Util\Images;
 use Friendica\Util\Network;
@@ -55,7 +56,6 @@ class Photo extends BaseModule
                $totalstamp = microtime(true);
 
                if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
-                       header("HTTP/1.1 304 Not Modified");
                        header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
                        if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
                                header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
@@ -67,7 +67,7 @@ class Photo extends BaseModule
                                header_remove("Expires");
                                header_remove("Cache-Control");
                        }
-                       exit;
+                       throw new NotModifiedException();
                }
 
                Profile::addVisitorCookieForHTTPSigner();
index 15ef1c44128b70639fc337ef560a56f8023647f4..86b8a95cbb607a03183856a378bacbeebeaef2c3 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\BaseModule;
 use Friendica\Core\Logger;
 use Friendica\Core\System;
 use Friendica\DI;
+use Friendica\Network\HTTPException\NotModifiedException;
 use Friendica\Object\Image;
 use Friendica\Util\HTTPSignature;
 use Friendica\Util\Images;
@@ -53,7 +54,6 @@ class Proxy extends BaseModule
                }
 
                if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
-                       header("HTTP/1.1 304 Not Modified");
                        header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
                        if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
                                header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
@@ -65,7 +65,7 @@ class Proxy extends BaseModule
                                header_remove("Expires");
                                header_remove("Cache-Control");
                        }
-                       exit;
+                       throw new NotModifiedException();
                }
 
                if (empty($request['url'])) {
index 09e962407a6a56c4628638909d529e97218338d3..137b72b36e1be692b93d5e24ae9c812a7ff1ff62 100644 (file)
@@ -21,6 +21,7 @@
 
 namespace Friendica\Module\Special;
 
+use Friendica\Core\Logger;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
 use Friendica\DI;
@@ -42,36 +43,10 @@ class HTTPException
         */
        private static function getVars(\Friendica\Network\HTTPException $e)
        {
-               $message = $e->getMessage();
-
-               $titles = [
-                       200 => 'OK',
-                       400 => DI::l10n()->t('Bad Request'),
-                       401 => DI::l10n()->t('Unauthorized'),
-                       403 => DI::l10n()->t('Forbidden'),
-                       404 => DI::l10n()->t('Not Found'),
-                       500 => DI::l10n()->t('Internal Server Error'),
-                       503 => DI::l10n()->t('Service Unavailable'),
-               ];
-               $title = ($titles[$e->getCode()] ?? '') ?: 'Error ' . $e->getCode();
-
-               if (empty($message)) {
-                       // Explanations are taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
-                       $explanation = [
-                               400 => DI::l10n()->t('The server cannot or will not process the request due to an apparent client error.'),
-                               401 => DI::l10n()->t('Authentication is required and has failed or has not yet been provided.'),
-                               403 => DI::l10n()->t('The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource, or may need an account.'),
-                               404 => DI::l10n()->t('The requested resource could not be found but may be available in the future.'),
-                               500 => DI::l10n()->t('An unexpected condition was encountered and no more specific message is suitable.'),
-                               503 => DI::l10n()->t('The server is currently unavailable (because it is overloaded or down for maintenance). Please try again later.'),
-                       ];
-
-                       $message = $explanation[$e->getCode()] ?? '';
-               }
-
+               // Explanations are mostly taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
                $vars = [
-                       '$title' => $title,
-                       '$message' => $message,
+                       '$title' => $e->httpdesc ?: 'Error ' . $e->getCode(),
+                       '$message' => $e->getMessage() ?: $e->explanation,
                        '$back' => DI::l10n()->t('Go back'),
                        '$stack_trace' => DI::l10n()->t('Stack trace:'),
                ];
@@ -113,6 +88,10 @@ class HTTPException
        {
                header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->httpdesc);
 
+               if ($e->getCode() >= 400) {
+                       Logger::debug('Exit with error', ['code' => $e->getCode(), 'description' => $e->httpdesc, 'query' => DI::args()->getQueryString(), 'callstack' => System::callstack(20), 'method' => $_SERVER['REQUEST_METHOD'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
+               }
+
                $tpl = Renderer::getMarkupTemplate('exception.tpl');
 
                return Renderer::replaceMacros($tpl, self::getVars($e));
index 8b3b12876a9ba74a49588164fdb3635a70e8db94..23264378686b2496a4e1f20ca4fbc319c80531a9 100644 (file)
@@ -22,8 +22,7 @@
 namespace Friendica\Network;
 
 use Friendica\Core\Logger;
-use Friendica\Core\System;
-use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\UnprocessableEntityException;
 use Friendica\Util\Network;
 
 /**
@@ -102,7 +101,7 @@ class CurlResult implements IHTTPResult
         * @param string $url optional URL
         *
         * @return IHTTPResult a CURL with error response
-        * @throws InternalServerErrorException
+        * @throws UnprocessableEntityException
         */
        public static function createErrorCurl($url = '')
        {
@@ -117,12 +116,12 @@ class CurlResult implements IHTTPResult
         * @param int $errorNumber the error number or 0 (zero) if no error
         * @param string $error the error message or '' (the empty string) if no
         *
-        * @throws InternalServerErrorException when HTTP code of the CURL response is missing
+        * @throws UnprocessableEntityException when HTTP code of the CURL response is missing
         */
        public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
        {
                if (!array_key_exists('http_code', $info)) {
-                       throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
+                       throw new UnprocessableEntityException('CURL response doesn\'t contains a response HTTP code');
                }
 
                $this->returnCode = $info['http_code'];
index d6698605d0a08dc1a57d4cba28fd378a30ab13fa..1ca6bd36df1e52d6f1de5ede8e26373f239c31aa 100644 (file)
@@ -31,15 +31,11 @@ use Exception;
  */
 abstract class HTTPException extends Exception
 {
-       public $httpdesc = '';
+       public $httpdesc    = '';
+       public $explanation = '';
 
        public function __construct($message = '', Exception $previous = null)
        {
                parent::__construct($message, $this->code, $previous);
-
-               if (empty($this->httpdesc)) {
-                       $classname = str_replace('Exception', '', str_replace('Friendica\Network\HTTPException\\', '', get_class($this)));
-                       $this->httpdesc = preg_replace("|([a-z])([A-Z])|",'$1 $2', $classname);
-               }
        }
 }
index a52c1a3ea31fd9078e4cf2cd3ac875b6209982c1..e7a8e5a3e0097f69760503656b8e49cddc16dcee 100644 (file)
@@ -26,4 +26,5 @@ use Friendica\Network\HTTPException;
 class AcceptedException extends HTTPException
 {
        protected $code = 202;
+       var $httpdesc   = 'Accepted';
 }
index ceaa3189e46822d842652ede0f935b0f7938ce4e..d2f6c3fbebc477b7ed114466b861f7b705d4968d 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class BadGatewayException extends HTTPException
 {
-       protected $code = 502;
+       protected $code  = 502;
+       var $httpdesc    = 'Bad Gateway';
+       var $explanation = 'The server was acting as a gateway or proxy and received an invalid response from the upstream server.';
 }
index 5b71e5b97690dca2a0ec7a66adaad0bbd4291d5b..c2fe816afc907dca900ba1402e3609892d9ac224 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class BadRequestException extends HTTPException
 {
-       protected $code = 400;
+       protected $code  = 400;
+       var $httpdesc    = 'Bad Request';
+       var $explanation = 'The server cannot or will not process the request due to an apparent client error.';
 }
index f7e2678359345580ac8daca72c94e06f127f53c8..01cfc8dce0428ce26ce34c57bfd05b834c4948f6 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class ConflictException extends HTTPException
 {
-       protected $code = 409;
+       protected $code  = 409;
+       var $httpdesc    = 'Conflict ';
+       var $explanation = 'Indicates that the request could not be processed because of conflict in the current state of the resource, such as an edit conflict between multiple simultaneous updates.';
 }
index 4269fbde51032c012b19d8bcf687257232e176da..9ae58bcc81ccb15ab7ac327ce3c7de7848aa4188 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class ExpectationFailedException extends HTTPException
 {
-       protected $code = 417;
+       protected $code  = 417;
+       var $httpdesc    = 'Expectation Failed';
+       var $explanation = 'The server cannot meet the requirements of the Expect request-header field.';
 }
index c492cb928e6ca61ca2e69ac65f699b0b598e7f5b..a1dafa9a5ba33474e76463e4e8a5c24c55aba8dd 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class ForbiddenException extends HTTPException
 {
-       protected $code = 403;
+       protected $code  = 403;
+       var $httpdesc    = 'Forbidden';
+       var $explanation = 'The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource, or may need an account.';
 }
diff --git a/src/Network/HTTPException/FoundException.php b/src/Network/HTTPException/FoundException.php
new file mode 100644 (file)
index 0000000..c60a5f8
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Network\HTTPException;
+
+use Friendica\Network\HTTPException;
+
+class FoundException extends HTTPException
+{
+       protected $code = 302;
+       var $httpdesc   = 'Found (Moved Temporarily)';
+}
index 4aca8fd836da92a8b7a5055cd6668d2e2480cd32..2f674b2dcd05e77339008d30da30161f0079d88b 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class GatewayTimeoutException extends HTTPException
 {
-       protected $code = 504;
+       protected $code  = 504;
+       var $httpdesc    = 'Gateway Timeout';
+       var $explanation = 'The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.';
 }
index 77f0a5f7a251d8bc6db4ba56f3afa7316f2fa538..73eedcb671fb7b4fff7e0ca747f2ae22b04910b7 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class GoneException extends HTTPException
 {
-       protected $code = 410;
+       protected $code  = 410;
+       var $httpdesc    = 'Gone';
+       var $explanation = 'Indicates that the resource requested is no longer available and will not be available again.';
 }
index 689bc36a2e7e9c1afa0fa04a26825fe8cac44d99..dcfd72de0455fd5014cc4a7369cbd616c2b8e444 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\Network\HTTPException;
 
 class ImATeapotException extends HTTPException
 {
-       protected $code = 418;
-       var $httpdesc = "I'm A Teapot";
+       protected $code  = 418;
+       var $httpdesc    = "I'm A Teapot";
+       var $explanation = 'This is a teapot that is requested to brew coffee.';
 }
index 570f8e0fcf113880b409b76bad917d67358f9162..d5df8fdd3a7754fe8e49a30dc4332c46078aaa0c 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class InternalServerErrorException extends HTTPException
 {
-       protected $code = 500;
+       protected $code  = 500;
+       var $httpdesc    = 'Internal Server Error';
+       var $explanation = 'An unexpected condition was encountered and no more specific message is suitable.';
 }
index 307154712228eda64532cdb90e68bb0f96db3227..3818f681aea3c9661c6ad9de43b89b5aff2b0862 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class LenghtRequiredException extends HTTPException
 {
-       protected $code = 411;
+       protected $code  = 411;
+       var $httpdesc    = 'Length Required';
+       var $explanation = 'The request did not specify the length of its content, which is required by the requested resource.';
 }
index 6a733a37460363d55593fef5e84a6906a150122d..d09c0fe318113bc2ea37ae3475aea2f4cbd941f7 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class MethodNotAllowedException extends HTTPException
 {
-       protected $code = 405;
+       protected $code  = 405;
+       var $httpdesc    = 'Method Not Allowed';
+       var $explanation = 'A request method is not supported for the requested resource; for example, a GET request on a form that requires data to be presented via POST, or a PUT request on a read-only resource.';
 }
diff --git a/src/Network/HTTPException/MovedPermanently.php b/src/Network/HTTPException/MovedPermanently.php
new file mode 100644 (file)
index 0000000..ef78f87
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Network\HTTPException;
+
+use Friendica\Network\HTTPException;
+
+class MovedPermanentlyException extends HTTPException
+{
+       protected $code = 301;
+       var $httpdesc   = 'Moved Permanently';
+}
index 78900aff54d2b2a206cc3310e7e4d7ac12532188..90d9d2ae6aa73334f5e8215acbcc2c32a731bebe 100644 (file)
@@ -26,4 +26,5 @@ use Friendica\Network\HTTPException;
 class NoContentException extends HTTPException
 {
        protected $code = 204;
+       var $httpdesc   = 'No Content';
 }
index 1eddc8b288df7c9108deed4355e078221733da96..c9811ddb79db754cb8709b9385d50e0061b02107 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class NonAcceptableException extends HTTPException
 {
-       protected $code = 406;
+       protected $code  = 406;
+       var $httpdesc    = 'Not Acceptable';
+       var $explanation = 'The requested resource is capable of generating only content not acceptable according to the Accept headers sent in the request.';
 }
index a89d8a4e19bca1bf2ed48cee77ae436dfff5232d..9acc32031dbd69a3a653951fdb057252ed88780b 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class NotFoundException extends HTTPException
 {
-       protected $code = 404;
+       protected $code  = 404;
+       var $httpdesc    = 'Not Found';
+       var $explanation = 'The requested resource could not be found but may be available in the future.';
 }
index 3a84f6a549e5015f0e2b138dbcf62a8775f167f2..6c2d1e25e64b48393ca50209197c5bdb93079704 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class NotImplementedException extends HTTPException
 {
-       protected $code = 501;
+       protected $code  = 501;
+       var $httpdesc    = 'Not Implemented';
+       var $explanation = 'The server either does not recognize the request method, or it lacks the ability to fulfil the request. Usually this implies future availability (e.g., a new feature of a web-service API).';
 }
diff --git a/src/Network/HTTPException/NotModifiedException.php b/src/Network/HTTPException/NotModifiedException.php
new file mode 100644 (file)
index 0000000..431b209
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Network\HTTPException;
+
+use Friendica\Network\HTTPException;
+
+class NotModifiedException extends HTTPException
+{
+       protected $code = 304;
+       var $httpdesc   = 'Not Modified';
+}
index 44dc6c80b879c4d931c23dfc45f9ad16b8876f31..e4b409ce0f1dd0c26c0c54e045ef993dee3e02eb 100644 (file)
@@ -26,4 +26,5 @@ use Friendica\Network\HTTPException;
 class OKException extends HTTPException
 {
        protected $code = 200;
+       var $httpdesc   = 'OK';
 }
index 9baf5a09282e29e8b7a67b5fdea09369356fe7cd..fe4ebcb123957309e5a9a436f4dfcc88da9048aa 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class PreconditionFailedException extends HTTPException
 {
-       protected $code = 412;
+       protected $code  = 412;
+       var $httpdesc    = 'Precondition Failed';
+       var $explanation = 'The server does not meet one of the preconditions that the requester put on the request header fields.';
 }
index 2f9ef8863e952dd9e68c3f506e36bc4b73ab91a1..a88390e6a719a05be65bde02ab306194bbc02904 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class ServiceUnavailableException extends HTTPException
 {
-       protected $code = 503;
+       protected $code  = 503;
+       var $httpdesc    = 'Service Unavailable';
+       var $explanation = 'The server is currently unavailable (because it is overloaded or down for maintenance). Please try again later.';
 }
diff --git a/src/Network/HTTPException/TemporaryRedirectException.php b/src/Network/HTTPException/TemporaryRedirectException.php
new file mode 100644 (file)
index 0000000..cae4be7
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Network\HTTPException;
+
+use Friendica\Network\HTTPException;
+
+class TemporaryRedirectException extends HTTPException
+{
+       protected $code = 307;
+       var $httpdesc   = 'Temporary Redirect';
+}
index 567191fc56c43ee79464c0d8dedabd53727522d0..c94679af72b80ebc0ff83633d956076c2ee466f3 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class TooManyRequestsException extends HTTPException
 {
-       protected $code = 429;
+       protected $code  = 429;
+       var $httpdesc    = 'Too Many Requests';
+       var $explanation = 'The user has sent too many requests in a given amount of time.';
 }
index d7bcb5c740068f0453c80d0b831450483c84b160..ad6adfd4234aa62f74a85409025a258220c6c21f 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class UnauthorizedException extends HTTPException
 {
-       protected $code = 401;
+       protected $code  = 401;
+       var $httpdesc    = 'Unauthorized';
+       var $explanation = 'Authentication is required and has failed or has not yet been provided.';
 }
index 41f952941e2334158f5234208e9c2c36e9c54360..29b0d3158efe36949fd2e29f2f74baf0b12b3f93 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class UnprocessableEntityException extends HTTPException
 {
-       protected $code = 422;
+       protected $code  = 422;
+       var $httpdesc    = 'Unprocessable Entity';
+       var $explanation = 'The request was well-formed but was unable to be followed due to semantic errors.';
 }
index 00c89cebdbb357e5c75d63f86408a539b223d2bd..adf008d0155ee72d6ec87a43359ddfb7ddd58749 100644 (file)
@@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
 
 class UnsupportedMediaTypeException extends HTTPException
 {
-       protected $code = 415;
+       protected $code  = 415;
+       var $httpdesc    = 'Unsupported Media Type';
+       var $explanation = 'The request entity has a media type which the server or resource does not support. For example, the client uploads an image as image/svg+xml, but the server requires that images use a different format.';
 }
index 8eafd7d94ac07d7719e22f9c7c1d55c8095ef3e7..c336273c728a41cd42baa7326a1fe03b657f246e 100644 (file)
@@ -23,7 +23,7 @@ namespace Friendica\Render;
 
 use Friendica\Core\Hook;
 use Friendica\DI;
-use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\ServiceUnavailableException;
 use Friendica\Util\Strings;
 
 /**
@@ -54,7 +54,7 @@ final class FriendicaSmartyEngine extends TemplateEngine
                        $message = is_site_admin() ?
                                $admin_message :
                                DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
-                       throw new InternalServerErrorException($message);
+                       throw new ServiceUnavailableException($message);
                }
        }
 
index 56034a6966bf192dc81db40e03970e8c81338e3b..1e1a064910a66552f949609deda9589c22035726 100644 (file)
@@ -27,7 +27,7 @@ use Friendica\Core\Config\IConfig;
 use Friendica\Core\L10n;
 use Friendica\Core\Renderer;
 use Friendica\Model\User;
-use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\UnprocessableEntityException;
 use Friendica\Object\Email;
 use Friendica\Object\EMail\IEmail;
 use Psr\Log\LoggerInterface;
@@ -226,7 +226,7 @@ abstract class MailBuilder
         *
         * @return IEmail A new generated email
         *
-        * @throws InternalServerErrorException
+        * @throws UnprocessableEntityException
         * @throws Exception
         */
        public function build(bool $raw = false)
@@ -241,11 +241,11 @@ abstract class MailBuilder
                }
 
                if (empty($this->recipientAddress)) {
-                       throw new InternalServerErrorException('Recipient address is missing.');
+                       throw new UnprocessableEntityException('Recipient address is missing.');
                }
 
                if (empty($this->senderAddress) || empty($this->senderName)) {
-                       throw new InternalServerErrorException('Sender address or name is missing.');
+                       throw new UnprocessableEntityException('Sender address or name is missing.');
                }
 
                $this->senderNoReply = $this->senderNoReply ?? $this->senderAddress;
index f11dbcceb4c20c73505d0fe9aa007e5a774f2721..663c5a752adec86b13c6bbf29a622b83f9b2ad8a 100644 (file)
@@ -476,7 +476,7 @@ class HTTPSignature
        public static function getSigner($content, $http_headers)
        {
                if (empty($http_headers['HTTP_SIGNATURE'])) {
-                       Logger::info('No HTTP_SIGNATURE header');
+                       Logger::debug('No HTTP_SIGNATURE header');
                        return false;
                }
 
index 9c7b6a8af8890957e5894a705798b79db5de52da..2631fc75b09094d84b2547d448d6696e5df2e3c3 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\DI;
 use Friendica\Model\Contact;
+use Friendica\Network\HTTPException\NotModifiedException;
 
 class Network
 {
@@ -544,8 +545,7 @@ class Network
                header('Last-Modified: ' . $last_modified);
 
                if ($flag_not_modified) {
-                       header("HTTP/1.1 304 Not Modified");
-                       exit;
+                       throw new NotModifiedException();
                }
        }
 
index 16baea281d7c13f0c76228f0430603c6ae50940b..6d7a7050b100c8d88f414f4de96bab7c014a92fa 100644 (file)
@@ -693,7 +693,7 @@ class Notifier
        private static function notifySelfRemoval($self_user_id, $priority, $created)
        {
                $owner = User::getOwnerDataById($self_user_id);
-               if (!$owner) {
+               if (empty($self_user_id) || empty($owner)) {
                        return false;
                }
 
index c60fb7a8f466754f8d2a40b33f644f7fad3ce383..f68f4c291ea313c1b777055a1b167140b2cf3c18 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 use Friendica\DI;
+use Friendica\Network\HTTPException\NotModifiedException;
 use Friendica\Util\Strings;
 
 require_once 'view/theme/frio/theme.php';
@@ -225,8 +226,7 @@ if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && isset($_SERVER['HTTP_IF_NONE_MA
                                stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
 
        if (($cached_modified == $modified) && ($cached_etag == $etag)) {
-               header('HTTP/1.1 304 Not Modified');
-               exit();
+               throw new NotModifiedException();
        }
 }
 
index a1b492b95050afc7d8a854d276869265bbe14657..909a27fc6cbb06135e03ce39624b41691a8f600a 100644 (file)
@@ -21,6 +21,7 @@
 
 use Friendica\Core\Logger;
 use Friendica\DI;
+use Friendica\Network\HTTPException\NotModifiedException;
 
 $uid = $_REQUEST['puid'] ?? 0;
 
@@ -67,8 +68,7 @@ if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && isset($_SERVER['HTTP_IF_NONE_MA
                                stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
 
        if (($cached_modified == $modified) && ($cached_etag == $etag)) {
-               header('HTTP/1.1 304 Not Modified');
-               exit();
+               throw new NotModifiedException();
        }
 }
 echo $stylecss;