Esempio n. 1
0
        public static HttpResponseMetadata Parse(byte[] buffer, int start)
        {
            // parse status line
            var line = HttpStatusLine.Parse(buffer, start);

            start += line.ByteCount;

            // parse headers
            var headers = HttpHeaderCollection.Parse(buffer, start);

            var metadata = new HttpResponseMetadata(line, headers);

            return(metadata);
        }
Esempio n. 2
0
        public static HttpStatusLine Parse(byte[] buffer, int start)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            if (start < 0 || start >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(start));
            }

            var crLfIndex = buffer.IndexOfSubarray(CoreHelper.CrLfBytes, start, -1);

            if (crLfIndex == -1)
            {
                throw new BadHttpDataException("Could not parse status line");
            }

            // version
            var indexOfSpaceAfterVersion = buffer.IndexOf(SPACE_BYTE, start);

            if (indexOfSpaceAfterVersion == -1)
            {
                throw new BadHttpDataException("Could not parse status line");
            }

            var length = indexOfSpaceAfterVersion - start;

            if (length == 0)
            {
                // version length is 0
                throw new BadHttpDataException("Could not parse status line"); // todo1[ak] a lot of copy/paste
            }

            var version = buffer.ToAsciiString(start, length);

            // advance
            start = indexOfSpaceAfterVersion + 1; // skip ' '

            // code
            var indexOfSpaceAfterCode = buffer.IndexOf(SPACE_BYTE, start);

            length = indexOfSpaceAfterCode - start;
            if (length == 0)
            {
                // code length is 0
                throw new BadHttpDataException("Could not parse status line");
            }

            var codeNumber = buffer.ToAsciiString(start, length).ToInt32();
            var code       = (HttpStatusCode)codeNumber;

            // advance
            start = indexOfSpaceAfterCode + 1; // skip ' '

            // reason
            length = crLfIndex - start;
            if (length == 0)
            {
                // reason length is 0
                throw new BadHttpDataException("Could not parse status line");
            }

            var reason = buffer.ToAsciiString(start, length);

            if (reason[0] == ' ')
            {
                // reason must not start with space
                throw new BadHttpDataException("Could not parse status line");
            }

            try
            {
                var line = new HttpStatusLine(version, code, reason);
                return(line);
            }
            catch (ArgumentException ex)
            {
                throw new BadHttpDataException("Could not parse request line", ex);
            }
        }
Esempio n. 3
0
 public HttpResponseMetadata(HttpStatusLine line, HttpHeaderCollection headers)
 {
     this.Line    = line ?? throw new ArgumentNullException(nameof(line));
     this.Headers = headers ?? throw new ArgumentNullException(nameof(headers));
 }