Exemplo n.º 1
0
        public void TestContentTypeLongMediaType()
        {
            const string contentTypeString =
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document; name=\"Hej.docx\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("application/vnd.openxmlformats-officedocument.wordprocessingml.document", contentType.MediaType);
            Assert.AreEqual("Hej.docx", contentType.Name);
        }
Exemplo n.º 2
0
        public void TestContentTypeWithSpace()
        {
            const string contentTypeString =
                "image/gif; name=\"aleabanr.gif\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("aleabanr.gif", contentType.Name);
            Assert.AreEqual("image/gif", contentType.MediaType);
        }
Exemplo n.º 3
0
        public void TestContentTypeFilenameInQoutes2()
        {
            const string contentTypeString =
                "image/jpeg;name=\"Rejsebazar_Kbh(2).jpg\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("Rejsebazar_Kbh(2).jpg", contentType.Name);
            Assert.AreEqual("image/jpeg", contentType.MediaType);
        }
Exemplo n.º 4
0
        public void TestContentTypeFilenameInQoutes()
        {
            const string contentTypeString =
                "image/gif;name=\"gradient.gif\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("gradient.gif", contentType.Name);
            Assert.AreEqual("image/gif", contentType.MediaType);
        }
Exemplo n.º 5
0
        public void TestContentTypeNameWithoutQuotes()
        {
            const string contentTypeString =
                "application/vnd.oasis.opendocument.text; name=Til";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("application/vnd.oasis.opendocument.text", contentType.MediaType);
            Assert.AreEqual("Til", contentType.Name);
        }
Exemplo n.º 6
0
        public void TestContentTypeWithNameEncoding()
        {
            const string contentTypeString =
                "application/pdf; name*=ISO-8859-1\'\'Ans%E6ttelseskontrakt.pdf";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("Ansættelseskontrakt.pdf", contentType.Name);
            Assert.AreEqual("application/pdf", contentType.MediaType);
        }
Exemplo n.º 7
0
        public void TestNoKeyValuesGetsIgnored()
        {
            //Test that values that don't have a key after the first one (which is the media type) do not override the media type
            const string contentTypeString = "text/html;iso-8859-1";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.NotNull(contentType);
            Assert.AreEqual("text/html", contentType.MediaType);
        }
Exemplo n.º 8
0
        public void TestContentTypeWithExcessEndingSemicolonAndWhitespace()
        {
            const string contentTypeString =
                "application/vnd.ms-excel;name=\"MMSCecpmcountry12_06_2010.xls\"; ";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("application/vnd.ms-excel", contentType.MediaType);
            Assert.AreEqual("MMSCecpmcountry12_06_2010.xls", contentType.Name);
        }
Exemplo n.º 9
0
        public void TestContentTypeWithFilenameIncludingSemicolon()
        {
            const string contentTypeString =
                "application/msword; name=\"NUMMER; 251478.doc\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("application/msword", contentType.MediaType);
            Assert.AreEqual("NUMMER; 251478.doc", contentType.Name);
        }
Exemplo n.º 10
0
        public void TestWhitespaceStrippedFromMediaType()
        {
            const string contentTypeString =
                "application/vnd.openxmlformats-officedocument.wordprocessingml.documen \r\n\tt";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.NotNull(contentType);
            Assert.AreEqual("application/vnd.openxmlformats-officedocument.wordprocessingml.document", contentType.MediaType);
        }
Exemplo n.º 11
0
        public void TestContentTypeBoundary()
        {
            const string contentTypeString =
                "multipart/mixed; boundary=\"_004_76C5825B768EE04E99BD2EAC9C43507557EDD335B3server1hqcbbe_\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("multipart/mixed", contentType.MediaType);
            Assert.AreEqual("_004_76C5825B768EE04E99BD2EAC9C43507557EDD335B3server1hqcbbe_", contentType.Boundary);
        }
Exemplo n.º 12
0
        public void TestContentTypeWithEncodedWordInName()
        {
            const string contentTypeString =
                "application/msword; name=\"=?Windows-1252?Q?revideret_forel=F8big_dagsorden_090110_version_2.doc?=\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("revideret foreløbig dagsorden 090110 version 2.doc", contentType.Name);
            Assert.AreEqual("application/msword", contentType.MediaType);
        }
