예제 #1
0
        public static async Task <TCPRequest> Parse(TCPStream input, bool includeHeaders)
        {
            var r = new TCPRequest();
            await r.ParseCore(input, includeHeaders).ConfigureAwait(false);

            return(r);
        }
예제 #2
0
        private async Task ParseCore(TCPStream stream, bool includeHeaders)
        {
            Method = await stream.ReadStringAync().ConfigureAwait(false);

            var requestUri = await stream.ReadStringAync().ConfigureAwait(false);

            var uri = new Uri(requestUri);

            Scheme      = uri.Scheme;
            IsHttps     = false;
            Host        = new HostString(uri.Host, uri.Port);
            PathBase    = new PathString(uri.AbsolutePath);
            Path        = new PathString(uri.AbsolutePath);
            QueryString = new QueryString(uri.Query);
            Query       = new QueryCollection(Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query));
            Protocol    = "http";

            if (includeHeaders)
            {
                var headers      = new List <Tuple <string, string> >();
                var headersCount = await stream.ReadVarIntAsync().ConfigureAwait(false);

                for (int i = 0; i < (int)headersCount; i++)
                {
                    var key = await stream.ReadStringAync().ConfigureAwait(false);

                    var value = await stream.ReadStringAync().ConfigureAwait(false);

                    headers.Add(Tuple.Create(key, value));
                }

                foreach (var h in headers.GroupBy(g => g.Item1, g => g.Item2))
                {
                    Headers.Add(h.Key, new StringValues(h.ToArray()));
                }
            }

            var hasContent = (await stream.ReadVarIntAsync().ConfigureAwait(false)) == 1;

            if (hasContent)
            {
                var buffer = await stream.ReadBytesAync(TCPStream.ReadType.ManagedPool).ConfigureAwait(false);

                Body = new MemoryStream(buffer.Array);
                Body.SetLength(buffer.Count);
                ContentLength = buffer.Count;
            }
        }