Exemple #1
0
        public static HttpRequestLine 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();
            }

            // method
            var indexOfSpaceAfterMethod = buffer.IndexOf(SPACE_BYTE, start);

            if (indexOfSpaceAfterMethod == -1)
            {
                throw new BadHttpDataException();
            }

            var length = indexOfSpaceAfterMethod - start;
            var method = buffer.ToAsciiString(start, length);

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

            // uri
            var indexOfSpaceAfterUri = buffer.IndexOf(SPACE_BYTE, start);

            length = indexOfSpaceAfterUri - start;
            var uri = buffer.ToAsciiString(start, length);

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

            // http version
            length = crLfIndex - start;
            var version = buffer.ToAsciiString(start, length);

            try
            {
                var line = new HttpRequestLine(new HttpMethod(method), uri, version);
                return(line);
            }
            catch (ArgumentException ex)
            {
                throw new BadHttpDataException("Could not parse request line", ex);
            }
            catch (FormatException ex)
            {
                throw new BadHttpDataException("Could not parse request line", ex);
            }
        }
Exemple #2
0
        public static HttpRequestMetadata Parse(byte[] buffer, int start)
        {
            // parse line
            var line = HttpRequestLine.Parse(buffer, start);

            start += line.ByteCount;

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

            var metadata = new HttpRequestMetadata(line, headers);

            return(metadata);
        }
Exemple #3
0
 public HttpRequestMetadata(HttpRequestLine line, HttpHeaderCollection headers)
 {
     this.Line    = line ?? throw new ArgumentNullException(nameof(line));
     this.Headers = headers ?? throw new ArgumentNullException(nameof(headers));
 }