コード例 #1
0
        /// <summary>
        /// Get header values.
        /// </summary>
        /// <returns></returns>
        /// <remarks>Will also look for multi header values and automatically merge them to one line.</remarks>
        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());
            }

            var value = _reader.ReadLine();

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

            _headerValue += value;
            if (_headerName.Equals("Content-Length", StringComparison.OrdinalIgnoreCase))
            {
                if (!int.TryParse(value, out _bodyBytesLeft))
                {
                    throw new HttpException(HttpStatusCode.BadRequest, "Content length is not a number: " + value);
                }
            }

            OnHeader(_headerName, _headerValue);

            _headerName   = null;
            _headerValue  = string.Empty;
            _parserMethod = GetHeaderName;
            return(true);
        }
コード例 #2
0
        public void TestSingleNewLine()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello\nWorld!");
            var reader = new BufferSliceReader(buffer, 0, buffer.Length, Encoding.ASCII);

            var actual = reader.ReadLine();

            Assert.Equal("Hello", actual);
        }
コード例 #3
0
        public void ReadLineWithRN()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello\r\nWorld!");
            var reader = new BufferSliceReader(buffer, 0, buffer.Length, Encoding.ASCII);

            var actual  = reader.ReadLine();
            var actual2 = reader.ReadToEnd();

            Assert.Equal("Hello", actual);
            Assert.Equal("World!", actual2);
        }