Exemplo n.º 1
0
        public void ProcessHeaderData_csv_Test()
        {
            var data   = @"LastName, FirstName, Gender, FavoriteColor, DateOfBirth";
            var result = headerParser.Parse(data);

            Assert.AreEqual(result.Delimiter, ',');
        }
Exemplo n.º 2
0
        public void ParsesHeaders()
        {
            var bytes   = Encoding.Default.GetBytes(Properties.Resources.HeaderSample);
            var stream  = new MemoryStream(bytes);
            var headers = HeaderParser.Parse(stream);

            Assert.Equal(headers["Content-Length"].Single(), "8");
            Assert.Equal(headers["Proxy-Connection"].Single(), "Keep-Alive");
            Assert.Equal(headers["Transfer-Encoding"].Single(), "chunked");
            Assert.Equal(headers["Via"].Single(), "1.1 TK5-PRXY-21");
            Assert.Equal(headers["Expires"].Single(), "Thu, 19 Nov 1981 08:52:00 GMT");
            Assert.Equal(headers["Date"].Single(), "Mon, 16 Jan 2012 23:39:47 GMT");
            Assert.Equal(headers["Server"].Single(), "nginx/0.6.30");
            Assert.Equal(headers["X-Powered-By"].Single(), "PHP/5.2.4-2ubuntu5.6");
            Assert.Equal(headers["Pragma"].Single(), "no-cache");
            Assert.Equal(headers["X-Pingback"].Single(), "http://whereslou.com/xmlrpc.php");
            Assert.Equal(headers["Host"].Single(), "whereslou.com");
            Assert.Equal(headers["User-Agent"].Single(), "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0");
            Assert.Equal(headers["Accept"].Single(), "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            Assert.Equal(headers["Accept-Language"].Single(), "en-us,en;q=0.5");
            Assert.Equal(headers["Accept-Encoding"].Single(), "gzip, deflate");
            Assert.Equal(headers["Accept-Charset"].Single(), "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            Assert.Equal(headers["Cookie"].Single(), "PHPSESSID=a3047ee50d8ba3f4302a9926aasdf; wordpress_test_cookie=WP+Cookie+check; wp-settings-1=editor%3Dhtml%26m0%3Do%26m1%3Do%26m2%3Dc%26m3%3Dc%26m4%3Dc%26m5%3Do%26m6%3Do%26m7%3Do%26m8%3Dc%26m9%3Dc%26m10%3Dc%26imgsize%3Dmedium%26urlbutton%3Durlfile%26align%3Dright; wp-settings-time-1=1326754593; __utma=24333308.2009914498.1326754717.1326754717.1326754717.1; __utmc=24333308; __utmz=24333308.1326754717.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)");

            Assert.Contains("max-age=0", headers["Cache-Control"]);
            Assert.Contains("no-store, no-cache, must-revalidate, post-check=0, pre-check=0", headers["Cache-Control"]);

            Assert.Contains("text/html", headers["Content-Type"]);
            Assert.Contains("text/html; charset=UTF-8", headers["Content-Type"]);
            Assert.Contains("keep-alive", headers["Connection"]);
            Assert.Contains("Keep-Alive", headers["Connection"]);
        }
Exemplo n.º 3
0
        public void ParseHeader_SkipBody()
        {
            const string HttpPost = @"POST / HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 11
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
Referer: http://localhost:8080/ajaxPost.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: sv,en;q=0.8,en-US;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ASP.NET_SessionId=5vkr4tfivb1ybu1sm4u4kahy; GriffinLanguageSwitcher=sv-se; __RequestVerificationToken=LiTSJATsiqh8zlcft_3gZwvY8HpcCUkirm307njxIZLdsJSYyqaV2st1tunH8sMvMwsVrj3W4dDoV8ECZRhU4s6DhTvd2F-WFkgApDBB-CA1; .ASPXAUTH=BF8BE1C246428B10B49AE867BEDF9748DB3842285BC1AF1EC44AD80281C4AE084B75F0AE13EAF1BE7F71DD26D0CE69634E83C4846625DC7E4D976CA1845914E2CC7A7CF2C522EA5586623D9B73B0AE433337FC59CF6AF665DC135491E78978EF

hello=world";
            string       actual   = "";
            var          slice    = new SocketBufferFake();
            var          buffer   = Encoding.UTF8.GetBytes(HttpPost);

            slice.SetBuffer(buffer, 0, buffer.Length);
            slice.BytesTransferred = buffer.Length;
            var parser = new HeaderParser();

            parser.HeaderParsed += (name, value) => actual = value;
            slice.Offset         = parser.Parse(slice, 0);

            Assert.Equal("ASP.NET_SessionId=5vkr4tfivb1ybu1sm4u4kahy; GriffinLanguageSwitcher=sv-se; __RequestVerificationToken=LiTSJATsiqh8zlcft_3gZwvY8HpcCUkirm307njxIZLdsJSYyqaV2st1tunH8sMvMwsVrj3W4dDoV8ECZRhU4s6DhTvd2F-WFkgApDBB-CA1; .ASPXAUTH=BF8BE1C246428B10B49AE867BEDF9748DB3842285BC1AF1EC44AD80281C4AE084B75F0AE13EAF1BE7F71DD26D0CE69634E83C4846625DC7E4D976CA1845914E2CC7A7CF2C522EA5586623D9B73B0AE433337FC59CF6AF665DC135491E78978EF", actual);
            Assert.Equal('h', (char)slice.Buffer[slice.Offset]);
        }
Exemplo n.º 4
0
        public void Parse(Stream stream)
        {
            if (stream == null)
            {
                throw new InvalidOperationException("Stream cannot be null");
            }

            GedcomLine lastLine = default;

            using (var reader = new StreamReader(stream))
            {
                var firstRawLine = reader.ReadLine();

                if (firstRawLine == null)
                {
                    throw new InvalidOperationException("File empty");
                }

                var firstLine = ParserHelper.ParseLine(firstRawLine);
                if (firstLine.Level != 0 && firstLine.GetTagOrRef() != "HEAD")
                {
                    throw new InvalidOperationException("GEDCOM Header Not Found");
                }

                LineProvider lineProvider = new LineProvider(reader);

                var gedcomHeaderParse = HeaderParser.Parse(firstLine, lineProvider);
                _headerCallback?.Invoke(gedcomHeaderParse.Result);

                var newLine = gedcomHeaderParse.Line;
                lastLine = newLine;

                while (!newLine.Equals(default))
Exemplo n.º 5
0
        static CodeCompilationUnit ParseAutoGenHeader(string content)
        {
            //at this version, use a simple parser
            //very specific to this header
            List <string> lines           = new List <string>();
            bool          startCollecting = true;

            using (StringReader reader = new StringReader(content))
            {
                string line = reader.ReadLine();
                while (line != null)
                {
                    if (!line.StartsWith("#") && !line.StartsWith("//"))
                    {
                        lines.Add(line);
                    }
                    else
                    {
                        //temp fix
                        //insert blank line,just want to preserve line number
                        lines.Add("");
                    }
                    line = reader.ReadLine();
                }
            }

            //
            HeaderParser headerParser = new HeaderParser();

            headerParser.Parse("virtual_filename", lines);
            return(headerParser.Result);
        }
        /// <summary>
        ///     We've received bytes from the socket. Build a message out of them.
        /// </summary>
        /// <param name="buffer">Buffer</param>
        public void ProcessReadBytes(ISocketBuffer buffer)
        {
            var receiveBufferOffset      = buffer.Offset;
            var bytesLeftInReceiveBuffer = buffer.BytesTransferred;

            while (true)
            {
                if (bytesLeftInReceiveBuffer <= 0)
                {
                    break;
                }


                if (!_isHeaderParsed)
                {
                    var offsetBefore = receiveBufferOffset;
                    receiveBufferOffset = _headerParser.Parse(buffer, receiveBufferOffset);
                    if (!_isHeaderParsed)
                    {
                        return;
                    }

                    if (_message == null)
                    {
                        throw new HttpException(HttpStatusCode.InternalServerError, "Failed to decode message properly. Decoder state: " + _headerParser.State);
                    }

                    bytesLeftInReceiveBuffer -= receiveBufferOffset - offsetBefore;
                    _frameContentBytesLeft    = _message.ContentLength;
                    if (_frameContentBytesLeft == 0)
                    {
                        TriggerMessageReceived(_message);
                        _message        = null;
                        _isHeaderParsed = false;
                        continue;
                    }

                    if (_message == null)
                    {
                        throw new HttpException(HttpStatusCode.InternalServerError, "Failed to decode message properly. Decoder state: " + _headerParser.State);
                    }
                    _message.Body = new MemoryStream();
                }

                var bytesRead    = BytesProcessed(buffer.Offset, receiveBufferOffset);
                var bytesToWrite = Math.Min(_frameContentBytesLeft, buffer.BytesTransferred - bytesRead);
                _message.Body.Write(buffer.Buffer, receiveBufferOffset, bytesToWrite);
                _frameContentBytesLeft   -= bytesToWrite;
                receiveBufferOffset      += bytesToWrite;
                bytesLeftInReceiveBuffer -= bytesToWrite;
                if (_frameContentBytesLeft == 0)
                {
                    _message.Body.Position = 0;
                    TriggerMessageReceived(_message);
                    Clear();
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 抓取新闻
        /// </summary>
        /// <returns></returns>
        private NewsBody[] CrawleNews()
        {
            LogManager.WriteLine("Crawle news rss...");

            List <NewsHeader> headers = new List <NewsHeader>();

            for (int i = 0; i < ConfigManager.Config.Rss.Length; i++)
            {
                string xmlUrl = ConfigManager.Config.Rss[i][0];

                if (xmlUrl.StartsWith("#"))//使用#暂时屏蔽订阅
                {
                    continue;
                }

                string rssClass = ConfigManager.Config.Rss[i][1];
                try
                {
                    string xml = Client.GET(xmlUrl);
                    headers.AddRange(RssParser.Parse(xml, rssClass));
                }
                catch (Exception ex)
                {
                    LogManager.ShowException(ex, "Cannot get " + xmlUrl);
                }
            }


            LogManager.WriteLine("Crawle news body...");
            int count = 0;

            List <NewsBody> bodyList = new List <NewsBody>();

            foreach (NewsHeader header in headers)
            {
                count++;
                if (count % 5 == 0)//每爬去5个新闻提示一次
                {
                    LogManager.WriteLine(string.Format("<{0}> items done...", count));
                }
                try
                {
                    NewsBody body = HeaderParser.Parse(header);

                    bodyList.Add(body);
                }
                catch (Exception ex)
                {
                    LogManager.ShowException(ex);
                }
            }

            return(bodyList.ToArray());
        }
Exemplo n.º 8
0
        public void Parse()
        {
            var buffer = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nSERVER: LOCALHOST\r\n\r\n");
            var slice  = new SocketBufferFake();

            slice.SetBuffer(buffer, 0, buffer.Length);


            var parser = new HeaderParser();

            parser.HeaderParsed += (name, value) => Console.WriteLine(name + ": " + value);
            parser.Parse(slice, 0);
        }
Exemplo n.º 9
0
            public void Execute(string path)
            {
                try
                {
                    path = Path.GetFullPath(path);

                    using (_console.CreateScope("Generating table of contents..."))
                    {
                        var updatedFilesCounter = 0;
                        var elementParser       = new DocsElementParser();
                        var headerParser        = new HeaderParser();
                        var files = new MarkdownFileLocator(_fileSystem).GetFiles(path);

                        foreach (var file in files)
                        {
                            var lines = _fileSystem.ReadFile(file.FullPath);
                            var toc   = elementParser.Parse(lines, "toc").SingleOrDefault();

                            if (toc == null)
                            {
                                continue;
                            }

                            updatedFilesCounter++;
                            _console.WriteInfo($"Updating {file.RelativePath}");

                            var headers = headerParser.Parse(lines.Skip(toc.ElementLine));

                            lines.RemoveRange(toc.ElementLine, toc.ElementLines);

                            var minLevel = headers.Min(x => x.Level);
                            headers.ForEach(x => x.Level -= minLevel);
                            var formatter  = new LinkFormatter();
                            var tocContent = headers.Select(x =>
                                                            $"{Enumerable.Repeat("  ", x.Level).Join()}* {formatter.Format(x.Text)}");
                            lines.InsertRange(toc.ElementLine, new DocsElementWriter().Write(toc, tocContent));

                            _fileSystem.WriteFile(file.FullPath, lines);
                        }

                        _console.WriteInfo($"Updated {updatedFilesCounter} {(updatedFilesCounter == 1 ? "file" : "files")}.");
                    }
                }
                catch (Exception exception)
                {
                    _console.WriteError(exception.Message);
                }
            }
Exemplo n.º 10
0
        /// <summary>
        /// Default the input data schema. since we do not have a header available.
        /// </summary>
        protected override void ProcessRequest()
        {
            var dm = _headerParser.Parse(_personData);

            //default header fields as we do not have access to the header data. in rest.api
            dm.Fields = new Dictionary <string, int>
            {
                { "firstname", 0 },
                { "lastname", 1 },
                { "gender", 2 },
                { "favoritecolor", 3 },
                { "dateofbirth", 4 }
            };

            var person = _personParser.Parse(_personData, dm);

            _service.Add(person);
        }
Exemplo n.º 11
0
        static async Task MainAsync(CancellationToken cancellationToken)
        {
            var function = new Function();

            function.PrepareFunctionContext();

            System.Diagnostics.Debug.WriteLine("C# AfterBurn running.");

            var httpFormatter = new HttpFormatter();
            var stdin         = Console.OpenStandardInput();

            using (TextReader reader = new StreamReader(stdin))
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    HeaderParser parser = new HeaderParser();
                    var          header = parser.Parse(reader);

                    foreach (string v in header.HttpHeaders)
                    {
                        System.Diagnostics.Debug.WriteLine(v + "=" + header.HttpHeaders[v]);
                    }

                    System.Diagnostics.Debug.WriteLine("Content-Length: " + header.ContentLength);

                    BodyParser bodyParser = new BodyParser();
                    string     body       = String.Empty;
                    if (header.ContentLength > 0)
                    {
                        body = bodyParser.Parse(reader, header.ContentLength);

                        System.Diagnostics.Debug.WriteLine(body);
                    }

                    await function.Invoke(body, cancellationToken);

                    var httpAdded = httpFormatter.Format("");
                    Console.WriteLine(httpAdded);
                }
            }
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            var function = new Function.Handler();

            System.Diagnostics.Debug.WriteLine("C# AfterBurn running.");

            var httpFormatter = new HttpFormatter();
            var stdin         = Console.OpenStandardInput();

            using (TextReader reader = new StreamReader(stdin)) {
                while (true)
                {
                    HeaderParser parser = new HeaderParser();
                    var          header = parser.Parse(reader);

                    foreach (string v in header.HttpHeaders)
                    {
                        System.Diagnostics.Debug.WriteLine(v + "=" + header.HttpHeaders[v]);
                    }

                    System.Diagnostics.Debug.WriteLine("Content-Length: " + header.ContentLength);

                    BodyParser bodyParser = new BodyParser();
                    char[]     body       = new char[0] {
                    };
                    if (header.ContentLength > 0)
                    {
                        body = bodyParser.Parse(reader, header.ContentLength);

                        System.Diagnostics.Debug.WriteLine(body);
                    }

                    var httpAdded = httpFormatter.Format(function.Invoke(body));
                    Console.WriteLine(httpAdded);
                }
            }
        }
Exemplo n.º 13
0
        State?ReadHeaders(IByteBuffer buffer)
        {
            IHttpMessage httpMessage = _message;
            HttpHeaders  headers     = httpMessage.Headers;

            AppendableCharSequence line = _headerParser.Parse(buffer);

            if (line is null)
            {
                return(null);
            }
            // ReSharper disable once ConvertIfDoToWhile
            if ((uint)line.Count > 0u)
            {
                do
                {
                    byte firstChar = line.Bytes[0];
                    if (_name is object && (firstChar == c_space || firstChar == c_tab))
                    {
                        //please do not make one line from below code
                        //as it breaks +XX:OptimizeStringConcat optimization
                        ICharSequence trimmedLine = CharUtil.Trim(line);
                        _value = new AsciiString($"{_value} {trimmedLine}");
                    }
                    else
                    {
                        if (_name is object)
                        {
                            _ = headers.Add(_name, _value);
                        }
                        SplitHeader(line);
                    }

                    line = _headerParser.Parse(buffer);
                    if (line is null)
                    {
                        return(null);
                    }
                } while ((uint)line.Count > 0u);
            }

            // Add the last header.
            if (_name is object)
            {
                _ = headers.Add(_name, _value);
            }

            // reset name and value fields
            _name  = null;
            _value = null;

            var  values = headers.GetAll(HttpHeaderNames.ContentLength);
            uint contentLengthValuesCount = (uint)values.Count;

            if (contentLengthValuesCount > 0u)
            {
                // Guard against multiple Content-Length headers as stated in
                // https://tools.ietf.org/html/rfc7230#section-3.3.2:
                //
                // If a message is received that has multiple Content-Length header
                //   fields with field-values consisting of the same decimal value, or a
                //   single Content-Length header field with a field value containing a
                //   list of identical decimal values (e.g., "Content-Length: 42, 42"),
                //   indicating that duplicate Content-Length header fields have been
                //   generated or combined by an upstream message processor, then the
                //   recipient MUST either reject the message as invalid or replace the
                //   duplicated field-values with a single valid Content-Length field
                //   containing that decimal value prior to determining the message body
                //   length or forwarding the message.
                if (contentLengthValuesCount > 1u && httpMessage.ProtocolVersion == HttpVersion.Http11)
                {
                    ThrowHelper.ThrowArgumentException_Multiple_Content_Length_Headers_Found();
                }
                if (!long.TryParse(values[0].ToString(), out _contentLength))
                {
                    ThrowHelper.ThrowArgumentException_Invalid_Content_Length();
                }
            }

            if (IsContentAlwaysEmpty(httpMessage))
            {
                HttpUtil.SetTransferEncodingChunked(httpMessage, false);
                return(State.SkipControlChars);
            }
            else if (HttpUtil.IsTransferEncodingChunked(httpMessage))
            {
                if (contentLengthValuesCount > 0u && httpMessage.ProtocolVersion == HttpVersion.Http11)
                {
                    HandleTransferEncodingChunkedWithContentLength(httpMessage);
                }

                return(State.ReadChunkSize);
            }
            else if (ContentLength() >= 0L)
            {
                return(State.ReadFixedLengthContent);
            }
            else
            {
                return(State.ReadVariableLengthContent);
            }
        }
Exemplo n.º 14
0
        public void TestParse()
        {
            // Arrange
            const string Csv =
                ",Arrange,,,,,,,,,,,Assertion,,,,,,,,,,,,,,,,,,,,\r\n" +
                ",HttpRequest Expected,,HttpRequest Actual,,,,,,,,,Uri,StatusCode,Headers,,Cookies,,Contents,,,,,,,,,,,,,,\r\n" +
                ",BaseUri,PathInfos,BaseUri,Headers,Cookies,,PathInfos,,QueryStrings,,Fragment,,,Content-Type,Last-Modified,Location,Degree,Name,IsList,IsDateTime,IsTime,Expected,,,,,,Actual,,,,\r\n" +
                ",,,,User-Agent,Location,Degree,,,locations,type,,,,,,,,,,,,Value,Query,Attribute,Pattern,Format,FormatCulture,Query,Attribute,Pattern,Format,FormatCulture";

            // Act
            var root = HeaderParser.Parse(new CsvParser().Parse(Csv));

            HeaderValidator.Validate(root);

            // Assert

            // depth=-1 (root)
            Assert.AreEqual("Root", root.Name);
            Assert.AreEqual(2, root.Children.Count);
            Assert.AreEqual(-1, root.Depth);
            Assert.AreEqual(0, root.From);
            Assert.AreEqual(32, root.To);

            // depth=0 (Arrange)
            var arrange = root.Children.ElementAt(0);

            Assert.AreEqual("Arrange", arrange.Name);
            Assert.AreEqual(0, arrange.Depth);
            Assert.AreEqual(1, arrange.From);
            Assert.AreEqual(11, arrange.To);
            Assert.AreEqual(2, arrange.Children.Count);

            // depth=0 (Assertion)
            var assertion = root.Children.ElementAt(1);

            Assert.AreEqual("Assertion", assertion.Name);
            Assert.AreEqual(0, assertion.Depth);
            Assert.AreEqual(12, assertion.From);
            Assert.AreEqual(32, assertion.To);
            Assert.AreEqual(5, assertion.Children.Count);

            // depth=1 (HttpRequest expected)
            var expected = arrange.Children.ElementAt(0);

            Assert.AreEqual("HttpRequest Expected", expected.Name);
            Assert.AreEqual(1, expected.Depth);
            Assert.AreEqual(1, expected.From);
            Assert.AreEqual(2, expected.To);
            Assert.AreEqual(2, expected.Children.Count);

            // depth=1 (HttpRequest actual)
            var actual = arrange.Children.ElementAt(1);

            Assert.AreEqual("HttpRequest Actual", actual.Name);
            Assert.AreEqual(1, actual.Depth);
            Assert.AreEqual(3, actual.From);
            Assert.AreEqual(11, actual.To);
            Assert.AreEqual(6, actual.Children.Count);

            // depth=1 (Uri)
            var uri = assertion.Children.ElementAt(0);

            Assert.AreEqual("Uri", uri.Name);
            Assert.AreEqual(1, uri.Depth);
            Assert.AreEqual(12, uri.From);
            Assert.AreEqual(12, uri.To);
            Assert.AreEqual(0, uri.Children.Count);

            // depth=1 (StatusCode)
            var statusCode = assertion.Children.ElementAt(1);

            Assert.AreEqual("StatusCode", statusCode.Name);
            Assert.AreEqual(1, statusCode.Depth);
            Assert.AreEqual(13, statusCode.From);
            Assert.AreEqual(13, statusCode.To);
            Assert.AreEqual(0, uri.Children.Count);

            // depth=1 (Headers)
            var headersA = assertion.Children.ElementAt(2);

            Assert.AreEqual("Headers", headersA.Name);
            Assert.AreEqual(1, headersA.Depth);
            Assert.AreEqual(14, headersA.From);
            Assert.AreEqual(15, headersA.To);
            Assert.AreEqual(2, headersA.Children.Count);

            // depth=1 (Cookies)
            var cookiesA = assertion.Children.ElementAt(3);

            Assert.AreEqual("Cookies", cookiesA.Name);
            Assert.AreEqual(1, cookiesA.Depth);
            Assert.AreEqual(16, cookiesA.From);
            Assert.AreEqual(17, cookiesA.To);
            Assert.AreEqual(2, cookiesA.Children.Count);

            // depth=1 (Contents)
            var contents = assertion.Children.ElementAt(4);

            Assert.AreEqual("Contents", contents.Name);
            Assert.AreEqual(1, contents.Depth);
            Assert.AreEqual(18, contents.From);
            Assert.AreEqual(32, contents.To);
            Assert.AreEqual(6, contents.Children.Count);

            // depth=2 (expected BaseUri)
            var baseUriArrange = expected.Children.ElementAt(0);

            Assert.AreEqual("BaseUri", baseUriArrange.Name);
            Assert.AreEqual(2, baseUriArrange.Depth);
            Assert.AreEqual(1, baseUriArrange.From);
            Assert.AreEqual(1, baseUriArrange.To);
            Assert.AreEqual(0, baseUriArrange.Children.Count);
        }
Exemplo n.º 15
0
        /// <summary>
        ///     We've received bytes from the socket. Build a message out of them.
        /// </summary>
        /// <param name="buffer">Buffer</param>
        /// <remarks></remarks>
        public void ProcessReadBytes(ISocketBuffer buffer)
        {
            int receiveBufferOffset      = buffer.Offset;
            int bytesLeftInReceiveBuffer = buffer.BytesTransferred;

            while (true)
            {
                if (bytesLeftInReceiveBuffer <= 0)
                {
                    break;
                }

                if (!_isHeaderParsed)
                {
                    var offsetBefore = receiveBufferOffset;
                    receiveBufferOffset = _headerParser.Parse(buffer, receiveBufferOffset);
                    if (!_isHeaderParsed)
                    {
                        return;
                    }

                    bytesLeftInReceiveBuffer -= receiveBufferOffset - offsetBefore;
                    _frameContentBytesLeft    = _frame.ContentLength;
                    if (_frameContentBytesLeft == 0)
                    {
                        // the NULL message delimiter
                        if (bytesLeftInReceiveBuffer == 1)
                        {
                            bytesLeftInReceiveBuffer = 0;
                        }

                        MessageReceived(_frame);
                        _frame          = null;
                        _isHeaderParsed = false;
                        continue;
                    }

                    _frame.Body = new MemoryStream();
                }

                var bytesRead    = BytesProcessed(buffer.Offset, receiveBufferOffset);
                var bytesToWrite = Math.Min(_frameContentBytesLeft, buffer.BytesTransferred - bytesRead);
                _frame.Body.Write(buffer.Buffer, receiveBufferOffset, bytesToWrite);
                _frameContentBytesLeft   -= bytesToWrite;
                receiveBufferOffset      += bytesToWrite;
                bytesLeftInReceiveBuffer -= bytesToWrite;
                bytesRead += bytesToWrite;
                if (_frameContentBytesLeft == 0)
                {
                    // ignore NULL (message delimiter)
                    //TODO: Maybe verify it? ;)
                    var bytesRemaining = buffer.BytesTransferred - bytesRead;
                    if (bytesRemaining == 1)
                    {
                        bytesLeftInReceiveBuffer--;
                        receiveBufferOffset++;
                    }


                    _frame.Body.Position = 0;
                    MessageReceived(_frame);
                    Clear();
                }
            }
        }
Exemplo n.º 16
0
        internal ParsedHeaderValue ParseValue()
        {
            var parser = new HeaderParser(Value);

            return(parser.Parse());
        }
Exemplo n.º 17
0
        public void Parse(ILineProvider lineProvider)
        {
            GedcomLine lastLine = default;

            var firstRawLine = lineProvider.ReadLine();

            if (firstRawLine == null)
            {
                throw new InvalidOperationException("File empty");
            }

            var firstLine = ParserHelper.ParseLine(firstRawLine);

            if (firstLine.Level != 0 && !ParserHelper.Equals(firstLine.GetFirstItem(), "HEAD"))
            {
                throw new InvalidOperationException("GEDCOM Header Not Found");
            }

            var gedcomHeaderParse = HeaderParser.Parse(firstLine, lineProvider);

            _headerCallback?.Invoke(gedcomHeaderParse.Result);

            var newLine = gedcomHeaderParse.Line;

            lastLine = newLine;

            while (newLine.LineContent.Length > 0)
            {
                var content = newLine.GetLineContent();
                if (content.Length == 0)
                {
                    var unrecognisedRawLine = lineProvider.ReadLine();
                    if (unrecognisedRawLine != null)
                    {
                        newLine  = ParserHelper.ParseLine(unrecognisedRawLine);
                        lastLine = newLine;
                    }
                    else
                    {
                        newLine = default;
                    }
                    continue;
                }

                var unknown = false;

                if (ParserHelper.Equals(content, "INDI"))
                {
                    var individualParseResult = IndividualParser.Parse(newLine, lineProvider);
                    _individualCallback?.Invoke(individualParseResult.Result);
                    newLine  = individualParseResult.Line;
                    lastLine = newLine;
                }
                else if (ParserHelper.Equals(content, "FAM"))
                {
                    var familyParseResult = FamilyParser.Parse(newLine, lineProvider);
                    _familyCallback?.Invoke(familyParseResult.Result);
                    newLine  = familyParseResult.Line;
                    lastLine = newLine;
                }
                else if (ParserHelper.Equals(content, "NOTE"))
                {
                    var noteParseResult = NoteParser.Parse(newLine, lineProvider);
                    _noteCallback?.Invoke(noteParseResult.Result);
                    newLine  = noteParseResult.Line;
                    lastLine = newLine;
                }
                else if (ParserHelper.Equals(content, "OBJE"))
                {
                    var objParserResult = ObjectParser.Parse(newLine, lineProvider);
                    _imageCallback?.Invoke(objParserResult.Result);
                    newLine  = objParserResult.Line;
                    lastLine = newLine;
                }
                else
                {
                    var unrecognisedRawLine = lineProvider.ReadLine();
                    if (unrecognisedRawLine != null)
                    {
                        newLine  = ParserHelper.ParseLine(unrecognisedRawLine);
                        lastLine = newLine;
                    }
                    else
                    {
                        newLine = default;
                    }
                    unknown = true;
                }

                if (unknown)
                {
                    continue;
                }
            }

            if (lastLine.LineContent.Length == 0)
            {
                throw new InvalidOperationException("file contains no content");
            }

            if (lastLine.Level != 0 || !ParserHelper.Equals(lastLine.GetFirstItem(), "TRLR"))
            {
                throw new InvalidOperationException("GEDCOM TRLR not found");
            }
        }
Exemplo n.º 18
0
 internal ParsedHeaderValue ParseValue()
 {
     var parser = new HeaderParser(Value);
     return parser.Parse();
 }