private static string GetContentTypeStr(Content_Type type) // 将Content-Type转为字符串 { switch (type) { case Content_Type.Text_HTML: return("text/html"); case Content_Type.Text_Plain: return("text/plain"); case Content_Type.Multi_Alter: return("multipart/alternative"); case Content_Type.Multi_Mixed: return("multipart/mixed"); case Content_Type.Multi_Related: return("multipart/related"); case Content_Type.Appli_Pdf: return("application/pdf"); case Content_Type.Image_png: return("image/png"); default: return("application/octet-stream"); } }
private void ParseEML(FileStream fsEML) { Attachments = new List <byte[]>(); StreamReader sr = new StreamReader(fsEML); string sLine; // Read file to a list List <string> listAll = new List <string>(); while ((sLine = sr.ReadLine()) != null) { listAll.Add(sLine); } fsEML.Close(); int bodyStart = -1; // Convert to array string[] allContent = new string[listAll.Count]; listAll.CopyTo(allContent); // Itterate array, merge continuation lines and add result to new list listAll = new List <string>(); List <string> header = new List <string>(); for (int i = 0; i < allContent.Length; i++) { string sFullValue = allContent[i]; MergeSplitValue(allContent, ref i, ref sFullValue); listAll.Add(sFullValue); // If body has not started yet add value to header list if (bodyStart == -1) { header.Add(sFullValue); } // Body starts with first empty line. If empty line is found and body has not started yet then this is body start line number if (allContent[i] == string.Empty && bodyStart == -1) { bodyStart = i; } } // Transfer list to array to work with allContent = new string[listAll.Count]; listAll.CopyTo(allContent); // Set main EMLFile field from header SetFields(header.ToArray()); // Exit in case of no body if (bodyStart == -1) { return; } // Multipart email with HTML body if (Content_Type != null && (Content_Type.ToLower().Contains("multipart"))) { ParseMultipart(allContent); } // Plain text body type only else { Body = string.Empty; for (int n = bodyStart + 1; n < allContent.Length; n++) { Body += allContent[n] + "\r\n"; } } if ("base64".Equals(Content_Transfer_Encoding)) { Body = Encoding.UTF8.GetString(Convert.FromBase64String(Body)); } Body = FormatBody(Body); }
public static bool Validate(Package package, out List <string> warnings) { warnings = new List <string>(); var ovfEnv = package.OvfEnvelope; if (ovfEnv == null) { warnings.Add(Messages.VALIDATION_INVALID_OVF); return(false); } log.InfoFormat("Started validation of package {0}", package.Name); var validationFlags = (ValidationFlags)Properties.Settings.Default.RequiredValidations; if (validationFlags.HasFlag(ValidationFlags.Version)) { ValidateVersion(ovfEnv, ref warnings); } if (validationFlags.HasFlag(ValidationFlags.Schema)) { ValidateSchema(package.DescriptorXml, ref warnings); } var files = package.OvfEnvelope?.References?.File ?? new File_Type[0]; if (validationFlags.HasFlag(ValidationFlags.Files)) { foreach (File_Type file in files) { string ext = Path.GetExtension(file.href).ToLower(); if (ext == Package.MANIFEST_EXT || ext == Package.CERTIFICATE_EXT) { continue; } if (!package.HasFile(file.href)) { warnings.Add(string.Format(Messages.VALIDATION_FILE_NOT_FOUND, file.href)); log.Error($"Failed to find file {file.href} listed in the reference section."); return(false); } if (!KnownFileExtensions.Contains(ext)) { warnings.Add(string.Format(Messages.VALIDATION_FILE_UNSUPPORTED_EXTENSION, file.href)); } } } VirtualDiskDesc_Type[] disks = null; NetworkSection_TypeNetwork[] networks = null; foreach (var section in ovfEnv.Sections) { if (section is DiskSection_Type diskSection) { disks = diskSection.Disk; } else if (section is NetworkSection_Type netSection) { networks = netSection.Network; } } var systems = new Content_Type[0]; switch (ovfEnv.Item) { case VirtualSystemCollection_Type vsCol: systems = vsCol.Content; break; case VirtualSystem_Type _: systems = new [] { ovfEnv.Item }; break; default: log.Error($"OVF envelope item type {ovfEnv.Item.GetType()} is not recognized."); warnings.Add(string.Format(Messages.VALIDATION_INVALID_TYPE, ovfEnv.Item.GetType())); break; } var linkedFiles = new List <File_Type>(); foreach (var system in systems) { foreach (var section in system.Items) { if (!(section is VirtualHardwareSection_Type vhs)) { continue; } var hardwareType = vhs.System?.VirtualSystemType?.Value; if (hardwareType != null && !Properties.Settings.Default.knownVirtualSystemTypes.Contains(hardwareType)) { log.Warn($"Found unexpected virtual hardware type {hardwareType}."); warnings.Add(string.Format(Messages.VALIDATION_UNKNOWN_HARDWARE_TYPE, hardwareType)); } RASD_Type[] rasds = vhs.Item; foreach (RASD_Type rasd in rasds) { if ((rasd.ResourceType.Value == 17 || rasd.ResourceType.Value == 19 || rasd.ResourceType.Value == 20 || rasd.ResourceType.Value == 21) && validationFlags.HasFlag(ValidationFlags.Files)) { ValidateDisks(rasd, files, disks, ref linkedFiles, ref warnings); } if (rasd.ResourceType.Value == 3 && validationFlags.HasFlag(ValidationFlags.Cpu)) { ValidateCpu(rasd, ref warnings); } if (rasd.ResourceType.Value == 4 && validationFlags.HasFlag(ValidationFlags.Memory)) { ValidateMemory(rasd, ref warnings); } if (rasd.ResourceType.Value == 10 && validationFlags.HasFlag(ValidationFlags.Networks)) { ValidateNetworks(rasd, networks, ref warnings); } if (validationFlags.HasFlag(ValidationFlags.Capability)) { ValidateCapability(rasd, ref warnings); } } } } foreach (File_Type file in files) { if (!linkedFiles.Contains(file)) { log.WarnFormat("Disk linkage (file to RASD) does not exist for {0}", file.href); warnings.Add(string.Format(Messages.VALIDATION_FILE_INVALID_LINKAGE, file.href)); } } log.InfoFormat("Finished validation of package {0}", package.Name); return(true); }
public StreamMessage(string streamID, Content_Type contentType, string content) { ContentType = contentType; Content = content; StreamID = streamID; }
public static bool Validate(Package package, out List <string> warnings) { warnings = new List <string>(); var ovfEnv = package.OvfEnvelope; if (ovfEnv == null) { warnings.Add(Messages.VALIDATION_INVALID_OVF); return(false); } log.InfoFormat("Started validation of package {0}", package.Name); var validationFlags = (ValidationFlags)Properties.Settings.Default.RequiredValidations; if (validationFlags.HasFlag(ValidationFlags.Version)) { ValidateVersion(ovfEnv, ref warnings); } if (validationFlags.HasFlag(ValidationFlags.Schema)) { ValidateSchema(package.DescriptorXml, ref warnings); } VirtualDiskDesc_Type[] disks = null; NetworkSection_TypeNetwork[] networks = null; foreach (var section in ovfEnv.Sections) { if (section is DiskSection_Type diskSection) { disks = diskSection.Disk; } else if (section is NetworkSection_Type netSection) { networks = netSection.Network; } } var systems = new Content_Type[0]; switch (ovfEnv.Item) { case VirtualSystemCollection_Type vsCol: systems = vsCol.Content; break; case VirtualSystem_Type _: systems = new [] { ovfEnv.Item }; break; default: log.Error($"OVF envelope item type {ovfEnv.Item.GetType()} is not recognized."); warnings.Add(string.Format(Messages.VALIDATION_INVALID_TYPE, ovfEnv.Item.GetType())); break; } foreach (var system in systems) { foreach (var section in system.Items) { if (!(section is VirtualHardwareSection_Type vhs)) { continue; } var hardwareType = vhs.System?.VirtualSystemType?.Value; if (hardwareType != null && !Properties.Settings.Default.knownVirtualSystemTypes.Contains(hardwareType)) { log.Warn($"Found unexpected virtual hardware type {hardwareType}."); warnings.Add(string.Format(Messages.VALIDATION_UNKNOWN_HARDWARE_TYPE, hardwareType)); } RASD_Type[] rasds = vhs.Item; if (validationFlags.HasFlag(ValidationFlags.Files) && !ValidateDisks(package, disks, rasds, ref warnings)) { return(false); } foreach (RASD_Type rasd in rasds) { if (rasd.ResourceType.Value == 3 && validationFlags.HasFlag(ValidationFlags.Cpu)) { ValidateCpu(rasd, ref warnings); } if (rasd.ResourceType.Value == 4 && validationFlags.HasFlag(ValidationFlags.Memory)) { ValidateMemory(rasd, ref warnings); } if (rasd.ResourceType.Value == 10 && validationFlags.HasFlag(ValidationFlags.Networks)) { ValidateNetworks(rasd, networks, ref warnings); } if (validationFlags.HasFlag(ValidationFlags.Capability)) { ValidateCapability(rasd, ref warnings); } } } } log.InfoFormat("Finished validation of package {0}", package.Name); return(true); }
private void ParseEML(Stream fsEML) { StreamReader sr = new StreamReader(fsEML); string sLine; List <string> listAll = new List <string>(); while ((sLine = sr.ReadLine()) != null) { listAll.Add(sLine); } List <string> list = new List <string>(); int nStartBody = -1; string[] saAll = new string[listAll.Count]; listAll.CopyTo(saAll); for (int i = 0; i < saAll.Length; i++) { if (saAll[i] == string.Empty) { nStartBody = i; break; } string sFullValue = saAll[i]; GetFullValue(saAll, ref i, ref sFullValue); list.Add(sFullValue); //Debug.WriteLine(sFullValue); } SetFields(list.ToArray()); if (nStartBody == -1) // no body ? { return; } // Get the body info out of saAll and set the Body and/or HTMLBody properties if (Content_Type != null && Content_Type.ToLower().Contains("multipart/alternative")) // set for HTMLBody messages { int ix = Content_Type.ToLower().IndexOf("boundary"); // boundary is used to separate the different body types if (ix == -1) { return; } string sBoundaryMarker = Content_Type.Substring(ix + 8).Trim(new char[] { '=', '"', ' ', '\t' }); // save this boundaries elements into a list of strings list = new List <string>(); for (int n = nStartBody + 1; n < saAll.Length; n++) { if (saAll[n].Contains(sBoundaryMarker)) { if (list.Count > 0) { SetBody(list); list = new List <string>(); } continue; } list.Add(saAll[n]); } } else // plain text body type only { Body = string.Empty; for (int n = nStartBody + 1; n < saAll.Length; n++) { Body += saAll[n] + "\r\n"; } } }
public string Get(string url, string Encode = "", int TimeOut = 6000, string cookies = "") { int errortimes = 4; A: try { ClassLoger.Info("HttpUtils.GetAsync", url); System.Net.WebRequest wRequest = System.Net.WebRequest.Create(url); wRequest.Method = "GET"; wRequest.UseDefaultCredentials = true; wRequest.Headers.Set(HttpRequestHeader.UserAgent, UserAgent); wRequest.Headers.Set(HttpRequestHeader.KeepAlive, "true"); wRequest.Headers.Set(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.8"); if (!Content_Encoding.IsNull()) { wRequest.Headers.Set(HttpRequestHeader.ContentEncoding, Content_Encoding); } if (!Content_Type.IsNull()) { wRequest.Headers.Set(HttpRequestHeader.ContentType, Content_Type); } if (!Accept.IsNull()) { wRequest.Headers.Set(HttpRequestHeader.Accept, Accept); } if (!Referer.IsNull()) { wRequest.Headers.Set(HttpRequestHeader.Referer, Referer); } switch (HttpProxy) { case HttpProxyType.Default: break; case HttpProxyType.IE: wRequest.Proxy = WebRequest.GetSystemWebProxy(); break; case HttpProxyType.None: wRequest.Proxy = null; break; } if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { } wRequest.Timeout = TimeOut; if (!string.IsNullOrEmpty(cookies)) { wRequest.Headers.Set(HttpRequestHeader.Cookie, cookies); } Encoding encoder = Encoding.Default; if (!Encode.IsNull()) { encoder = Encoding.GetEncoding(Encode); } System.Net.WebResponse wResp = wRequest.GetResponse(); System.IO.Stream respStream = wResp.GetResponseStream(); using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, encoder)) { return(reader.ReadToEnd()); } } catch (Exception ex) { ClassLoger.Error("HTTPHelper.Get", ex); if (errortimes > 0) { errortimes--; goto A; } } return(string.Empty); }