Esempio n. 1
0
 public static UpdateInfo TryParse(byte[] bytes)
 {
     if (bytes == null) {
     return null;
      }
      string s = System.Text.Encoding.UTF8.GetString(bytes);
      s = s.Trim();
      if (s.Length <= expected.Length) {
     return null;
      }
      string initial = s.Substring(0, expected.Length);
      if (!string.Equals(initial, expected)) {
     return null;
      }
      string result = s.Substring(initial.Length);
      Regex regex = new Regex(pattern);
      MatchCollection matches = regex.Matches(result);
      if (matches.Count == 0) {
     return null;
      }
      NameValueCollection infoCollection = new NameValueCollection();
      foreach (Match match in matches) {
     infoCollection.Add(match.Groups[1].Value, match.Groups[2].Value);
      }
      string sAction = infoCollection[infoAction];
      if (string.IsNullOrEmpty(sAction)) {
     return null;
      }
      if (!Enum.IsDefined(typeof(UpdateAction), sAction)) {
     return null;
      }
      UpdateInfo updateInfo = new UpdateInfo();
      // Parse action
      updateInfo.action = (UpdateAction)Enum.Parse(typeof(UpdateAction), sAction);
      // Parse date
      DateTime date;
      if (DateTime.TryParse(infoCollection[infoDate], out date)) {
     updateInfo.date = date;
      }
      // Message
      updateInfo.message = infoCollection[infoMessage];
      // Parse url
      string strUrl = infoCollection[infoUrl];
      if (!string.IsNullOrEmpty(strUrl)) {
     strUrl = System.Web.HttpUtility.UrlDecode(strUrl);
     Uri uri;
     if (Uri.TryCreate(strUrl, UriKind.Absolute, out uri)) {
        string scheme = uri.Scheme;
        if (scheme.Equals(Uri.UriSchemeHttp) || scheme.Equals(Uri.UriSchemeHttps) ||
            scheme.Equals(Uri.UriSchemeFtp)) {
           updateInfo.uri = uri;
        }
     }
      }
      // Parse version
      try {
     updateInfo.version = new Version(infoCollection[infoVersion]);
      }
      catch (ArgumentException) {
      }
      catch (FormatException) {
      }
      catch (OverflowException) {
      }
      return updateInfo;
 }
Esempio n. 2
0
        public static UpdateInfo TryParse(byte[] bytes)
        {
            if (bytes == null)
            {
                return(null);
            }
            string s = System.Text.Encoding.UTF8.GetString(bytes);

            s = s.Trim();
            if (s.Length <= expected.Length)
            {
                return(null);
            }
            string initial = s.Substring(0, expected.Length);

            if (!string.Equals(initial, expected))
            {
                return(null);
            }
            string          result  = s.Substring(initial.Length);
            Regex           regex   = new Regex(pattern);
            MatchCollection matches = regex.Matches(result);

            if (matches.Count == 0)
            {
                return(null);
            }
            NameValueCollection infoCollection = new NameValueCollection();

            foreach (Match match in matches)
            {
                infoCollection.Add(match.Groups[1].Value, match.Groups[2].Value);
            }
            string sAction = infoCollection[infoAction];

            if (string.IsNullOrEmpty(sAction))
            {
                return(null);
            }
            if (!Enum.IsDefined(typeof(UpdateAction), sAction))
            {
                return(null);
            }
            UpdateInfo updateInfo = new UpdateInfo();

            // Parse action
            updateInfo.action = (UpdateAction)Enum.Parse(typeof(UpdateAction), sAction);
            // Parse date
            DateTime date;

            if (DateTime.TryParse(infoCollection[infoDate], out date))
            {
                updateInfo.date = date;
            }
            // Message
            updateInfo.message = infoCollection[infoMessage];
            // Parse url
            string strUrl = infoCollection[infoUrl];

            if (!string.IsNullOrEmpty(strUrl))
            {
                strUrl = System.Web.HttpUtility.UrlDecode(strUrl);
                Uri uri;
                if (Uri.TryCreate(strUrl, UriKind.Absolute, out uri))
                {
                    string scheme = uri.Scheme;
                    if (scheme.Equals(Uri.UriSchemeHttp) || scheme.Equals(Uri.UriSchemeHttps) ||
                        scheme.Equals(Uri.UriSchemeFtp))
                    {
                        updateInfo.uri = uri;
                    }
                }
            }
            // Parse version
            try {
                updateInfo.version = new Version(infoCollection[infoVersion]);
            }
            catch (ArgumentException) {
            }
            catch (FormatException) {
            }
            catch (OverflowException) {
            }
            return(updateInfo);
        }