示例#1
0
        public static MIMEPart ParseContent(string content)
        {
            MIMEPart part = ParseHeader(content);

            if (content.Length > part.HeaderLength + 4)
            {
                part.Content = content.Substring(part.HeaderLength + 4);                 // 4=\r\n\r\n
            }
            return(part);
        }
示例#2
0
        public static MHTAsset FromPart(MIMEPart part)
        {
            var asset = new MHTAsset();

            try
            {
                asset.Raw      = part.Content;
                asset.Type     = part.MediaType;
                asset.Location = part.Header["Content-Location"];
                asset.Encoding = part.Header["Content-Transfer-Encoding"];
            } catch (Exception ex)
            {
                throw new Exception("不能将 MIMEPart 解析为 Asset", ex);
            }

            return(asset);
        }
        public static MHTContainer ParseContent(string content)
        {
            var mht = new MHTContainer();

            // 解析 Header 部分
            mht.HeaderPart = MIMEPart.ParseHeader(content);
            content        = content.Substring(mht.HeaderPart.HeaderLength + 4);
            mht.Parts.Add(mht.HeaderPart);
            // 获取 boundary
            string boundary = mht.HeaderPart.ContentType.Boundary;

            //if (boundary[0] == '"')
            //	boundary = boundary.Trim('"');

            // 分割

            //var contentParts = content.Split(new[] { boundary }, StringSplitOptions.RemoveEmptyEntries);
            string[] contentParts = Regex.Split(content, "^.*?" + Regex.Escape(boundary) + ".*"
                                                , RegexOptions.Compiled
                                                | RegexOptions.Multiline);
            contentParts = contentParts.Skip(0).ToArray();
            // 解析每一部分
            foreach (string strPart in contentParts)
            {
                string str = strPart.Trim();
                if (string.IsNullOrWhiteSpace(str))                 // 忽略开头和结尾的两部分
                {
                    continue;
                }

                MIMEPart part = MIMEPart.ParseContent(str);

                mht.Parts.Add(part);
                string location;
                if (part.Header.TryGetValue("Content-Location", out location))
                {
                    mht.Locations[location] = part;
                }
            }

            return(mht);
        }
示例#4
0
        public static MIMEPart ParseHeader(string content)
        {
            var part = new MIMEPart();

            // 获取 Header 长度
            part.HeaderLength = content.IndexOf("\r\n\r\n", StringComparison.Ordinal);
            if (part.HeaderLength < 0)
            {
                part.HeaderLength = content.Length;
            }
            // 获取 HeaderRaw
            part.HeaderRaw = content.Substring(0, part.HeaderLength);
            // 解析 Header
            {
                Match match = HeaderRegex.Match(part.HeaderRaw);
                while (match != Match.Empty)
                {
                    string k = match.Groups["key"].Value.Trim();
                    string v = match.Groups["value"].Value.Trim();                     // remove white space

                    part.Header.Add(k, v);
                    match = match.NextMatch();
                }
            }
            // 解析 Type 参数
            {
                string ct = part.Header["Content-Type"];
                // 不知道为什么,这里不能解析 Type=text/html 这段参数
                ct = Regex.Replace(ct, "type=[^;]+", "");
                // 也不能包含有换行
                ct = ct.Replace("\r\n\t", "");
                part.ContentType = new ContentType(ct);
            }


            return(part);
        }
示例#5
0
 public static bool IsAsset(MIMEPart part)
 {
     return(true);
 }