* Fetch the content type of the given url
* @param string $url URL of the page
* @param string $accept content-type to accept
+ * @param int $timeout
* @return array content type
*/
- public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT): array
+ public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT, int $timeout = 0): array
{
- $curlResult = DI::httpClient()->head($url, [HttpClientOptions::ACCEPT_CONTENT => $accept]);
+ if (!empty($timeout)) {
+ $options = [HttpClientOptions::TIMEOUT => $timeout];
+ } else {
+ $options = [];
+ }
+
+ $curlResult = DI::httpClient()->head($url, array_merge([HttpClientOptions::ACCEPT_CONTENT => $accept], $options));
- // Workaround for systems that can't handle a HEAD request
- if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) {
- $curlResult = DI::httpClient()->get($url, $accept, [HttpClientOptions::CONTENT_LENGTH => 1000000]);
+ // 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, $accept, array_merge([HttpClientOptions::CONTENT_LENGTH => 1000000], $options));
}
if (!$curlResult->isSuccess()) {
case 'news_keywords':
$keywords = explode(',', $meta_tag['content']);
break;
+ default:
+ Logger::debug('Unsupported meta-tag found', ['meta-tag' => $meta_tag]);
}
}
{
if (!empty($siteinfo['images'])) {
array_walk($siteinfo['images'], function (&$image) use ($page_url) {
- // According to the specifications someone could place a picture url into the content field as well.
- // But this doesn't seem to happen in the wild, so we don't cover it here.
+ /*
+ * According to the specifications someone could place a picture
+ * URL into the content field as well. But this doesn't seem to
+ * happen in the wild, so we don't cover it here.
+ */
if (!empty($image['url'])) {
$image['url'] = self::completeUrl($image['url'], $page_url);
$photodata = Images::getInfoFromURLCached($image['url']);
- if (!empty($photodata) && ($photodata[0] > 50) && ($photodata[1] > 50)) {
+ if (($photodata) && ($photodata[0] > 50) && ($photodata[1] > 50)) {
$image['src'] = $image['url'];
$image['width'] = $photodata[0];
$image['height'] = $photodata[1];