<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;

namespace NetSparkle
{
    /// <summary>
    /// An app-cast 
    /// </summary>
    public class NetSparkleAppCast
    {
        private const string itemNode = "item";
        private const string enclosureNode = "enclosure";
        private const string releaseNotesLinkNode = "sparkle:releaseNotesLink";
        private const string descriptionNode = "description";
        private const string versionAttribute = "sparkle:version";
        private const string dsaSignature = "sparkle:dsaSignature";
        private const string criticalAttribute = "sparkle:critical";
        private const string lengthAttribute = "length";
        private const string urlAttribute = "url";
        private const string pubDateNode = "pubDate";

        private readonly Sparkle _sparkle;
        private readonly NetSparkleConfiguration _config;
        private readonly String _castUrl;
        private readonly List<NetSparkleAppCastItem> _items;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="castUrl">the URL of the appcast file</param>
        /// <param name="config">the current configuration</param>
        public NetSparkleAppCast(string castUrl, Sparkle sparkle, NetSparkleConfiguration config)
        {
            _sparkle = sparkle;
            _config = config;
            _castUrl = castUrl;

            _items = new List<NetSparkleAppCastItem>();
        }

        private string TryReadSignature()
        {
            try
            {
                var signaturestream = _sparkle.GetWebContentStream(_castUrl + ".dsa");
                var signature = string.Empty;
                using (StreamReader reader = new StreamReader(signaturestream, Encoding.ASCII))
                {
                    return reader.ReadToEnd().Trim();
                }
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Download castUrl resource and parse it
        /// </summary>
        public bool Read()
        {
            try
            {
                var inputstream = _sparkle.GetWebContentStream(_castUrl);
                var signature = TryReadSignature();
                return ReadStream(inputstream, signature);
            }
            catch (Exception e)
            {
                _sparkle.ReportDiagnosticMessage(string.Format("error reading app cast {0}: {1} ", _castUrl, e.Message));
                return false;
            }
        }

        private bool ReadStream(Stream inputstream, String signature)
        {
            if (inputstream == null)
            {
                _sparkle.ReportDiagnosticMessage("Cannot read response from URL " + _castUrl);
                return false;
            }

            // inputstream needs to be copied. WebResponse can't ne positionized back
            var memorystream = new MemoryStream();
            inputstream.CopyTo(memorystream);
            memorystream.Position = 0;

            // checking signature
            var signatureNeeded = _sparkle.DSAVerificator.SignatureNeeded();
            if (signatureNeeded && _sparkle.DSAVerificator.VerifyDSASignature(signature, memorystream) == ValidationResult.Invalid)
            {
                _sparkle.ReportDiagnosticMessage("Signature check of appcast failed");
                return false;
            }
            memorystream.Position = 0;

            // parse xml
            using (XmlTextReader reader = new XmlTextReader(memorystream))
            {
                Parse(reader);
                return true;
            }
        }

        private void Parse(XmlReader reader)
        {
            NetSparkleAppCastItem currentItem = null;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                        case itemNode:
                            currentItem = new NetSparkleAppCastItem()
                            {
                                AppVersionInstalled = _config.InstalledVersion,
                                AppName = _config.ApplicationName<commit_msg>Make sure to set some NetSparkleAppCastItem members on constructor<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;

namespace NetSparkle
{
    /// <summary>
    /// An app-cast 
    /// </summary>
    public class NetSparkleAppCast
    {
        private const string itemNode = "item";
        private const string enclosureNode = "enclosure";
        private const string releaseNotesLinkNode = "sparkle:releaseNotesLink";
        private const string descriptionNode = "description";
        private const string versionAttribute = "sparkle:version";
        private const string dsaSignature = "sparkle:dsaSignature";
        private const string criticalAttribute = "sparkle:critical";
        private const string lengthAttribute = "length";
        private const string urlAttribute = "url";
        private const string pubDateNode = "pubDate";

        private readonly Sparkle _sparkle;
        private readonly NetSparkleConfiguration _config;
        private readonly String _castUrl;
        private readonly List<NetSparkleAppCastItem> _items;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="castUrl">the URL of the appcast file</param>
        /// <param name="config">the current configuration</param>
        public NetSparkleAppCast(string castUrl, Sparkle sparkle, NetSparkleConfiguration config)
        {
            _sparkle = sparkle;
            _config = config;
            _castUrl = castUrl;

            _items = new List<NetSparkleAppCastItem>();
        }

        private string TryReadSignature()
        {
            try
            {
                var signaturestream = _sparkle.GetWebContentStream(_castUrl + ".dsa");
                var signature = string.Empty;
                using (StreamReader reader = new StreamReader(signaturestream, Encoding.ASCII))
                {
                    return reader.ReadToEnd().Trim();
                }
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Download castUrl resource and parse it
        /// </summary>
        public bool Read()
        {
            try
            {
                var inputstream = _sparkle.GetWebContentStream(_castUrl);
                var signature = TryReadSignature();
                return ReadStream(inputstream, signature);
            }
            catch (Exception e)
            {
                _sparkle.ReportDiagnosticMessage(string.Format("error reading app cast {0}: {1} ", _castUrl, e.Message));
                return false;
            }
        }

        private bool ReadStream(Stream inputstream, String signature)
        {
            if (inputstream == null)
            {
                _sparkle.ReportDiagnosticMessage("Cannot read response from URL " + _castUrl);
                return false;
            }

            // inputstream needs to be copied. WebResponse can't ne positionized back
            var memorystream = new MemoryStream();
            inputstream.CopyTo(memorystream);
            memorystream.Position = 0;

            // checking signature
            var signatureNeeded = _sparkle.DSAVerificator.SignatureNeeded();
            if (signatureNeeded && _sparkle.DSAVerificator.VerifyDSASignature(signature, memorystream) == ValidationResult.Invalid)
            {
                _sparkle.ReportDiagnosticMessage("Signature check of appcast failed");
                return false;
            }
            memorystream.Position = 0;

            // parse xml
            using (XmlTextReader reader = new XmlTextReader(memorystream))
            {
                Parse(reader);
                return true;
            }
        }

        private void Parse(XmlReader reader)
        {
            NetSparkleAppCastItem currentItem = null;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                        case itemNode:
                            currentItem = new NetSparkleAppCastItem()
                            {
                                AppVersionInstalled = _config.InstalledVersion,
                                AppName = _config.ApplicationName,
                                UpdateSize = 0,
                                IsCriticalUpdate = false
{'fraction_non_alphanumeric': 0.07752971231811619, 'fraction_numerical': 0.000999666777740753, 'mean_word_length': 1.6090988119385685, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5262560', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2061, 'n_words': 539}