/**
* Try to get the raw HTTP response from the update site, hopefully containing the update XML.
*
* @param array $options The update options, see findUpdate() in children classes
*
* @return \Joomla\CMS\Http\Response|bool False if we can't connect to the site, HTTP Response object otherwise
*
* @throws \Exception
*/
protected function getUpdateSiteResponse($options = array())
{
$url = trim($options['location']);
$this->_url =& $url;
$this->updateSiteId = $options['update_site_id'];
if (!isset($options['update_site_name'])) {
$options['update_site_name'] = $this->getUpdateSiteName($this->updateSiteId);
}
$this->updateSiteName = $options['update_site_name'];
$this->appendExtension = false;
if (\array_key_exists('append_extension', $options)) {
$this->appendExtension = $options['append_extension'];
}
if ($this->appendExtension && substr($url, -4) !== '.xml') {
if (substr($url, -1) !== '/') {
$url .= '/';
}
$url .= 'extension.xml';
}
// Disable the update site. If the get() below fails with a fatal error (e.g. timeout) the faulty update
// site will remain disabled
$this->toggleUpdateSite($this->updateSiteId, false);
$startTime = microtime(true);
$version = new Version();
$httpOption = new Registry();
$httpOption->set('userAgent', $version->getUserAgent('Joomla', true, false));
// JHttp transport throws an exception when there's no response.
try {
$http = HttpFactory::getHttp($httpOption);
$response = $http->get($url, array(), 20);
} catch (\RuntimeException $e) {
$response = null;
}
// Enable the update site. Since the get() returned the update site should remain enabled
$this->toggleUpdateSite($this->updateSiteId, true);
// Log the time it took to load this update site's information
$endTime = microtime(true);
$timeToLoad = sprintf('%0.2f', $endTime - $startTime);
Log::add("Loading information from update site #{$this->updateSiteId} with name " . "\"{$this->updateSiteName}\" and URL {$url} took {$timeToLoad} seconds", Log::INFO, 'updater');
if ($response === null || $response->code !== 200) {
// If the URL is missing the .xml extension, try appending it and retry loading the update
if (!$this->appendExtension && substr($url, -4) !== '.xml') {
$options['append_extension'] = true;
return $this->getUpdateSiteResponse($options);
}
// Log the exact update site name and URL which could not be loaded
Log::add('Error opening url: ' . $url . ' for update site: ' . $this->updateSiteName, Log::WARNING, 'updater');
$app = Factory::getApplication();
$app->enqueueMessage(Text::sprintf('JLIB_UPDATER_ERROR_OPEN_UPDATE_SITE', $this->updateSiteId, $this->updateSiteName, $url), 'warning');
return false;
}
return $response;
}