private static async Task<byte[]> GetPostData(HttpStreamReader streamReader, IHttpHeaders headers)
 {
     int postContentLength;
     byte[] post;
     if (headers.TryGetByName("content-length", out postContentLength))
     {
         byte[] buffer = new byte[postContentLength];
         var readBytes = await streamReader.BaseStream.ReadAsync(buffer, 0, postContentLength);
         post = buffer;
     }
     else
     {
         post = null;
     }
     return post;
 }
        public async Task<IHttpRequest> Provide(HttpStreamReader streamReader)
        {
            // parse the http request
            var request = await streamReader.ReadLineAsync().ConfigureAwait(false);

            if (request == null)
                return null;

            var tokens = request.Split(' ');

            if (tokens.Length != 3)
            {
                return null;
            }

            var httpMethod = HttpMethodProvider.Default.Provide(tokens[0]);
            var httpProtocol = tokens[2];

            var url = tokens[1];
            var queryString = GetQueryStringData(ref url);
            var uri = new Uri(url, UriKind.Relative);

            var headersRaw = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

            // get the headers
            string line;
            while (!string.IsNullOrEmpty((line = await streamReader.ReadLineAsync().ConfigureAwait(false))))
            { 
                var headerKvp = SplitHeader(line);
                headersRaw.Add(headerKvp.Key, headerKvp.Value);
            }

            IHttpHeaders headers = new HttpHeaders(headersRaw);
            byte[] post = await GetPostData(streamReader, headers);

            return new HttpRequest(headers, httpMethod, httpProtocol, uri,
                uri.OriginalString.Split(Separators, StringSplitOptions.RemoveEmptyEntries), queryString, post);
        }