Пример #1
0
        public void HeaderParserSingleNameValueHeader()
        {
            byte[] data = InternetMessageFormatHeaderParserTests.CreateBuffer("N:V");

            for (var cnt = 1; cnt <= data.Length; cnt++)
            {
                HttpHeaders headers;
                InternetMessageFormatHeaderParser parser =
                    InternetMessageFormatHeaderParserTests.CreateHeaderParser(
                        data.Length,
                        out headers
                        );
                Assert.NotNull(parser);

                int         totalBytesConsumed;
                ParserState state = InternetMessageFormatHeaderParserTests.ParseBufferInSteps(
                    parser,
                    data,
                    cnt,
                    out totalBytesConsumed
                    );
                Assert.Equal(ParserState.Done, state);
                Assert.Equal(data.Length, totalBytesConsumed);

                Assert.Single(headers);
                IEnumerable <string> parsedValues = headers.GetValues("N");
                string parsedValue = Assert.Single(parsedValues);
                Assert.Equal("V", parsedValue);
            }
        }
Пример #2
0
        public void HeaderParserSpecializedHeaders()
        {
            Dictionary <string, string> headerData = new Dictionary <string, string>
            {
                { @"JsonProperties0", @"{ ""SessionId"": ""{27729E1-B37B-4D29-AA0A-E367906C206E}"", ""MessageId"": ""{701332E1-B37B-4D29-AA0A-E367906C206E}"", ""TimeToLive"" : 90, ""CorrelationId"": ""{701332F3-B37B-4D29-AA0A-E367906C206E}"", ""SequenceNumber"" : 12345, ""DeliveryCount"" : 2, ""To"" : ""http://contoso.com/path1"", ""ReplyTo"" : ""http://fabrikam.com/path1"",  ""SentTimeUtc"" : ""Sun, 06 Nov 1994 08:49:37 GMT"", ""ScheduledEnqueueTimeUtc"" : ""Sun, 06 Nov 1994 08:49:37 GMT""}" },
                { @"JsonProperties1", @"{ ""SessionId"": ""{2813D4D2-46A9-4F4D-8904-E9BDE3712B70}"", ""MessageId"": ""{24AE31D6-63B8-46F3-9975-A3DAF1B6D3F4}"", ""TimeToLive"" : 80, ""CorrelationId"": ""{896DD5BD-1645-44D7-9E7C-D7F70958ECD6}"", ""SequenceNumber"" : 54321, ""DeliveryCount"" : 4, ""To"" : ""http://contoso.com/path2"", ""ReplyTo"" : ""http://fabrikam.com/path2"",  ""SentTimeUtc"" : ""Sun, 06 Nov 1994 10:49:37 GMT"", ""ScheduledEnqueueTimeUtc"" : ""Sun, 06 Nov 1994 10:49:37 GMT""}" },
                { @"P3P", @"CP=""ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI""" },
                { @"Cookie", @"omniID=1297715979621_9f45_1519_3f8a_f22f85346ac6; WT_FPC=id=65.55.227.138-2323234032.30136233:lv=1309374389020:ss=1309374389020; A=I&I=AxUFAAAAAACNCAAADYEZ7CFPss7Swnujy4PXZA!!&M=1&CS=126mAa0002ZB51a02gZB51a; MC1=GUID=568428660ad44d4ab8f46133f4b03738&HASH=6628&LV=20113&V=3; WT_NVR_RU=0=msdn:1=:2=; MUID=A44DE185EA1B4E8088CCF7B348C5D65F; MSID=Microsoft.CreationDate=03/04/2011 23:38:15&Microsoft.LastVisitDate=06/20/2011 04:15:08&Microsoft.VisitStartDate=06/20/2011 04:15:08&Microsoft.CookieId=f658f3f2-e6d6-42ab-b86b-96791b942b6f&Microsoft.TokenId=ffffffff-ffff-ffff-ffff-ffffffffffff&Microsoft.NumberOfVisits=106&Microsoft.CookieFirstVisit=1&Microsoft.IdentityToken=AA==&Microsoft.MicrosoftId=0441-6141-1523-9969; msresearch=%7B%22version%22%3A%224.6%22%2C%22state%22%3A%7B%22name%22%3A%22IDLE%22%2C%22url%22%3Aundefined%2C%22timestamp%22%3A1299281911415%7D%2C%22lastinvited%22%3A1299281911415%2C%22userid%22%3A%2212992819114151265672533023080%22%2C%22vendorid%22%3A1%2C%22surveys%22%3A%5Bundefined%5D%7D; CodeSnippetContainerLang=C#; msdn=L=1033; ADS=SN=175A21EF; s_cc=true; s_sq=%5B%5BB%5D%5D; TocHashCookie=ms310241(n)/aa187916(n)/aa187917(n)/dd273952(n)/dd295083(n)/ff472634(n)/ee667046(n)/ee667070(n)/gg259047(n)/gg618436(n)/; WT_NVR=0=/:1=query|library|en-us:2=en-us/vcsharp|en-us/library" },
                { @"Set-Cookie", @"A=I&I=AxUFAAAAAADsBgAA1sWZz4FGun/kOeyV4LGZVg!!&M=1; domain=.microsoft.com; expires=Sun, 30-Jun-2041 00:14:40 GMT; path=/" },
            };

            byte[] data = InternetMessageFormatHeaderParserTests.CreateBuffer(headerData.Select((kv) => { return(String.Format("{0}: {1}", kv.Key, kv.Value)); }).ToArray());
            for (var cnt = 1; cnt <= data.Length; cnt++)
            {
                HttpHeaders headers;
                InternetMessageFormatHeaderParser parser = InternetMessageFormatHeaderParserTests.CreateHeaderParser(data.Length, out headers);
                Assert.NotNull(parser);

                int         totalBytesConsumed = 0;
                ParserState state = InternetMessageFormatHeaderParserTests.ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
                Assert.Equal(ParserState.Done, state);
                Assert.Equal(data.Length, totalBytesConsumed);

                Assert.Equal(headerData.Count, headers.Count());
                for (int hCnt = 0; hCnt < headerData.Count; hCnt++)
                {
                    Assert.Equal(headerData.Keys.ElementAt(hCnt), headers.ElementAt(hCnt).Key);
                    Assert.Equal(headerData.Values.ElementAt(hCnt), headers.ElementAt(hCnt).Value.ElementAt(0));
                }
            }
        }
