diff --git a/CFUValidator/Updates/AlternativeUrlDownloadUpdatesXmlSourceAction.cs b/CFUValidator/Updates/AlternativeUrlDownloadUpdatesXmlSourceAction.cs index 3946e6a43..45a903b1d 100644 --- a/CFUValidator/Updates/AlternativeUrlDownloadUpdatesXmlSourceAction.cs +++ b/CFUValidator/Updates/AlternativeUrlDownloadUpdatesXmlSourceAction.cs @@ -32,50 +32,51 @@ using System; using System.IO; using System.Net; -using System.Threading; using System.Xml; using XenAdmin.Actions; -using XenAdmin.Core; -using XenAPI; + namespace CFUValidator.Updates { class AlternativeUrlDownloadUpdatesXmlSourceAction : DownloadUpdatesXmlAction, ICheckForUpdatesXMLSource { - private readonly string newLocation; + private readonly string _url; public AlternativeUrlDownloadUpdatesXmlSourceAction(string url) - : base(true, true, true, "CFU", "1", url) + : base(true, true, true, "CFU", "1") { - newLocation = url; - ErrorRaised = null; + _url = url ?? throw new ArgumentNullException(nameof(url)); } - protected override XmlDocument FetchCheckForUpdatesXml(string location) - { - XmlDocument xdoc; - using (Stream xmlstream = GetXmlDoc()) - { - xdoc = Helpers.LoadXmlDocument(xmlstream); - } - return xdoc; - } - - private Stream GetXmlDoc() + protected override XmlDocument FetchCheckForUpdatesXml() { try { - WebRequest wr = WebRequest.Create(newLocation); - return wr.GetResponse().GetResponseStream(); + XmlDocument doc = new XmlDocument(); + XmlReaderSettings settings = new XmlReaderSettings + { + IgnoreComments = true, + IgnoreWhitespace = true, + IgnoreProcessingInstructions = true + }; + + WebRequest wr = WebRequest.Create(_url); + using (Stream xmlStream = wr.GetResponse().GetResponseStream()) + { + if (xmlStream != null) + using (var reader = XmlReader.Create(xmlStream, settings)) + doc.Load(reader); + } + + return doc; } catch (Exception) { - ErrorRaised = new CFUValidationException("Failed to wget the URL: " + newLocation); + ErrorRaised = new CFUValidationException("Failed to wget the URL: " + _url); throw ErrorRaised; } } public Exception ErrorRaised { get; private set; } - } } diff --git a/CFUValidator/Updates/ReadFromFileUpdatesXmlSource.cs b/CFUValidator/Updates/ReadFromFileUpdatesXmlSource.cs index 71876e47a..d256bfb3d 100644 --- a/CFUValidator/Updates/ReadFromFileUpdatesXmlSource.cs +++ b/CFUValidator/Updates/ReadFromFileUpdatesXmlSource.cs @@ -38,40 +38,37 @@ namespace CFUValidator.Updates { class ReadFromFileUpdatesXmlSource : DownloadUpdatesXmlAction, ICheckForUpdatesXMLSource { - private readonly string newLocation; + private readonly string _location; + public ReadFromFileUpdatesXmlSource(string location) - : base(true, true, true, "CFU", "1", location) + : base(true, true, true, "CFU", "1") { - newLocation = location; - ErrorRaised = null; + _location = location ?? throw new ArgumentNullException(nameof(location)); } - protected override XmlDocument FetchCheckForUpdatesXml(string location) + protected override XmlDocument FetchCheckForUpdatesXml() { - if (!File.Exists(newLocation)) + if (!File.Exists(_location)) { - ErrorRaised = new CFUValidationException("File not found at: " + newLocation); + ErrorRaised = new CFUValidationException("File not found at: " + _location); throw ErrorRaised; } try { XmlDocument xdoc = new XmlDocument(); - using (StreamReader sr = new StreamReader(newLocation)) - { + using (StreamReader sr = new StreamReader(_location)) xdoc.Load(sr); - } return xdoc; } catch(Exception) { - ErrorRaised = new CFUValidationException("Could not read/parse file: " + newLocation); + ErrorRaised = new CFUValidationException("Could not read/parse file: " + _location); throw ErrorRaised; } } public Exception ErrorRaised { get; private set; } - } } diff --git a/XenAdmin/Core/Registry.cs b/XenAdmin/Core/Registry.cs index b26566d32..4862aaffb 100644 --- a/XenAdmin/Core/Registry.cs +++ b/XenAdmin/Core/Registry.cs @@ -165,7 +165,11 @@ namespace XenAdmin.Core public static string AdditionalFeatures => ReadInstalledKey(ADDITIONAL_FEATURES); - public static string CustomUpdatesXmlLocation => ReadString(CUSTOM_UPDATES_XML_LOCATION); + public static string GetCustomUpdatesXmlLocation() + { + return ReadRegistryValue(RegistryHive.CurrentUser, XENCENTER_LOCAL_KEYS, CUSTOM_UPDATES_XML_LOCATION) ?? + ReadRegistryValue(RegistryHive.LocalMachine, XENCENTER_LOCAL_KEYS, CUSTOM_UPDATES_XML_LOCATION); + } public static string CustomHelpUrl => ReadString(HELP_URL_OVERRIDE); diff --git a/XenAdmin/Core/Updates.cs b/XenAdmin/Core/Updates.cs index 308b0a2e4..5f15ef217 100644 --- a/XenAdmin/Core/Updates.cs +++ b/XenAdmin/Core/Updates.cs @@ -50,8 +50,6 @@ namespace XenAdmin.Core { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); - private static readonly string CheckForUpdatesUrl = Registry.CustomUpdatesXmlLocation ?? BrandManager.UpdatesUrl; - public static event Action CheckForUpdatesCompleted; public static event Action CheckForUpdatesStarted; public static event Action RestoreDismissedUpdatesStarted; @@ -122,7 +120,7 @@ namespace XenAdmin.Core string userAgentId = GetUniqueIdHash(); return new DownloadUpdatesXmlAction(checkForXenCenter, checkForServerVersion, checkForPatches, - userAgent, userAgentId, CheckForUpdatesUrl); + userAgent, userAgentId); } internal static string GetUniqueIdHash() diff --git a/XenAdmin/WinformsXenAdminConfigProvider.cs b/XenAdmin/WinformsXenAdminConfigProvider.cs index eb77efb6d..e105f9b4d 100644 --- a/XenAdmin/WinformsXenAdminConfigProvider.cs +++ b/XenAdmin/WinformsXenAdminConfigProvider.cs @@ -212,6 +212,11 @@ namespace XenAdmin public string GetXenCenterMetadata(bool isForXenCenter) { return Metadata.Generate(PluginManager, isForXenCenter); - } + } + + public string GetCustomUpdatesXmlLocation() + { + return Registry.GetCustomUpdatesXmlLocation(); + } } } diff --git a/XenModel/Actions/Updates/DownloadUpdatesXmlAction.cs b/XenModel/Actions/Updates/DownloadUpdatesXmlAction.cs index 8dfa662e6..997ef86aa 100644 --- a/XenModel/Actions/Updates/DownloadUpdatesXmlAction.cs +++ b/XenModel/Actions/Updates/DownloadUpdatesXmlAction.cs @@ -32,13 +32,11 @@ using System; using System.Collections.Generic; using System.Linq; -using XenAPI; using System.IO; using System.Xml; using XenAdmin.Core; using System.Diagnostics; using System.Net; -using System.Text; namespace XenAdmin.Actions @@ -54,9 +52,9 @@ namespace XenAdmin.Actions private const string RequiredPatchNode = "requiredpatch"; - public List XenCenterVersions { get; private set; } - public List XenServerVersions { get; private set; } - public List XenServerPatches { get; private set; } + public List XenCenterVersions { get; } = new List(); + public List XenServerVersions { get; } = new List(); + public List XenServerPatches { get; } = new List(); public List XenServerVersionsForAutoCheck { @@ -73,24 +71,17 @@ namespace XenAdmin.Actions private readonly bool _checkForXenCenter; private readonly bool _checkForServerVersion; private readonly bool _checkForPatches; - private readonly string _checkForUpdatesUrl; private readonly string _userAgent; private readonly string _userAgentId; - public DownloadUpdatesXmlAction(bool checkForXenCenter, bool checkForServerVersion, bool checkForPatches, string userAgent, string userAgentId, string checkForUpdatesUrl) + public DownloadUpdatesXmlAction(bool checkForXenCenter, bool checkForServerVersion, bool checkForPatches, string userAgent, string userAgentId) : base(null, "_get_updates", "_get_updates", true) { - Debug.Assert(checkForUpdatesUrl != null, "Parameter checkForUpdatesUrl should not be null. This class does not default its value anymore."); Debug.Assert(!string.IsNullOrWhiteSpace(userAgent) && !string.IsNullOrWhiteSpace(userAgentId)); - XenServerPatches = new List(); - XenServerVersions = new List(); - XenCenterVersions = new List(); - _checkForXenCenter = checkForXenCenter; _checkForServerVersion = checkForServerVersion; _checkForPatches = checkForPatches; - _checkForUpdatesUrl = checkForUpdatesUrl; _userAgent = userAgent; _userAgentId = userAgentId; } @@ -99,12 +90,11 @@ namespace XenAdmin.Actions { this.Description = Messages.AVAILABLE_UPDATES_SEARCHING; - XmlDocument xdoc = FetchCheckForUpdatesXml(_checkForUpdatesUrl); + XmlDocument xdoc = FetchCheckForUpdatesXml(); GetXenCenterVersions(xdoc); GetXenServerPatches(xdoc); GetXenServerVersions(xdoc); - } private void GetXenCenterVersions(XmlDocument xdoc) @@ -327,18 +317,20 @@ namespace XenAdmin.Actions } } - protected virtual XmlDocument FetchCheckForUpdatesXml(string location) + protected virtual XmlDocument FetchCheckForUpdatesXml() { var xdoc = new XmlDocument(); - var uri = new Uri(location); - var proxy = XenAdminConfigManager.Provider.GetProxyFromSettings(Connection, false); - + var checkForUpdatesUrl = XenAdminConfigManager.Provider.GetCustomUpdatesXmlLocation() ?? BrandManager.UpdatesUrl; + var uri = new Uri(checkForUpdatesUrl); + if (uri.IsFile) { - xdoc.Load(location); + xdoc.Load(checkForUpdatesUrl); } else { + var proxy = XenAdminConfigManager.Provider.GetProxyFromSettings(Connection, false); + using (var webClient = new WebClient()) { webClient.Proxy = proxy; @@ -346,9 +338,7 @@ namespace XenAdmin.Actions webClient.Headers.Add("X-User-Agent-Id", _userAgentId); using (var stream = new MemoryStream(webClient.DownloadData(uri))) - { xdoc.Load(stream); - } } } diff --git a/XenModel/Utils/Helpers.cs b/XenModel/Utils/Helpers.cs index 8ce224855..4cbbcb99f 100755 --- a/XenModel/Utils/Helpers.cs +++ b/XenModel/Utils/Helpers.cs @@ -1445,25 +1445,6 @@ namespace XenAdmin.Core return null; } - /// - /// Load an xml stream and ignore comments and whitespace - /// - /// - /// - public static XmlDocument LoadXmlDocument(Stream xmlStream) - { - XmlDocument doc = new XmlDocument(); - XmlReaderSettings settings = new XmlReaderSettings(); - - settings.IgnoreComments = true; - settings.IgnoreWhitespace = true; - settings.IgnoreProcessingInstructions = true; - - doc.Load(XmlReader.Create(xmlStream, settings)); - - return doc; - } - public static Regex HostnameOrIpRegex = new Regex(@"[\w.]+"); public static string HostnameFromLocation(string p) diff --git a/XenModel/XenAdminConfigManager.cs b/XenModel/XenAdminConfigManager.cs index 589765b93..acfacccf1 100644 --- a/XenModel/XenAdminConfigManager.cs +++ b/XenModel/XenAdminConfigManager.cs @@ -63,5 +63,6 @@ namespace XenAdmin void SaveSettingsIfRequired(); bool ShowHiddenVMs { get; } string GetXenCenterMetadata(bool isForXenCenter); + string GetCustomUpdatesXmlLocation(); } } diff --git a/XenServerHealthCheck/XenServerHealthCheckConfigProvider.cs b/XenServerHealthCheck/XenServerHealthCheckConfigProvider.cs index c16bb599e..2a4be9cb1 100644 --- a/XenServerHealthCheck/XenServerHealthCheckConfigProvider.cs +++ b/XenServerHealthCheck/XenServerHealthCheckConfigProvider.cs @@ -73,6 +73,11 @@ namespace XenServerHealthCheck return metadataString.Replace(HealthCheckSettings.REPORT_TIME_PLACEHOLDER, DateTime.UtcNow.ToString("u")); } + public string GetCustomUpdatesXmlLocation() + { + return string.Empty; + } + public int GetProxyTimeout(bool timeout) { return timeout ? Properties.Settings.Default.HttpTimeout : 0;