From 2b617fc5c22766455ed200117f54c5413f7b02f4 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Sat, 13 Jul 2024 17:49:01 +0200 Subject: [PATCH] Remove contact immediately on 410 response code --- .../Capability/ICanHandleHttpResponses.php | 7 +++++++ src/Network/HTTPClient/Response/CurlResult.php | 17 +++++++++++++++++ .../HTTPClient/Response/GuzzleResponse.php | 14 ++++++++++++++ src/Worker/OnePoll.php | 6 ++++++ 4 files changed, 44 insertions(+) diff --git a/src/Network/HTTPClient/Capability/ICanHandleHttpResponses.php b/src/Network/HTTPClient/Capability/ICanHandleHttpResponses.php index 5eb2e9bf4d..76ce255a0b 100644 --- a/src/Network/HTTPClient/Capability/ICanHandleHttpResponses.php +++ b/src/Network/HTTPClient/Capability/ICanHandleHttpResponses.php @@ -84,6 +84,13 @@ interface ICanHandleHttpResponses */ public function isSuccess(): bool; + /** + * Returns if the URL is permanently gone (return code 410) + * + * @return bool + */ + public function isGone(): bool; + /** * @return string */ diff --git a/src/Network/HTTPClient/Response/CurlResult.php b/src/Network/HTTPClient/Response/CurlResult.php index 2680a8b806..15c13b779c 100644 --- a/src/Network/HTTPClient/Response/CurlResult.php +++ b/src/Network/HTTPClient/Response/CurlResult.php @@ -56,6 +56,11 @@ class CurlResult implements ICanHandleHttpResponses */ private $isSuccess; + /** + * @var boolean true (if HTTP 410 result) or false + */ + private $isGone; + /** * @var string the URL which was called */ @@ -148,6 +153,7 @@ class CurlResult implements ICanHandleHttpResponses $this->parseBodyHeader($result); $this->checkSuccess(); + $this->checkGone(); $this->checkRedirect(); $this->checkInfo(); } @@ -194,6 +200,11 @@ class CurlResult implements ICanHandleHttpResponses } } + private function checkGone() + { + $this->isGone = $this->returnCode == 410; + } + private function checkRedirect() { if (!array_key_exists('url', $this->info)) { @@ -322,6 +333,12 @@ class CurlResult implements ICanHandleHttpResponses return $this->isSuccess; } + /** {@inheritDoc} */ + public function isGone(): bool + { + return $this->isSuccess; + } + /** {@inheritDoc} */ public function getUrl(): string { diff --git a/src/Network/HTTPClient/Response/GuzzleResponse.php b/src/Network/HTTPClient/Response/GuzzleResponse.php index 277acbbc9a..31e6e89573 100644 --- a/src/Network/HTTPClient/Response/GuzzleResponse.php +++ b/src/Network/HTTPClient/Response/GuzzleResponse.php @@ -38,6 +38,8 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon private $isTimeout; /** @var boolean */ private $isSuccess; + /** @var boolean */ + private $isGone; /** * @var int the error number or 0 (zero) if no error */ @@ -63,6 +65,7 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon $this->errorNumber = $errorNumber; $this->checkSuccess(); + $this->checkGone(); $this->checkRedirect($response); } @@ -86,6 +89,11 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon } } + private function checkGone() + { + $this->isGone = $this->getStatusCode() == 410; + } + private function checkRedirect(ResponseInterface $response) { $headersRedirect = $response->getHeader(RedirectMiddleware::HISTORY_HEADER) ?? []; @@ -135,6 +143,12 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon return $this->isSuccess; } + /** {@inheritDoc} */ + public function isGone(): bool + { + return $this->isGone; + } + /** {@inheritDoc} */ public function getUrl(): string { diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index 7c784ab704..e2db89fce5 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -174,6 +174,12 @@ class OnePoll return false; } + if ($curlResult->isGone()) { + Logger::notice('URL is permanently gone', ['id' => $contact['id'], 'url' => $contact['poll']]); + Contact::remove($contact['id']); + return false; + } + if ($curlResult->redirectIsPermanent()) { Logger::notice('Poll address permanently changed', [ 'id' => $contact['id'],