Example #1
0
        private MimePart ParseHeader()
        {
            MimePart result = new MimePart();
            // header is terminated by an empty line.
            string line = ReadLine();

            while (line != null && line != "")
            {
                if (line.StartsWith(ContentTypePrefix, StringComparison.InvariantCultureIgnoreCase))
                {
                    ParseContentType(result, line.Substring(ContentTypePrefix.Length).Trim());
                }
                else if (line.StartsWith(ContentEncodingPrefix, StringComparison.InvariantCultureIgnoreCase))
                {
                    result.ContentEncoding = line.Substring(ContentEncodingPrefix.Length).Trim();
                }
                else if (line.StartsWith(ContentLocationPrefix, StringComparison.InvariantCultureIgnoreCase))
                {
                    result.ContentLocation = line.Substring(ContentLocationPrefix.Length).Trim();
                }
                else if (line.StartsWith(ContentIdPrefix, StringComparison.InvariantCultureIgnoreCase))
                {
                    result.ContentLocation = line.Substring(ContentIdPrefix.Length).Trim();
                }
                else if (line.StartsWith(ContentDescriptionPrefix, StringComparison.InvariantCultureIgnoreCase))
                {
                    result.ContentLocation = line.Substring(ContentDescriptionPrefix.Length).Trim();
                }
                else
                {
                    // ignore unknown headers.
                }

                line = ReadLine();
            }

            return(result);
        }
Example #2
0
        private bool FixHtmlLinks(MimePart part)
        {
            bool save    = false;
            bool changed = false;
            var  doc     = part.HtmlDocument;

            // find any "a" tags with "href" attributes and fix them.
            foreach (XElement anchor in doc.Descendants(doc.Root.Name.Namespace + "a"))
            {
                string href    = (string)anchor.Attribute("href");
                string newhref = MapUrl(href);
                if (href != newhref)
                {
                    anchor.SetAttributeValue("href", System.Uri.EscapeUriString(newhref));
                    changed = true;
                }
            }
            if (changed)
            {
                part.Body = doc.ToString();
                save      = true;
            }
            return(save);
        }
Example #3
0
        public void ParseContentType(MimePart part, string s)
        {
            int i = s.IndexOf('/');

            if (i < 0)
            {
                Error("Malformed content type missing subtype after '/'");
            }

            part.Type = s.Substring(0, i).Trim();

            s = s.Substring(i + 1);

            i = s.IndexOf(';');
            if (i < 0)
            {
                part.SubType = s.Trim();
                return;
            }
            else
            {
                part.SubType = s.Substring(0, i).Trim();
            }

            // parameters: attribute "=" value
            while (i >= 0)
            {
                s = s.Substring(i + 1);
                i = s.IndexOf('=');
                if (i < 0)
                {
                    Error(part.Type + " parameter missing equals '='");
                }

                string paramName = s.Substring(0, i).Trim();

                bool quoted = false;
                s = s.Substring(i + 1);
                string paramValue = s.Trim();

                if (paramValue.Length > 1 && paramValue[0] == '"')
                {
                    quoted = true;
                    // quoted value.
                    int endQuote = paramValue.IndexOf('"', 1);
                    if (endQuote > 0)
                    {
                        s          = paramValue.Substring(endQuote + 1);
                        paramValue = paramValue.Substring(1, endQuote - 1);
                    }
                    else
                    {
                        Error("Missing end quote on value for parameter " + paramName);
                    }
                }


                i = s.IndexOf(';');

                if (!quoted && i >= 0)
                {
                    paramValue = paramValue.Substring(0, i).Trim();
                }

                part.ContentTypeParameters.Add(paramName, paramValue);
            }
        }