Пример #3
0
        private static void RunRfc5322SampleTest(
            string[] testHeaders,
            Action <HttpHeaders> validation
            )
        {
            byte[] data = InternetMessageFormatHeaderParserTests.CreateBuffer(testHeaders);
            for (var cnt = 1; cnt <= data.Length; cnt++)
            {
                HttpHeaders headers;
                InternetMessageFormatHeaderParser parser =
                    InternetMessageFormatHeaderParserTests.CreateHeaderParser(
                        data.Length,
                        out headers
                        );
                Assert.NotNull(parser);

                int         totalBytesConsumed = 0;
                ParserState state = InternetMessageFormatHeaderParserTests.ParseBufferInSteps(
                    parser,
                    data,
                    cnt,
                    out totalBytesConsumed
                    );
                Assert.Equal(ParserState.Done, state);
                Assert.Equal(data.Length, totalBytesConsumed);

                validation(headers);
            }
        }
Пример #4
0
        private static ParserState ParseBufferInSteps(
            InternetMessageFormatHeaderParser parser,
            byte[] buffer,
            int readsize,
            out int totalBytesConsumed
            )
        {
            ParserState state = ParserState.Invalid;

            totalBytesConsumed = 0;
            while (totalBytesConsumed <= buffer.Length)
            {
                int    size        = Math.Min(buffer.Length - totalBytesConsumed, readsize);
                byte[] parseBuffer = new byte[size];
                Buffer.BlockCopy(buffer, totalBytesConsumed, parseBuffer, 0, size);

                int bytesConsumed = 0;
                state = parser.ParseBuffer(parseBuffer, parseBuffer.Length, ref bytesConsumed);
                totalBytesConsumed += bytesConsumed;

                if (state != ParserState.NeedMoreData)
                {
                    return(state);
                }
            }

            return(state);
        }
