From d1af85b27fe3004ed671d9a23adab8fa724f5416 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 11 Apr 2023 02:11:39 +0000 Subject: [PATCH] Handle exception on "head" / missing class variable added --- src/Content/OEmbed.php | 4 ++-- src/Object/OEmbed.php | 1 + src/Util/Network.php | 12 ++++++++++-- src/Util/ParseUrl.php | 7 ++++++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Content/OEmbed.php b/src/Content/OEmbed.php index c113110662..7afdfac35c 100644 --- a/src/Content/OEmbed.php +++ b/src/Content/OEmbed.php @@ -334,8 +334,8 @@ class OEmbed $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text)); // If it doesn't parse at all, just return the text. - $dom = @DOMDocument::loadHTML($html_text); - if (!$dom) { + $dom = new DOMDocument(); + if (!@$dom->loadHTML($html_text)) { return $text; } $xpath = new DOMXPath($dom); diff --git a/src/Object/OEmbed.php b/src/Object/OEmbed.php index 2511a0f758..3f53f48cd1 100644 --- a/src/Object/OEmbed.php +++ b/src/Object/OEmbed.php @@ -34,6 +34,7 @@ class OEmbed public $type = ''; public $title = ''; + public $description = ''; public $author_name = ''; public $author_url = ''; public $provider_name = ''; diff --git a/src/Util/Network.php b/src/Util/Network.php index e02767be4a..f7ceab5433 100644 --- a/src/Util/Network.php +++ b/src/Util/Network.php @@ -79,11 +79,19 @@ class Network if (in_array(parse_url($url, PHP_URL_SCHEME), ['https', 'http'])) { $options = [HttpClientOptions::VERIFY => true, HttpClientOptions::TIMEOUT => $xrd_timeout]; - $curlResult = DI::httpClient()->head($url, $options); + try { + $curlResult = DI::httpClient()->head($url, $options); + } catch (\Exception $e) { + return false; + } // Workaround for systems that can't handle a HEAD request. Don't retry on timeouts. if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() >= 400) && !in_array($curlResult->getReturnCode(), [408, 504])) { - $curlResult = DI::httpClient()->get($url, HttpClientAccept::DEFAULT, $options); + try { + $curlResult = DI::httpClient()->get($url, HttpClientAccept::DEFAULT, $options); + } catch (\Exception $e) { + return false; + } } if (!$curlResult->isSuccess()) { diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index 3382884839..79f427a654 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -70,7 +70,12 @@ class ParseUrl $options = []; } - $curlResult = DI::httpClient()->head($url, array_merge([HttpClientOptions::ACCEPT_CONTENT => $accept], $options)); + try { + $curlResult = DI::httpClient()->head($url, array_merge([HttpClientOptions::ACCEPT_CONTENT => $accept], $options)); + } catch (\Exception $e) { + DI::logger()->debug('Got exception', ['url' => $url, 'message' => $e->getMessage()]); + return []; + } // Workaround for systems that can't handle a HEAD request. Don't retry on timeouts. if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() >= 400) && !in_array($curlResult->getReturnCode(), [408, 504])) { -- 2.39.5