Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }
Beispiel #5
0
 public void ReadH_ContainsH_ExpectItContainsH()
 {
     CreateBufferReader("H");
     var contains = _reader.Contains('H').Should().BeTrue();
 }