Пример #5
0
        public void HeaderParserDataTooBigSingle()
        {
            byte[] data = InternetMessageFormatHeaderParserTests.CreateBuffer("N:V");

            for (var cnt = 1; cnt <= data.Length; cnt++)
            {
                HttpHeaders headers;
                InternetMessageFormatHeaderParser parser =
                    InternetMessageFormatHeaderParserTests.CreateHeaderParser(
                        ParserData.MinHeaderSize,
                        out headers
                        );
                Assert.NotNull(parser);

                int         totalBytesConsumed = 0;
                ParserState state = InternetMessageFormatHeaderParserTests.ParseBufferInSteps(
                    parser,
                    data,
                    cnt,
                    out totalBytesConsumed
                    );
                Assert.Equal(ParserState.DataTooBig, state);
                Assert.Equal(ParserData.MinHeaderSize, totalBytesConsumed);
            }
        }
Пример #6
0
        public void HeaderParserTestDataTooBigMulti()
        {
            byte[] data = InternetMessageFormatHeaderParserTests.CreateBuffer(
                "N1:V1",
                "N2:V2",
                "N3:V3"
                );

            for (var cnt = 1; cnt <= data.Length; cnt++)
            {
                HttpHeaders headers;
                InternetMessageFormatHeaderParser parser =
                    InternetMessageFormatHeaderParserTests.CreateHeaderParser(10, out headers);
                Assert.NotNull(parser);

                int         totalBytesConsumed = 0;
                ParserState state = InternetMessageFormatHeaderParserTests.ParseBufferInSteps(
                    parser,
                    data,
                    cnt,
                    out totalBytesConsumed
                    );
                Assert.Equal(ParserState.DataTooBig, state);
                Assert.Equal(10, totalBytesConsumed);
            }
        }
Пример #7
0
        public void HeaderParserInvalidHeader()
        {
            byte[] data = InternetMessageFormatHeaderParserTests.CreateBuffer("N1 :V1");

            for (var cnt = 1; cnt <= data.Length; cnt++)
            {
                HttpHeaders headers;
                InternetMessageFormatHeaderParser parser =
                    InternetMessageFormatHeaderParserTests.CreateHeaderParser(
                        data.Length,
                        out headers
                        );
                Assert.NotNull(parser);

                int         totalBytesConsumed = 0;
                ParserState state = InternetMessageFormatHeaderParserTests.ParseBufferInSteps(
                    parser,
                    data,
                    cnt,
                    out totalBytesConsumed
                    );
                Assert.Equal(ParserState.Invalid, state);
                Assert.Equal(data.Length - 2, totalBytesConsumed);
            }
        }
Пример #8
0
        public void HeaderParserConstructorTest()
        {
            IEnumerable <HttpHeaders> headers =
                InternetMessageFormatHeaderParserTests.CreateHttpHeaders();

            foreach (var header in headers)
            {
                InternetMessageFormatHeaderParser parser = new InternetMessageFormatHeaderParser(
                    header,
                    ParserData.MinHeaderSize
                    );
                Assert.NotNull(parser);
            }

            Assert.ThrowsArgumentGreaterThanOrEqualTo(
                () =>
                new InternetMessageFormatHeaderParser(
                    headers.ElementAt(0),
                    ParserData.MinHeaderSize - 1
                    ),
                "maxHeaderSize",
                ParserData.MinHeaderSize.ToString(),
                ParserData.MinHeaderSize - 1
                );

            Assert.ThrowsArgumentNull(
                () =>
            {
                new InternetMessageFormatHeaderParser(null, ParserData.MinHeaderSize);
            },
                "headers"
                );
        }
