Пример #1
0
        private void ParseStatusLine(HttpResponseMessage response, Span <byte> line)
        {
            const int MinStatusLineLength = 12; // "HTTP/1.x 123"

            if (line.Length < MinStatusLineLength || line[8] != ' ')
            {
                throw new HttpRequestException("Invalid response");
            }

            ulong first8Bytes = BinaryPrimitives.ReadUInt64LittleEndian(line);

            if (first8Bytes != s_http10Bytes)
            {
                throw new HttpRequestException("Invalid response");
            }
            response.Version = HttpVersion.Version10;
            // Set the status code
            byte status1 = line[9], status2 = line[10], status3 = line[11];

            if (!HttpParser.IsDigit(status1) || !HttpParser.IsDigit(status2) || !HttpParser.IsDigit(status3))
            {
                throw new HttpRequestException("Invalid response");
            }

            response.StatusCode = (HttpStatusCode)(100 * (status1 - '0') + 10 * (status2 - '0') + (status3 - '0'));
            // Parse (optional) reason phrase
            if (line.Length == MinStatusLineLength)
            {
                response.ReasonPhrase = string.Empty;
            }
            else if (line[MinStatusLineLength] == ' ')
            {
                Span <byte> reasonBytes = line.Slice(MinStatusLineLength + 1);
                try
                {
                    response.ReasonPhrase = HttpParser.GetAsciiString(reasonBytes);
                }
                catch (FormatException error)
                {
                    throw new HttpRequestException("Invalid response", error);
                }
            }
            else
            {
                throw new HttpRequestException("Invalid response");
            }
        }