コード例 #1
0
ファイル: HttpParser.cs プロジェクト: wingcd/utnt
        /// <summary>
        /// Try to find a header name.
        /// </summary>
        /// <returns></returns>
        private bool GetHeaderName()
        {
            // empty line. body is begining.
            if (_reader.Current == '\r' && _reader.Peek == '\n')
            {
                // Eat the line break
                _reader.Consume('\r', '\n');

                // Don't have a body?
                if (_bodyBytesLeft == 0)
                {
                    OnComplete();
                    _parserMethod = ParseFirstLine;
                }
                else
                {
                    _parserMethod = GetBody;
                }

                return(true);
            }

            _headerName = _reader.ReadUntil(':');
            if (_headerName == null)
            {
                return(false);
            }

            _reader.Consume();             // eat colon
            _parserMethod = GetHeaderValue;

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Try to find a header name.
        /// </summary>
        /// <returns></returns>
        private bool GetHeaderName()
        {
            // empty line. body is begining.
            if (_reader.Current == '\r' && _reader.Peek == '\n')
            {
                // Eat the line break
                _reader.Consume('\r', '\n');

                // Don't have a body?
                if (_bodyBytesLeft == 0)
                {
                    OnComplete();
                    _parserMethod = ParseFirstLine;
                }
                else
                    _parserMethod = GetBody;

                return true;
            }

            _headerName = _reader.ReadUntil(':');
            if (_headerName == null)
                return false;

            _reader.Consume(); // eat colon
            _parserMethod = GetHeaderValue;
            return true;
        }
コード例 #3
0
        private void Value_CompletedOrMultiLine(char ch)
        {
            if (IsHorizontalWhitespace(ch))
            {
                _headerValue.Append(' ');
                _parserMethod = Value_StripWhitespacesBefore;
                return;
            }
            if (ch == '\r')
            {
                return; //empty line
            }
            HeaderParsed(_headerName.ToString(), _headerValue.ToString());
            ResetLineParsing();
            _parserMethod = Name_StripWhiteSpacesBefore;

            if (ch == '\n')
            {
                //Header completed
                TriggerHeaderCompleted();
                return;
            }

            _lookAhead = ch;
        }
コード例 #4
0
        private bool GetHeaderName()
        {
            // empty line. body is begining.
            if (_reader.Current == '\r' && _reader.Peek == '\n')
            {
                // Eat the line break
                _reader.Consume('\r', '\n');

                // Don't have a body?
                if (_bodyBytesLeft == 0)
                {
                    _onBody(null);
                    _parserMethod = ParseFirstLine;
                    return(false);
                }

                _parserMethod = GetBody;
                return(true);
            }

            _headerName = _reader.ReadUntil(':');
            if (_headerName == null)
            {
                throw new ParseException(ExceptionMessage.InvalidHeaderName);
            }

            _reader.Consume(); // eat colon
            _parserMethod = GetHeaderValue;
            return(true);
        }
コード例 #5
0
ファイル: SipParser.cs プロジェクト: goupviet/Hallo
        public bool ParseFirstLine()
        {
            _reader.Consume('\r', '\n');

            if (!_reader.Contains('\n'))
            {
                throw new ParseException("Invalid firstline.");
            }

            var firstLine = _reader.ReadFoldedLine();

            if (firstLine.EndsWith(SipConstants.SipTwoZeroString))
            {
                SipRequestLine requestLine = new SipRequestLineParser().Parse(firstLine);
                _listener.OnRequest(requestLine);
            }
            else if (firstLine.StartsWith(SipConstants.SipTwoZeroString))
            {
                var message    = new SipResponse();
                var statusLine = new SipStatusLineParser().Parse(firstLine);
                _listener.OnResponse(statusLine);
            }
            else
            {
                throw new ParseException(ExceptionMessage.InvalidFirstLineFormat);
            }

            _parserMethod = GetHeaderName;

            return(true);
        }
コード例 #6
0
        /// <summary>
        /// Read cookie value qouted
        /// </summary>
        private bool Value_Qouted()
        {
            MoveNext(); // skip '"'

            var last = char.MinValue;

            while (Current != '"' && !IsEOF)
            {
                if (Current == '"' && last == '\\')
                {
                    _cookieValue += '#';
                    MoveNext();
                }
                else
                {
                    _cookieValue += Current;
                }

                last = Current;
                MoveNext();
            }

            _parserMethod = Value_After;
            return(true);
        }
コード例 #7
0
        private bool GetHeaderValue()
        {
            // remove white spaces.
            _reader.ConsumeWhiteSpaces();

            string value = _reader.ReadFoldedLine();

            if (value == null)
            {
                throw new ParseException(ExceptionMessage.InvalidFormat);
            }

            _headerValue += value;

            _headerValue = ProcessSingleLine(_headerValue);

            if (System.String.Compare(_headerName, SipHeaderNames.ContentLength, System.StringComparison.OrdinalIgnoreCase) == 0)
            {
                if (!int.TryParse(value, out _bodyBytesLeft))
                {
                    throw new ParseException("Content length is not a number.");
                }
            }

            _onHeader(_headerName, _headerValue);

            _headerName   = null;
            _headerValue  = string.Empty;
            _parserMethod = GetHeaderName;
            return(true);
        }
コード例 #8
0
ファイル: HttpParser.cs プロジェクト: wingcd/utnt
        /// <summary>
        /// Parses the first line in a request/response.
        /// </summary>
        /// <returns><c>true</c> if first line is well formatted; otherwise <c>false</c>.</returns>
        /// <exception cref="BadRequestException">Invalid request/response line.</exception>
        public bool ParseFirstLine()
        {
            _reader.Consume('\r', '\n');

            // Do not contain a complete first line.
            if (!_reader.Contains('\n'))
            {
                return(false);
            }

            var words = new string[3];

            words[0] = _reader.ReadUntil(' ');
            _reader.Consume();             // eat delimiter
            words[1] = _reader.ReadUntil(' ');
            _reader.Consume();             // eat delimiter
            words[2] = _reader.ReadLine();
            if (string.IsNullOrEmpty(words[0]) ||
                string.IsNullOrEmpty(words[1]) ||
                string.IsNullOrEmpty(words[2]))
            {
                throw new BadRequestException("Invalid request/response line.");
            }

            OnFirstLine(words);
            _parserMethod = GetHeaderName;

            return(true);
        }
コード例 #9
0
ファイル: HttpParser.cs プロジェクト: wingcd/utnt
 /// <summary>
 /// Reset parser to initial state.
 /// </summary>
 public void Reset()
 {
     _logger.Info("Resetting..");
     _headerValue   = null;
     _headerName    = string.Empty;
     _bodyBytesLeft = 0;
     _parserMethod  = ParseFirstLine;
 }
コード例 #10
0
ファイル: HttpParser.cs プロジェクト: wingcd/utnt
        /// <summary>
        /// Get header values.
        /// </summary>
        /// <returns></returns>
        /// <remarks>Will also look for multi header values and automatically merge them to one line.</remarks>
        /// <exception cref="ParserException">Content length is not a number.</exception>
        private bool GetHeaderValue()
        {
            // remove white spaces.
            _reader.Consume(' ', '\t');

            // multi line or empty value?
            if (_reader.Current == '\r' && _reader.Peek == '\n')
            {
                _reader.Consume('\r', '\n');

                // empty value.
                if (_reader.Current != '\t' && _reader.Current != ' ')
                {
                    OnHeader(_headerName, string.Empty);
                    _headerName   = null;
                    _headerValue  = string.Empty;
                    _parserMethod = GetHeaderName;
                    return(true);
                }

                if (_reader.RemainingLength < 1)
                {
                    return(false);
                }

                // consume one whitespace
                _reader.Consume();

                // and fetch the rest.
                return(GetHeaderValue());
            }

            string value = _reader.ReadLine();

            if (value == null)
            {
                return(false);
            }

            _headerValue += value;
            if (string.Compare(_headerName, "Content-Length", true) == 0)
            {
                if (!int.TryParse(value, out _bodyBytesLeft))
                {
                    throw new ParserException("Content length is not a number.");
                }
            }

            OnHeader(_headerName, value);

            _headerName   = null;
            _headerValue  = string.Empty;
            _parserMethod = GetHeaderName;

            return(true);
        }
コード例 #11
0
        private void Name_ParseUntilComma(char ch)
        {
            if (ch == ':')
            {
                _parserMethod = Value_StripWhitespacesBefore;
                return;
            }

            _headerName.Append(ch);
        }
コード例 #12
0
        private void Value_StripWhitespacesBefore(char ch)
        {
            if (IsHorizontalWhitespace(ch))
            {
                return;
            }

            _parserMethod = Value_ParseUntilQouteOrNewLine;
            _lookAhead    = ch;
        }
コード例 #13
0
        private void Name_StripWhiteSpacesBefore(char ch)
        {
            if (IsHorizontalWhitespace(ch))
            {
                return;
            }

            _parserMethod = Name_ParseUntilComma;
            _lookAhead    = ch;
        }
コード例 #14
0
        /// <summary>
        /// Remove all white spaces until colon is found
        /// </summary>
        protected virtual bool Name_After()
        {
            while (CharExtensions.IsWhiteSpace(Current) || Current == ':')
            {
                MoveNext();
            }

            _parserMethod = Value_Before;
            return(true);
        }
コード例 #15
0
        /// <summary>
        /// Parse state method, remove all white spaces before the cookie name
        /// </summary>
        protected bool Name_Before()
        {
            while (CharExtensions.IsWhiteSpace(Current))
            {
                MoveNext();
            }

            _parserMethod = Name;
            return(true);
        }
コード例 #16
0
        /// <summary>
        /// Read cookie name until white space or equals are found
        /// </summary>
        protected virtual bool Name()
        {
            while (!CharExtensions.IsWhiteSpace(Current) && Current != '=')
            {
                _cookieName += Current;
                MoveNext();
            }

            _parserMethod = Name_After;
            return(true);
        }
コード例 #17
0
        /// <summary>
        /// Read cookie value
        /// </summary>
        private bool Value()
        {
            while (Current != ';' && !IsEOF)
            {
                _cookieValue += Current;
                MoveNext();
            }

            _parserMethod = Value_After;
            return(true);
        }
コード例 #18
0
        private void Value_ParseQuoted(char ch)
        {
            if (ch == '"')
            {
                // exited the quouted string
                _parserMethod = Value_ParseUntilQouteOrNewLine;
                return;
            }

            _headerValue.Append(ch);
        }
コード例 #19
0
 /// <summary>
 /// Determine if the cookie value is quoted or regular.
 /// </summary>
 protected virtual bool Value_Before()
 {
     if (Current == '"')
     {
         _parserMethod = Value_Qouted;
     }
     else
     {
         _parserMethod = Value;
     }
     MoveNext();
     return(true);
 }
コード例 #20
0
        private bool Value_After()
        {
            OnCookie(_cookieName, _cookieValue);
            _cookieName  = "";
            _cookieValue = "";
            while (CharExtensions.IsWhiteSpace(Current) || Current == ';')
            {
                MoveNext();
            }

            _parserMethod = Name_Before;
            return(true);
        }
コード例 #21
0
    public static T[] ParseArray <T>(string arrayString, ParserMethod <T> parser)
    {
        string[] ss = arrayString.Split(';');

        T[] ts = new T[ss.Length];

        for (int i = 0; i < ss.Length; i++)
        {
            ts[i] = parser(ss[i].Trim());
        }

        return(ts);
    }
コード例 #22
0
        /// <summary>
        /// 解析请求或响应消息的第一行
        /// </summary>
        /// <returns><c>true</c> if first line is well formatted; otherwise <c>false</c>.</returns>
        /// <exception cref="BadRequestException">Invalid request/response line.</exception>
        private bool ParseFirstLine()
        {
            /* HTTP Message Example
             * GET /path/file.html HTTP/1.0
             * From: [email protected]
             * User-Agent: HTTPTool/1.0
             * [blank line here]
             *
             * HTTP/1.0 200 OK
             * Date: Fri, 31 Dec 1999 23:59:59 GMT
             * Content-Type: text/html
             * Content-Length: 1354
             *
             * <html>
             * <body>
             * <h1>Happy New Millennium!</h1>
             * (more file contents)
             * </body>
             * </html>
             */

            _reader.Consume('\r', '\n');

            // Do not contain a complete first line.
            if (!_reader.Contains('\n'))
            {
                return(false);
            }

            var words = new string[3];

            words[0] = _reader.ReadUntil(' ');
            _reader.Consume(); // eat delimiter
            words[1] = _reader.ReadUntil(' ');
            _reader.Consume(); // eat delimiter
            words[2] = _reader.ReadLine();
            if (string.IsNullOrEmpty(words[0]) ||
                string.IsNullOrEmpty(words[1]) ||
                string.IsNullOrEmpty(words[2]))
            {
                throw new BadRequestException("Invalid request/response line.");
            }

            // 成功找到消息的第一行
            OnFirstLine(words);

            // 指定下一步解析方式
            _parserMethod = GetHeaderName;

            return(true);
        }
コード例 #23
0
        /// <summary>
        /// Parse cookie string
        /// </summary>
        /// <returns>A generated cookie collection.</returns>
        public IHttpCookieCollection Parse(string value)
        {
            if (value == null) throw new ArgumentNullException("value");
            _headerValue = value;
            _cookies = new HttpCookieCollection();
            _parserMethod = Name_Before;

            while (!IsEOF)
            {
                _parserMethod();
            }

            OnCookie(_cookieName, _cookieValue);
            return (IHttpCookieCollection)_cookies;
        }
コード例 #24
0
ファイル: KPUtil.cs プロジェクト: ShinjiSakanami/KerbalParser
        public static T[] ParseArray <T>(string arrayString, ParserMethod <T> parser)
        {
            string[] array = arrayString.Split(new char[]
            {
                ';'
            });
            T[] array2 = new T[array.Length];
            int num    = array.Length;

            for (int i = 0; i < num; i++)
            {
                array2[i] = parser(array[i].Trim());
            }
            return(array2);
        }
コード例 #25
0
        internal void Lex(byte[] bytes, Action <string> onFirstLine, Action <string, string> onHeader, Action <byte[]> onBody)
        {
            _onBody       = onBody;
            _onHeader     = onHeader;
            _onFirstLine  = onFirstLine;
            _parserMethod = ParseFirstLine;

            _buffer = bytes;

            _logger.Debug("Lexing " + _buffer.Length + " bytes.");
            _reader.Assign(_buffer, 0, _buffer.Length);

            while (_parserMethod())
            {
                _logger.Trace("Switched lexer method to " + _parserMethod.Method.Name + " at index " + _reader.Index);
            }
        }
コード例 #26
0
ファイル: SipLexer.cs プロジェクト: HNeukermans/Hallo
        internal void Lex(byte[] bytes, Action<string> onFirstLine, Action<string, string> onHeader, Action<byte[]> onBody)
        {
            _onBody = onBody;
            _onHeader = onHeader;
            _onFirstLine = onFirstLine;
            _parserMethod = ParseFirstLine;

            _buffer = bytes;

            _logger.Debug("Lexing " + _buffer.Length + " bytes.");
            _reader.Assign(_buffer, 0, _buffer.Length);

            while (_parserMethod())
            {
                _logger.Trace("Switched lexer method to " + _parserMethod.Method.Name + " at index " + _reader.Index);
            }
        }
コード例 #27
0
        private bool ParseFirstLine()
        {
            _reader.Consume('\r', '\n');

            if (!_reader.Contains('\n'))
            {
                throw new ParseException("Invalid firstline.");
            }

            var firstLine = _reader.ReadFoldedLine();

            _onFirstLine(firstLine);

            _parserMethod = GetHeaderName;

            return(true);
        }
コード例 #28
0
        private T Parse <T>(string input, ParserMethod <T> parser)
            where T : NativeXNode
        {
            List <CodeToken> tokens = NativeXCodeParser.Tokenize(input.ToCharArray());
            int  currentToken       = 0;
            bool parseSuccess       = false;
            T    result             = parser(tokens, ref currentToken, ref parseSuccess);

            Assert.AreEqual(tokens.Count, currentToken);
            Assert.AreEqual(tokens[0].Start, result.Start);
            Assert.AreEqual(tokens[tokens.Count - 1].End, result.End);
            if (result != null)
            {
                result.BuildScope(null);
            }
            return(result);
        }
コード例 #29
0
 protected void Declare(XmlNodeType nodeType, ParserMethod parserMethod)
 {
     if (_nodeTypeMap.ContainsKey(nodeType))
     {
         _nodeTypeMap[nodeType].Add(parserMethod);
     }
     else
     {
         _nodeTypeMap[nodeType] = new List <ParserMethod>()
         {
             parserMethod
         };
     }
     if (nodeType == XmlNodeType.Attribute)
     {
         _doAttributes = true;
     }
 }
コード例 #30
0
        /// <summary>
        /// Parse cookie string
        /// </summary>
        /// <returns>A generated cookie collection.</returns>
        public IHttpCookieCollection Parse(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            _headerValue  = value;
            _cookies      = new HttpCookieCollection();
            _parserMethod = Name_Before;

            while (!IsEOF)
            {
                _parserMethod();
            }

            OnCookie(_cookieName, _cookieValue);
            return((IHttpCookieCollection)_cookies);
        }
コード例 #31
0
        private void Value_ParseUntilQouteOrNewLine(char ch)
        {
            if (ch == '"')
            {
                _parserMethod = Value_ParseQuoted;
                return;
            }

            if (ch == '\r')
            {
                return;
            }
            if (ch == '\n')
            {
                _parserMethod = Value_CompletedOrMultiLine;
                return;
            }

            _headerValue.Append(ch);
        }
コード例 #32
0
        private bool ParseRequestLine()
        {
            _reader.Consume('\r', '\n');

            // Do not contain a complete first line.
            if (!_reader.Contains('\n'))
                return false;

            var words = new string[3];
            words[0] = _reader.ReadUntil(' ');
            _reader.Consume(); // eat delimiter
            words[1] = _reader.ReadUntil(' ');
            _reader.Consume(); // eat delimiter
            words[2] = _reader.ReadLine();
            if (string.IsNullOrEmpty(words[0])
                || string.IsNullOrEmpty(words[1])
                || string.IsNullOrEmpty(words[2]))
                throw new BadRequestException("Invalid request/response line.");
            
            _reqLineRecieved(words);
            _parserMethod = GetHeaderName;
            return true;
        }
コード例 #33
0
        private void FirstLine(char ch)
        {
            //ignore empty lines before the request line
            if (ch == '\r' || ch == '\n' && _headerName.Length == 0)
            {
                return;
            }

            if (ch == '\r')
            {
                return;
            }
            if (ch == '\n')
            {
                var line = _headerName.ToString().Split(new char[] { ' ' }, 3);
                RequestLineParsed(line[0], line[1], line[2]);

                _headerName.Clear();
                _parserMethod = Name_StripWhiteSpacesBefore;
                return;
            }

            _headerName.Append(ch);
        }
コード例 #34
0
ファイル: SipParser.cs プロジェクト: HNeukermans/Hallo
 public SipParser(ISipParserListener listener)
 {
     _listener = listener;
     _parserMethod = ParseFirstLine;
 }
コード例 #35
0
        private void Name_ParseUntilComma(char ch)
        {
            if (ch == ':')
            {
                _parserMethod = Value_StripWhitespacesBefore;
                return;
            }

            _headerName.Append(ch);
        }
コード例 #36
0
        /// <summary>
        /// Parse state method, remove all white spaces before the cookie name
        /// </summary>
        protected bool Name_Before()
        {
            while (CharExtensions.IsWhiteSpace(Current))
            {
                MoveNext();
            }

            _parserMethod = Name;
            return true;
        }
コード例 #37
0
ファイル: HttpParser.cs プロジェクト: wingcd/utnt
 /// <summary>
 /// Initializes a new instance of the <see cref="HttpParser"/> class.
 /// </summary>
 public HttpParser()
 {
     _parserMethod = ParseFirstLine;
 }
コード例 #38
0
        private bool Value_After()
        {
            OnCookie(_cookieName, _cookieValue);
            _cookieName = "";
            _cookieValue = "";
            while (CharExtensions.IsWhiteSpace(Current) || Current == ';')
            {
                MoveNext();
            }

            _parserMethod = Name_Before;
            return true;
        }
コード例 #39
0
        /// <summary>
        /// Read cookie name until white space or equals are found
        /// </summary>
        protected virtual bool Name()
        {
            while (!CharExtensions.IsWhiteSpace(Current) && Current != '=')
            {
                _cookieName += Current;
                MoveNext();
            }

            _parserMethod = Name_After;
            return true;
        }
コード例 #40
0
ファイル: SipLexer.cs プロジェクト: HNeukermans/Hallo
        private bool GetHeaderValue()
        {
            // remove white spaces.
            _reader.ConsumeWhiteSpaces();

            string value = _reader.ReadFoldedLine();
            if (value == null)
                throw new ParseException(ExceptionMessage.InvalidFormat);

            _headerValue += value;

            _headerValue = ProcessSingleLine(_headerValue);

            if (System.String.Compare(_headerName, SipHeaderNames.ContentLength, System.StringComparison.OrdinalIgnoreCase) == 0)
            {
                if (!int.TryParse(value, out _bodyBytesLeft))
                    throw new ParseException("Content length is not a number.");
            }

            _onHeader(_headerName, _headerValue);

            _headerName = null;
            _headerValue = string.Empty;
            _parserMethod = GetHeaderName;
            return true;
        }
コード例 #41
0
 /// <summary>
 /// Reset parser state
 /// </summary>
 public void Reset()
 {
     ResetLineParsing();
     _parserMethod = FirstLine;
 }
コード例 #42
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HeaderParser" /> class.
 /// </summary>
 public HeaderParser()
 {
     _parserMethod = FirstLine;
 }
コード例 #43
0
        private void Value_StripWhitespacesBefore(char ch)
        {
            if (IsHorizontalWhitespace(ch))
                return;

            _parserMethod = Value_ParseUntilQouteOrNewLine;
            _lookAhead = ch;
        }
コード例 #44
0
        private void Value_ParseUntilQouteOrNewLine(char ch)
        {
            if (ch == '"')
            {
                _parserMethod = Value_ParseQuoted;
                return;
            }

            if (ch == '\r')
                return;
            if (ch == '\n')
            {
                _parserMethod = Value_CompletedOrMultiLine;
                return;
            }

            _headerValue.Append(ch);
        }
コード例 #45
0
        private void Value_ParseQuoted(char ch)
        {
            if (ch == '"')
            {
                // exited the quouted string
                _parserMethod = Value_ParseUntilQouteOrNewLine;
                return;
            }

            _headerValue.Append(ch);
        }
コード例 #46
0
        private void Value_CompletedOrMultiLine(char ch)
        {
            if (IsHorizontalWhitespace(ch))
            {
                _headerValue.Append(' ');
                _parserMethod = Value_StripWhitespacesBefore;
                return;
            }
            if (ch == '\r')
                return; //empty line

            HeaderParsed(_headerName.ToString(), _headerValue.ToString());
            ResetLineParsing();
            _parserMethod = Name_StripWhiteSpacesBefore;

            if (ch == '\n')
            {
                //Header completed
                TriggerHeaderCompleted();
                return;
            }

            _lookAhead = ch;
        }
コード例 #47
0
        private void Name_StripWhiteSpacesBefore(char ch)
        {
            if (IsHorizontalWhitespace(ch))
                return;

            _parserMethod = Name_ParseUntilComma;
            _lookAhead = ch;
        }
コード例 #48
0
 /// <summary>
 /// Reset parser to initial state.
 /// </summary>
 public void Reset()
 {
     _logger.Info("Resetting..");
     _headerValue = null;
     _headerName = string.Empty;
     _bodyBytesLeft = 0;
     _parserMethod = ParseFirstLine;
 }
コード例 #49
0
        /// <summary>
        /// Remove all white spaces until colon is found
        /// </summary>
        protected virtual bool Name_After()
        {
            while (CharExtensions.IsWhiteSpace(Current) || Current == ':')
            {
                MoveNext();
            }

            _parserMethod = Value_Before;
            return true;
        }
コード例 #50
0
        /// <summary>
        /// Get header values.
        /// </summary>
        /// <returns></returns>
        /// <remarks>Will also look for multi header values and automatically merge them to one line.</remarks>
        /// <exception cref="ParserException">Content length is not a number.</exception>
        private bool GetHeaderValue()
        {
            // remove white spaces.
            _reader.Consume(' ', '\t');

            // multi line or empty value?
            if (_reader.Current == '\r' && _reader.Peek == '\n')
            {
                _reader.Consume('\r', '\n');

                // empty value.
                if (_reader.Current != '\t' && _reader.Current != ' ')
                {
                    OnHeader(_headerName, string.Empty);
                    _headerName = null;
                    _headerValue = string.Empty;
                    _parserMethod = GetHeaderName;
                    return true;
                }

                if (_reader.RemainingLength < 1)
                    return false;

                // consume one whitespace
                _reader.Consume();

                // and fetch the rest.
                return GetHeaderValue();
            }

            string value = _reader.ReadLine();
            if (value == null)
                return false;

            _headerValue += value;
            if (string.Compare(_headerName, "Content-Length", true) == 0)
            {
                if (!int.TryParse(value, out _bodyBytesLeft))
                    throw new ParserException("Content length is not a number.");
            }

            OnHeader(_headerName, value);
            
            _headerName = null;
            _headerValue = string.Empty;
            _parserMethod = GetHeaderName;
            return true;
        }
コード例 #51
0
ファイル: SipLexer.cs プロジェクト: HNeukermans/Hallo
        private bool ParseFirstLine()
        {
            _reader.Consume('\r', '\n');

            if (!_reader.Contains('\n'))
                throw new ParseException("Invalid firstline.");

            var firstLine = _reader.ReadFoldedLine();
            _onFirstLine(firstLine);

            _parserMethod = GetHeaderName;

            return true;
        }
コード例 #52
0
ファイル: MessageParser.cs プロジェクト: sclcwwl/Gimela
 /// <summary>
 /// HTTP消息解析器,用于解析请求或响应消息
 /// </summary>
 public MessageParser()
 {
   _parserMethod = ParseFirstLine;
 }
コード例 #53
0
        /// <summary>
        /// Read cookie value qouted
        /// </summary>
        private bool Value_Qouted()
        {
            MoveNext(); // skip '"'

            var last = char.MinValue;
            while (Current != '"' && !IsEOF)
            {
                if (Current == '"' && last == '\\')
                {
                    _cookieValue += '#';
                    MoveNext();
                }
                else
                {
                    _cookieValue += Current;
                }

                last = Current;
                MoveNext();

            }

            _parserMethod = Value_After;
            return true;
        }
コード例 #54
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HttpParser"/> class.
 /// </summary>
 public HttpParser()
 {
     _parserMethod = ParseFirstLine;
 }
コード例 #55
0
        /// <summary>
        /// Read cookie value
        /// </summary>
        private bool Value()
        {
            while (Current != ';' && !IsEOF)
            {
                _cookieValue += Current;
                MoveNext();
            }

            _parserMethod = Value_After;
            return true;
        }
コード例 #56
0
ファイル: SipParser.cs プロジェクト: jgauffin/SipSharp
 public void Reset()
 {
     _parserMethod = ParseFirstLine;
 }
コード例 #57
0
        /// <summary>
        /// Determine if the cookie value is quoted or regular.
        /// </summary>
        protected virtual bool Value_Before()
        {
            if (Current == '"')
            {
                _parserMethod = Value_Qouted;
            }
            else
            {
                _parserMethod = Value;

            }
            MoveNext();
            return true;
        }
コード例 #58
0
ファイル: SipParser.cs プロジェクト: HNeukermans/Hallo
        public bool ParseFirstLine()
        {
            _reader.Consume('\r', '\n');

            if (!_reader.Contains('\n'))
                throw new ParseException("Invalid firstline.");

            var firstLine = _reader.ReadFoldedLine();

            if (firstLine.EndsWith(SipConstants.SipTwoZeroString))
            {
                SipRequestLine requestLine = new SipRequestLineParser().Parse(firstLine);
                _listener.OnRequest(requestLine);
            }
            else if (firstLine.StartsWith(SipConstants.SipTwoZeroString))
            {
                var message = new SipResponse();
                var statusLine = new SipStatusLineParser().Parse(firstLine);
                _listener.OnResponse(statusLine);
            }
            else
            {
                throw new ParseException(ExceptionMessage.InvalidFirstLineFormat);
            }

            _parserMethod = GetHeaderName;

            return true;
        }
コード例 #59
0
ファイル: SipLexer.cs プロジェクト: HNeukermans/Hallo
        private bool GetHeaderName()
        {
            // empty line. body is begining.
            if (_reader.Current == '\r' && _reader.Peek == '\n')
            {
                // Eat the line break
                _reader.Consume('\r', '\n');

                // Don't have a body?
                if (_bodyBytesLeft == 0)
                {
                    _onBody(null);
                    _parserMethod = ParseFirstLine;
                    return false;
                }

                _parserMethod = GetBody;
                return true;
            }

            _headerName = _reader.ReadUntil(':');
            if (_headerName == null)
                throw new ParseException(ExceptionMessage.InvalidHeaderName);

            _reader.Consume(); // eat colon
            _parserMethod = GetHeaderValue;
            return true;
        }
コード例 #60
0
        private void FirstLine(char ch)
        {
            //ignore empty lines before the request line
            if (ch == '\r' || ch == '\n' && _headerName.Length == 0)
                return;

            if (ch == '\r')
                return;
            if (ch == '\n')
            {
                var line = _headerName.ToString().Split(new char[] { ' ' }, 3);
                RequestLineParsed(line[0], line[1], line[2]);

                _headerName.Clear();
                _parserMethod = Name_StripWhiteSpacesBefore;
                return;
            }

            _headerName.Append(ch);
        }