Пример #9
0
        public void HeaderParserLwsHeader()
        {
            byte[] data = InternetMessageFormatHeaderParserTests.CreateBuffer("N1:V1", "N2: V2", "N3:\tV3");

            for (var cnt = 1; cnt <= data.Length; cnt++)
            {
                HttpHeaders headers;
                InternetMessageFormatHeaderParser parser = InternetMessageFormatHeaderParserTests.CreateHeaderParser(data.Length, out headers);
                Assert.NotNull(parser);

                int         totalBytesConsumed = 0;
                ParserState state = InternetMessageFormatHeaderParserTests.ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
                Assert.Equal(ParserState.Done, state);
                Assert.Equal(data.Length, totalBytesConsumed);

                Assert.Equal(3, headers.Count());

                IEnumerable <string> parsedValues = headers.GetValues("N1");
                string parsedValue = Assert.Single(parsedValues);
                Assert.Equal("V1", parsedValue);

                parsedValues = headers.GetValues("N2");
                parsedValue  = Assert.Single(parsedValues);
                Assert.Equal("V2", parsedValue);

                parsedValues = headers.GetValues("N3");
                parsedValue  = Assert.Single(parsedValues);
                Assert.Equal("V3", parsedValue);
            }
        }
Пример #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MimeBodyPart"/> class.
 /// </summary>
 /// <param name="streamProvider">The stream provider.</param>
 /// <param name="maxBodyPartHeaderSize">The max length of the MIME header within each MIME body part.</param>
 public MimeBodyPart(IMultipartStreamProvider streamProvider, int maxBodyPartHeaderSize)
 {
     Contract.Assert(streamProvider != null, "Stream provider cannot be null.");
     _streamProvider = streamProvider;
     Segments = new ArrayList(2);
     _headers = FormattingUtilities.CreateEmptyContentHeaders();
     HeaderParser = new InternetMessageFormatHeaderParser(_headers, maxBodyPartHeaderSize);
 }
Пример #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MimeBodyPart"/> class.
 /// </summary>
 /// <param name="streamProvider">The stream provider.</param>
 /// <param name="maxBodyPartHeaderSize">The max length of the MIME header within each MIME body part.</param>
 public MimeBodyPart(MultipartStreamProvider streamProvider, int maxBodyPartHeaderSize)
 {
     Contract.Assert(streamProvider != null);
     _streamProvider = streamProvider;
     Segments = new List<ArraySegment<byte>>(2);
     _headers = FormattingUtilities.CreateEmptyContentHeaders();
     HeaderParser = new InternetMessageFormatHeaderParser(_headers, maxBodyPartHeaderSize);
 }
Пример #12
0
        public void HeaderParserNullBuffer()
        {
            HttpHeaders headers;
            InternetMessageFormatHeaderParser parser = InternetMessageFormatHeaderParserTests.CreateHeaderParser(128, out headers);

            Assert.NotNull(parser);
            int bytesConsumed = 0;

            Assert.ThrowsArgumentNull(() => { parser.ParseBuffer(null, 0, ref bytesConsumed); }, "buffer");
        }