Exemplo n.º 13
0
        public void TestContentTypeCharacterSet()
        {
            const string contentTypeString =
                "text/html; charset=\"iso-8859-1\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("text/html", contentType.MediaType);
            Assert.AreEqual("iso-8859-1", contentType.CharSet);
        }
Exemplo n.º 14
0
        public void TestContentTypeWithFormat()
        {
            const string contentTypeString =
                "text/plain; charset=\"us-ascii\"; format=\"flowed\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("text/plain", contentType.MediaType);
            Assert.AreEqual("us-ascii", contentType.CharSet);
            Assert.AreEqual("flowed", contentType.Parameters["format"]);
        }
Exemplo n.º 15
0
        public void TestContentTypeWithSpaceAndSemicolon()
        {
            const string contentTypeString =
                "text/plain; charset=\"us-ascii\" ; name=\"foo\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("text/plain", contentType.MediaType);
            Assert.AreEqual("us-ascii", contentType.CharSet);
            Assert.AreEqual("foo", contentType.Name);
        }
Exemplo n.º 16
0
        public void TestContentTypeWithSpaceAtCharacterSet()
        {
            const string contentTypeString =
                "text/plain; charset = \"us-ascii\"";

            ContentType contentType = null;

            Assert.DoesNotThrow(delegate { contentType = HeaderFieldParser.ParseContentType(contentTypeString); });

            Assert.AreEqual("us-ascii", contentType.CharSet);
        }
Exemplo n.º 17
0
        public void TestContentTypeWithMissingSemicolonAndExcesiveWhitespace()
        {
            // Notice there is no semicolon after charset="iso-8859-1"
            const string contentTypeString =
                "text/plain; charset   =   \"iso-8859-1\" name   =   \"somefile.txt\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("text/plain", contentType.MediaType);
            Assert.AreEqual("iso-8859-1", contentType.CharSet);
            Assert.AreEqual("somefile.txt", contentType.Name);
        }
Exemplo n.º 18
0
        public void TestContentTypeWithContinuationAndEncoding()
        {
            const string contentTypeString =
                "application/x-stuff;" +
                " title*0*=us-ascii\'en\'This%20is%20even%20more%20;" +
                " title*1*=%2A%2A%2Afun%2A%2A%2A%20;" +
                " title*2=\"isn\'t it!\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("This is even more ***fun*** isn\'t it!", contentType.Parameters["title"]);
        }
Exemplo n.º 19
0
        public void ParseMultiPartBoundaryWithContinuation()
        {
            const string contentTypeString =
                "multipart/report; report-type=delivery-status;" +
                " boundary*0=1804289383_1288411300_549365113_21474836;" +
                " boundary*1=47_bda2385.bisx.prod.on.blackberry";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.NotNull(contentType.Boundary);
            Assert.AreEqual("1804289383_1288411300_549365113_2147483647_bda2385.bisx.prod.on.blackberry", contentType.Boundary);
        }
Exemplo n.º 20
0
        public void TestContentTypeWithLongName()
        {
            const string contentTypeString =
                "text/plain;" +
                " name=\"very long text document name is here to test if we can parse continuation" +
                " in the name in a header.txt\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("very long text document name is here to test if we can parse" +
                            " continuation in the name in a header.txt", contentType.Name);
        }
Exemplo n.º 21
0
        public void TestIllegalContentTypeMediaTypeTrailingSlash()
        {
            const string contentTypeString =
                "text/; charset=\"utf-8\";";

            ContentType contentType = null;

            Assert.DoesNotThrow(delegate { contentType = HeaderFieldParser.ParseContentType(contentTypeString); });
            Assert.NotNull(contentType);
            Assert.AreEqual("text/plain", contentType.MediaType);
            Assert.AreEqual("utf-8", contentType.CharSet);
        }
Exemplo n.º 22
0
        public void TestContentTypeWithLongNameAndUsingEncodedWord()
        {
            const string contentTypeString =
                "text/plain;" +
                " name=\"=?ISO-8859-1?Q?very_long_text_document_name_is_here_to_te?=" +
                "=?ISO-8859-1?Q?st_if_we_can_parse_continuation_in_the_na?=" +
                "=?ISO-8859-1?Q?me_in_a_header_now_with_=C6=D8=C5=2Etxt?=\"";

            ContentType contentType = HeaderFieldParser.ParseContentType(contentTypeString);

            Assert.AreEqual("very long text document name is here to test if we can parse" +
                            " continuation in the name in a header now with ÆØÅ.txt", contentType.Name);
        }
