Handle exception on "head" / missing class variable added

This commit is contained in:
Michael 2023-04-11 02:11:39 +00:00
parent a74bb57298
commit d1af85b27f
4 changed files with 19 additions and 5 deletions

View file

@ -334,8 +334,8 @@ class OEmbed
$html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text)); $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
// If it doesn't parse at all, just return the text. // If it doesn't parse at all, just return the text.
$dom = @DOMDocument::loadHTML($html_text); $dom = new DOMDocument();
if (!$dom) { if (!@$dom->loadHTML($html_text)) {
return $text; return $text;
} }
$xpath = new DOMXPath($dom); $xpath = new DOMXPath($dom);

View file

@ -34,6 +34,7 @@ class OEmbed
public $type = ''; public $type = '';
public $title = ''; public $title = '';
public $description = '';
public $author_name = ''; public $author_name = '';
public $author_url = ''; public $author_url = '';
public $provider_name = ''; public $provider_name = '';

View file

@ -79,11 +79,19 @@ class Network
if (in_array(parse_url($url, PHP_URL_SCHEME), ['https', 'http'])) { if (in_array(parse_url($url, PHP_URL_SCHEME), ['https', 'http'])) {
$options = [HttpClientOptions::VERIFY => true, HttpClientOptions::TIMEOUT => $xrd_timeout]; $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. // 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])) { 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()) { if (!$curlResult->isSuccess()) {

View file

@ -70,7 +70,12 @@ class ParseUrl
$options = []; $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. // 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])) { if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() >= 400) && !in_array($curlResult->getReturnCode(), [408, 504])) {