Пример #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MimeBodyPart"/> class.
 /// </summary>
 /// <param name="streamProvider">The stream provider.</param>
 /// <param name="maxBodyPartHeaderSize">The max length of the MIME header within each MIME body part.</param>
 /// <param name="parentContent">The part's parent content</param>
 public MimeBodyPart(MultipartStreamProvider streamProvider, int maxBodyPartHeaderSize, HttpContent parentContent)
 {
     Contract.Assert(streamProvider != null);
     Contract.Assert(parentContent != null);
     _streamProvider = streamProvider;
     _parentContent = parentContent;
     Segments = new List<ArraySegment<byte>>(2);
     _headers = FormattingUtilities.CreateEmptyContentHeaders();
     HeaderParser = new InternetMessageFormatHeaderParser(
         _headers, 
         maxBodyPartHeaderSize, 
         ignoreHeaderValidation: true);
 }
        public void HeaderParserConstructorTest()
        {
            IEnumerable<HttpHeaders> headers = InternetMessageFormatHeaderParserTests.CreateHttpHeaders();
            foreach (var header in headers)
            {
                InternetMessageFormatHeaderParser parser = new InternetMessageFormatHeaderParser(header, ParserData.MinHeaderSize);
                Assert.NotNull(parser);
            }

            Assert.ThrowsArgumentGreaterThanOrEqualTo(() => new InternetMessageFormatHeaderParser(headers.ElementAt(0), ParserData.MinHeaderSize - 1),
                "maxHeaderSize", ParserData.MinHeaderSize.ToString(), ParserData.MinHeaderSize - 1);
            
            Assert.ThrowsArgumentNull(() => { new InternetMessageFormatHeaderParser(null, ParserData.MinHeaderSize); }, "headers");
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpRequestHeaderParser"/> class.
        /// </summary>
        /// <param name="httpRequest">The parsed HTTP request without any header sorting.</param>
        /// <param name="maxRequestLineSize">The max length of the HTTP request line.</param>
        /// <param name="maxHeaderSize">The max length of the HTTP header.</param>
        public HttpRequestHeaderParser(HttpUnsortedRequest httpRequest, int maxRequestLineSize, int maxHeaderSize)
        {
            if (httpRequest == null)
            {
                throw Error.ArgumentNull("httpRequest");
            }

            _httpRequest = httpRequest;

            // Create request line parser
            _requestLineParser = new HttpRequestLineParser(_httpRequest, maxRequestLineSize);

            // Create header parser
            _headerParser = new InternetMessageFormatHeaderParser(_httpRequest.HttpHeaders, maxHeaderSize);
        }
Пример #16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpResponseHeaderParser"/> class.
        /// </summary>
        /// <param name="httpResponse">The parsed HTTP response without any header sorting.</param>
        /// <param name="maxResponseLineSize">The max length of the HTTP status line.</param>
        /// <param name="maxHeaderSize">The max length of the HTTP header.</param>
        public HttpResponseHeaderParser(HttpUnsortedResponse httpResponse, int maxResponseLineSize, int maxHeaderSize)
        {
            if (httpResponse == null)
            {
                throw new ArgumentNullException("httpResponse");
            }

            _httpResponse = httpResponse;

            // Create status line parser
            _statusLineParser = new HttpStatusLineParser(_httpResponse, maxResponseLineSize);

            // Create header parser
            _headerParser = new InternetMessageFormatHeaderParser(_httpResponse.HttpHeaders, maxHeaderSize);
        }
Пример #17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpRequestHeaderParser"/> class.
        /// </summary>
        /// <param name="httpRequest">The parsed HTTP request without any header sorting.</param>
        /// <param name="maxRequestLineSize">The max length of the HTTP request line.</param>
        /// <param name="maxHeaderSize">The max length of the HTTP header.</param>
        public HttpRequestHeaderParser(HttpUnsortedRequest httpRequest, int maxRequestLineSize, int maxHeaderSize)
        {
            if (httpRequest == null)
            {
                throw Error.ArgumentNull("httpRequest");
            }

            _httpRequest = httpRequest;

            // Create request line parser
            _requestLineParser = new HttpRequestLineParser(_httpRequest, maxRequestLineSize);

            // Create header parser
            _headerParser = new InternetMessageFormatHeaderParser(_httpRequest.HttpHeaders, maxHeaderSize);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpResponseHeaderParser"/> class.
        /// </summary>
        /// <param name="httpResponse">The parsed HTTP response without any header sorting.</param>
        /// <param name="maxResponseLineSize">The max length of the HTTP status line.</param>
        /// <param name="maxHeaderSize">The max length of the HTTP header.</param>
        public HttpResponseHeaderParser(HttpUnsortedResponse httpResponse, int maxResponseLineSize, int maxHeaderSize)
        {
            if (httpResponse == null)
            {
                throw new ArgumentNullException("httpResponse");
            }

            _httpResponse = httpResponse;

            // Create status line parser
            _statusLineParser = new HttpStatusLineParser(_httpResponse, maxResponseLineSize);

            // Create header parser
            _headerParser = new InternetMessageFormatHeaderParser(_httpResponse.HttpHeaders, maxHeaderSize);
        }
Пример #19
0
        public void HeaderParserEmptyBuffer()
        {
            byte[]      data = InternetMessageFormatHeaderParserTests.CreateBuffer();
            HttpHeaders headers;
            InternetMessageFormatHeaderParser parser = InternetMessageFormatHeaderParserTests.CreateHeaderParser(data.Length, out headers);

            Assert.NotNull(parser);

            int         bytesConsumed = 0;
            ParserState state         = parser.ParseBuffer(data, data.Length, ref bytesConsumed);

            Assert.Equal(ParserState.Done, state);
            Assert.Equal(data.Length, bytesConsumed);

            Assert.Empty(headers);
        }
Пример #20
0
        public void HeaderParserSplitHeader()
        {
            byte[] data = InternetMessageFormatHeaderParserTests.CreateBuffer("N:V1,", " V2,", "\tV3,", "      V4,", " \tV5");

            for (var cnt = 1; cnt <= data.Length; cnt++)
            {
                HttpHeaders headers;
                InternetMessageFormatHeaderParser parser = InternetMessageFormatHeaderParserTests.CreateHeaderParser(data.Length, out headers);
                Assert.NotNull(parser);

                int         totalBytesConsumed = 0;
                ParserState state = InternetMessageFormatHeaderParserTests.ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
                Assert.Equal(ParserState.Done, state);
                Assert.Equal(data.Length, totalBytesConsumed);

                Assert.Equal(1, headers.Count());
                IEnumerable <string> parsedValues = headers.GetValues("N");
                Assert.Equal(1, parsedValues.Count());
                Assert.Equal("V1, V2, V3,      V4, \tV5", parsedValues.ElementAt(0));
            }
        }
        private static ParserState ParseBufferInSteps(InternetMessageFormatHeaderParser parser, byte[] buffer, int readsize, out int totalBytesConsumed)
        {
            ParserState state = ParserState.Invalid;
            totalBytesConsumed = 0;
            while (totalBytesConsumed <= buffer.Length)
            {
                int size = Math.Min(buffer.Length - totalBytesConsumed, readsize);
                byte[] parseBuffer = new byte[size];
                Buffer.BlockCopy(buffer, totalBytesConsumed, parseBuffer, 0, size);

                int bytesConsumed = 0;
                state = parser.ParseBuffer(parseBuffer, parseBuffer.Length, ref bytesConsumed);
                totalBytesConsumed += bytesConsumed;

                if (state != ParserState.NeedMoreData)
                {
                    return state;
                }
            }

            return state;
        }
        public ParserState ParseBuffer(
            byte[] buffer,
            int bytesReady,
            ref int bytesConsumed)
        {
            if (buffer == null)
            {
                throw Error.ArgumentNull("buffer");
            }

            ParserState parseStatus = ParserState.NeedMoreData;

            if (bytesConsumed >= bytesReady)
            {
                // We already can tell we need more data
                return(parseStatus);
            }

            try
            {
                parseStatus = InternetMessageFormatHeaderParser.ParseHeaderFields(
                    buffer,
                    bytesReady,
                    ref bytesConsumed,
                    ref _headerState,
                    _maxHeaderSize,
                    ref _totalBytesConsumed,
                    _currentHeader,
                    _headers,
                    _ignoreHeaderValidation);
            }
            catch (Exception)
            {
                parseStatus = ParserState.Invalid;
            }

            return(parseStatus);
        }