internal async Task <Stream> GetBody(RequestBuffer buffer)
        {
            var body = (Length > Configuration.RequestMemoryLimit) ? TemporaryFileStream.Create() : new MemoryStream((int)Length);

            var toFetch = Length;

            while (toFetch > 0)
            {
                await buffer.Read().ConfigureAwait(false);

                var toRead = Math.Min(buffer.Data.Length, Math.Min(Configuration.TransferBufferSize, toFetch));

                if (toRead == 0)
                {
                    throw new InvalidOperationException($"No data read from the transport but {toFetch} bytes are remaining");
                }

                var data = buffer.Data.Slice(0, toRead);

                var position = data.GetPosition(0);

                while (data.TryGet(ref position, out var memory))
                {
                    await body.WriteAsync(memory).ConfigureAwait(false);
                }

                buffer.Advance(toRead);

                toFetch -= toRead;
            }

            body.Seek(0, SeekOrigin.Begin);

            return(body);
        }
Exemple #2
0
        internal async ValueTask <RequestBuilder?> TryParseAsync(RequestBuffer buffer)
        {
            WebPath?     path;
            RequestQuery?query;

            string?method, protocol;

            if ((method = await ReadToken(buffer, ' ').ConfigureAwait(false)) != null)
            {
                Request.Type(method);
            }
            else
            {
                return(null);
            }

            if ((path = await TryReadPath(buffer).ConfigureAwait(false)) != null)
            {
                Request.Path(path);
            }
            else
            {
                return(null);
            }

            if ((query = await TryReadQuery(buffer).ConfigureAwait(false)) != null)
            {
                Request.Query(query);
            }

            if ((protocol = await ReadToken(buffer, '\r', 1, 5).ConfigureAwait(false)) != null)
            {
                Request.Protocol(protocol);
            }
            else
            {
                return(null);
            }

            while (await TryReadHeader(buffer, Request).ConfigureAwait(false)) /* nop */ } {