Exemplo n.º 23
0
        public override void ProcessCommandResult(string data)
        {
            int index, index2;

            if ((index = data.IndexOf("BODY[" + ContentNumber + ".MIME]", StringComparison.Ordinal)) != -1)
            {
                if ((index2 = data.IndexOf("BODY[" + ContentNumber + ".MIME] NIL", StringComparison.Ordinal)) != -1)
                {
                    data = data.Replace("BODY[" + ContentNumber + ".MIME] NIL", "");
                }
                else
                {
                    data = CleanData(data, index).Trim();

                    if (data.StartsWith("*") && data.Contains("FETCH"))
                    {
                        data = CommandStartRex.Replace(data, "");
                    }

                    if (!string.IsNullOrEmpty(data))
                    {
                        AppendDataToContentStream(data);
                    }
                    _fetchState    = MessageFetchState.Headers;
                    _fetchProgress = _fetchProgress | MessageFetchState.Headers;
                    return;
                }
            }

            if ((index = data.IndexOf("BODY[" + ContentNumber + "]", StringComparison.Ordinal)) != -1)
            {
                data = CleanData(data, index).Trim();

                if (data.StartsWith("*") && (data.Contains("UID") || data.Contains("FETCH")))
                {
                    data = CommandStartRex.Replace(data, "");
                }

                if (!string.IsNullOrEmpty(data))
                {
                    AppendDataToContentStream(data);
                }

                _fetchState    = MessageFetchState.Body;
                _fetchProgress = _fetchProgress | MessageFetchState.Body;
                return;
            }



            if (_fetchState == MessageFetchState.Headers)
            {
                try
                {
                    Match headerMatch = Expressions.HeaderParseRex.Match(data);
                    if (!headerMatch.Success)
                    {
                        return;
                    }

                    string key   = headerMatch.Groups[1].Value.ToLower();
                    string value = headerMatch.Groups[2].Value;

                    if (this.ContentType != null && this.ContentType.MediaType == "message/rfc822")
                    {
                        _contentBuilder.AppendLine(data);
                    }

                    if (Parameters.ContainsKey(key))
                    {
                        Parameters[key] = value;
                    }
                    else
                    {
                        Parameters.Add(key, value);
                    }

                    switch (key)
                    {
                    case "content-type":
                        if (ContentType == null)
                        {
                            ContentType = HeaderFieldParser.ParseContentType(value);
                        }

                        if (!string.IsNullOrEmpty(ContentType.Name))
                        {
                            ContentType.Name = StringDecoder.Decode(ContentType.Name, true);
                            if (ContentDisposition == null)
                            {
                                ContentDisposition = new ContentDisposition
                                {
                                    DispositionType = DispositionTypeNames.Attachment
                                }
                            }
                            ;
                            ContentDisposition.FileName = ContentType.Name;
                        }

                        break;

                    case "charset":
                        if (ContentType == null)
                        {
                            ContentType = new ContentType();
                        }
                        ContentType.CharSet = value;
                        break;

                    case "filename":
                    case "name":

                        value = StringDecoder.Decode(value, true);

                        if (ContentType == null)
                        {
                            ContentType = new ContentType();
                        }

                        if (ContentDisposition == null)
                        {
                            ContentDisposition = new ContentDisposition();
                        }

                        ContentDisposition.FileName = value;

                        if (string.IsNullOrEmpty(ContentDisposition.DispositionType) && string.IsNullOrEmpty(ContentId))
                        {
                            ContentDisposition.DispositionType = DispositionTypeNames.Attachment;
                        }

                        ContentType.Name = value;
                        break;

                    case "content-id":
                        if (ContentDisposition == null)
                        {
                            ContentDisposition = new ContentDisposition();
                        }

                        ContentDisposition.DispositionType = DispositionTypeNames.Inline;

                        ContentId = value.Trim(' ', '<', '>');
                        break;

                    case "content-disposition":
                        if (ContentDisposition == null)
                        {
                            ContentDisposition = new ContentDisposition(value);
                        }

                        if (!string.IsNullOrEmpty(ContentId))
                        {
                            ContentDisposition.DispositionType = DispositionTypeNames.Inline;
                        }

                        break;

                    case "content-transfer-encoding":
                        ContentTransferEncoding = value.ToContentTransferEncoding();
                        break;
                    }
                }
                catch
                {
                }
            }
            else if (CommandEndRex.IsMatch(data))
            {
                for (var i = _contentBuilder.Length - 1; i >= 0; i--)
                {
                    if (_contentBuilder[i] == ')')
                    {
                        _contentBuilder.Remove(i, _contentBuilder.Length - i);
                        return;
                    }
                }
            }
            else if ((index = data.IndexOf("UID")) != -1)
            {
                data = CommandJunkUID.Replace(data, "");
                AppendDataToContentStream(data);
                return;
            }
            else if ((index = data.IndexOf("UID")) != -1)
            {
                data = CommandJunkUID.Replace(data, "");
                AppendDataToContentStream(data);
                return;
            }
            else
            {
                AppendDataToContentStream(data);
            }
        }
