From 377baa1527f2c22858d02f01bcac89148ce00378 Mon Sep 17 00:00:00 2001 From: joshuaruesweg Date: Thu, 4 Mar 2021 19:52:40 +0100 Subject: [PATCH] Add helper method for inaccessible urls --- .../message/unfurl/UnfurlResponse.class.php | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php b/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php index a038713c3b..fc0bd6bedc 100644 --- a/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php +++ b/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php @@ -88,20 +88,12 @@ final class UnfurlResponse } catch (BadResponseException $e) { $response = $e->getResponse(); - switch ($response->getStatusCode()) { - case 400: // Bad Request - case 401: // Unauthorized - case 402: // Payment Required - case 403: // Forbidden - case 404: // Not Found - case 406: // Not Acceptable - $message = "Request failed with status code {$response->getStatusCode()}."; - - throw new UrlInaccessible($message, $response->getStatusCode(), $e); - break; - - default: - throw new DownloadFailed("Could not download content.", $response->getStatusCode(), $e); + if (self::isUrlInaccessible($response)) { + $message = "Request failed with status code {$response->getStatusCode()}."; + + throw new UrlInaccessible($message, $response->getStatusCode(), $e); + } else { + throw new DownloadFailed("Could not download content.", $response->getStatusCode(), $e); } } catch (ClientExceptionInterface $e) { throw new DownloadFailed("Could not download content.", 0, $e); @@ -310,26 +302,34 @@ final class UnfurlResponse } catch (BadResponseException $e) { $response = $e->getResponse(); - switch ($response->getStatusCode()) { - case 400: // Bad Request - case 401: // Unauthorized - case 402: // Payment Required - case 403: // Forbidden - case 404: // Not Found - case 406: // Not Acceptable - $message = "Request failed with status code {$response->getStatusCode()}."; - - throw new UrlInaccessible($message, $response->getStatusCode(), $e); - break; + if (self::isUrlInaccessible($response)) { + $message = "Request failed with status code {$response->getStatusCode()}."; - default: - throw new DownloadFailed("Could not download content.", $response->getStatusCode(), $e); + throw new UrlInaccessible($message, $response->getStatusCode(), $e); + } else { + throw new DownloadFailed("Could not download content.", $response->getStatusCode(), $e); } } catch (ClientExceptionInterface $e) { throw new DownloadFailed("Could not download content.", 0, $e); } } + private static function isUrlInaccessible(Response $response): bool + { + switch ($response->getStatusCode()) { + case 400: // Bad Request + case 401: // Unauthorized + case 402: // Payment Required + case 403: // Forbidden + case 404: // Not Found + case 406: // Not Acceptable + return true; + break; + } + + return false; + } + /** * Returns a "static" instance of the HTTP client to use to allow * for TCP connection reuse. -- 2.20.1