Improved http error handling

This commit is contained in:
Michael 2021-10-29 23:21:07 +00:00
parent b2165cdf22
commit 4236a9a105
55 changed files with 282 additions and 135 deletions

View 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) {

View 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();
}
}

View 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;
/**

View 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();

View 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'])) {

View 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));