Exemplo n.º 24
0
        private void BindHeadersToFields()
        {
            foreach (var header in Headers)
            {
                switch (header.Key)
                {
                case MessageHeader.MimeVersion:
                    MimeVersion = header.Value;
                    break;

                case MessageHeader.Sender:
                    Sender = HeaderFieldParser.ParseMailAddress(header.Value);
                    break;

                case MessageHeader.Subject:
                    Subject = StringDecoder.Decode(header.Value, true);
                    break;

                case MessageHeader.To:
                    if (To.Count == 0)
                    {
                        To = HeaderFieldParser.ParseMailAddressCollection(header.Value);
                    }
                    else
                    {
                        foreach (MailAddress addr in HeaderFieldParser.ParseMailAddressCollection(header.Value))
                        {
                            To.Add(addr);
                        }
                    }
                    break;

                case MessageHeader.DeliveredTo:
                    To.Add(HeaderFieldParser.ParseMailAddress(header.Value));
                    break;

                case MessageHeader.From:
                    From = HeaderFieldParser.ParseMailAddress(header.Value);
                    break;

                case MessageHeader.Cc:
                    Cc = HeaderFieldParser.ParseMailAddressCollection(header.Value);
                    break;

                case MessageHeader.Bcc:
                    Bcc = HeaderFieldParser.ParseMailAddressCollection(header.Value);
                    break;

                case MessageHeader.Organisation:
                case MessageHeader.Organization:
                    Organization = StringDecoder.Decode(header.Value, true);
                    break;

                case MessageHeader.Date:
                    Date = HeaderFieldParser.ParseDate(header.Value);
                    break;

                case MessageHeader.Importance:
                    Importance = header.Value.ToMessageImportance();
                    break;

                case MessageHeader.ContentType:
                    ContentType = HeaderFieldParser.ParseContentType(header.Value);
                    break;

                case MessageHeader.ContentTransferEncoding:
                    ContentTransferEncoding = header.Value;
                    break;

                case MessageHeader.MessageId:
                    MessageId = header.Value;
                    break;

                case MessageHeader.Mailer:
                case MessageHeader.XMailer:
                    Mailer = header.Value;
                    break;

                case MessageHeader.ReplyTo:
                    ReplyTo = HeaderFieldParser.ParseMailAddressCollection(header.Value);
                    break;

                case MessageHeader.Sensitivity:
                    Sensitivity = header.Value.ToMessageSensitivity();
                    break;

                case MessageHeader.ReturnPath:
                    ReturnPath =
                        HeaderFieldParser.ParseMailAddress(
                            header.Value.Split(new[] { '\r', '\n' }, StringSplitOptions.None)
                            .Distinct()
                            .FirstOrDefault());
                    break;

                case MessageHeader.ContentLanguage:
                case MessageHeader.Language:
                    Language = header.Value;
                    break;

                case MessageHeader.InReplyTo:
                    InReplyTo = header.Value;
                    break;

                case MessageHeader.Comments:
                    Comments = header.Value;
                    break;
                }
            }
        }