Exemplo n.º 1
0
        public bool ReadHeader(out string headerName, out string headerValue)
        {
            int index = 0;

            while (index < _span.Length && ValidHeaderNameChar(_span[index]))
            {
                index++;
            }

            if (index > 0)
            {
                CheckResponseMsgFormat(index < _span.Length);
                CheckResponseMsgFormat(_span[index] == ':');
                HeaderBufferSpan headerNameSpan = _span.Substring(0, index);
                if (!HttpKnownHeaderNames.TryGetHeaderName(_span.Buffer, _span.Length, out headerName))
                {
                    headerName = headerNameSpan.ToString();
                }
                CheckResponseMsgFormat(headerName.Length > 0);

                index++;
                headerValue = _span.Substring(index).Trim().ToString();
                return(true);
            }

            headerName  = null;
            headerValue = null;
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads a header line.
        /// Empty header lines are skipped, as are malformed header lines that are missing a colon character.
        /// </summary>
        /// <returns>true if the next header was read successfully, or false if all characters have been read.</returns>
        public bool ReadHeader([NotNullWhen(true)] out string?name, [NotNullWhen(true)] out string?value)
        {
            int startIndex;
            int length;

            while (ReadLine(out startIndex, out length))
            {
                // Skip empty lines.
                if (length == 0)
                {
                    continue;
                }

                int colonIndex = Array.IndexOf(_buffer, ':', startIndex, length);

                // Skip malformed header lines that are missing the colon character.
                if (colonIndex == -1)
                {
                    continue;
                }

                int nameLength = colonIndex - startIndex;

                // If it's a known header name, use the known name instead of allocating a new string.
                if (!HttpKnownHeaderNames.TryGetHeaderName(_buffer, startIndex, nameLength, out name))
                {
                    name = new string(_buffer, startIndex, nameLength);
                }

                // Normalize header value by trimming whitespace.
                int valueStartIndex = colonIndex + 1;
                int valueLength     = startIndex + length - colonIndex - 1;
                CharArrayHelpers.Trim(_buffer, ref valueStartIndex, ref valueLength);

                value = HttpKnownHeaderNames.GetHeaderValue(name, _buffer, valueStartIndex, valueLength);

                return(true);
            }

            name  = null;
            value = null;
            return(false);
        }
Exemplo n.º 3
0
        public bool ReadHeader(out string headerName, out string headerValue)
        {
            int index = 0;

            while (index < _span.Length && ValidHeaderNameChar(_span[index]))
            {
                index++;
            }

            if (index > 0)
            {
                // For compatability, skip past any whitespace before the colon, even though
                // the RFC suggests there shouldn't be any.
                int headerNameLength = index;
                while (index < _span.Length && IsWhiteSpaceLatin1(_span[index]))
                {
                    index++;
                }

                CheckResponseMsgFormat(index < _span.Length);
                CheckResponseMsgFormat(_span[index] == ':');
                HeaderBufferSpan headerNameSpan = _span.Substring(0, headerNameLength);
                if (!HttpKnownHeaderNames.TryGetHeaderName(_span.Buffer, _span.Length, out headerName))
                {
                    headerName = headerNameSpan.ToString();
                }
                CheckResponseMsgFormat(headerName.Length > 0);

                index++;
                headerValue = _span.Substring(index).Trim().ToString();
                return(true);
            }

            headerName  = null;
            headerValue = null;
            return(false);
        }