Beispiel #1
0
        public async static Task <StringContent> Read(TestContext ctx, HttpStreamReader reader, int length, CancellationToken cancellationToken)
        {
            var buffer = new char [length];
            int offset = 0;

            while (offset < length)
            {
                cancellationToken.ThrowIfCancellationRequested();
                var len = Math.Min(16384, length - offset);
                var ret = await reader.ReadAsync(buffer, offset, len, cancellationToken);

                if (ret <= 0)
                {
                    if (offset == 0)
                    {
                        throw new IOException("Client didn't send any body.");
                    }
                    throw new InvalidOperationException();
                }

                offset += ret;
            }

            return(new StringContent(new string (buffer)));
        }
Beispiel #2
0
        public static async Task <byte[]> ReadChunk(TestContext ctx, HttpStreamReader reader, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var header = await reader.ReadLineAsync(cancellationToken);

            var length = int.Parse(header, NumberStyles.HexNumber);

            if (length == 0)
            {
                return(null);
            }

            cancellationToken.ThrowIfCancellationRequested();

            var buffer = new byte[length];
            int pos    = 0;

            while (pos < length)
            {
                var ret = await reader.ReadAsync(buffer, pos, length - pos, cancellationToken);

                if (ret < 0)
                {
                    throw new IOException();
                }
                if (ret == 0)
                {
                    break;
                }
                pos += ret;
            }

            ctx.Assert(pos, Is.EqualTo(length), "read entire chunk");

            cancellationToken.ThrowIfCancellationRequested();

            var empty = await reader.ReadLineAsync(cancellationToken);

            if (!string.IsNullOrEmpty(empty))
            {
                throw new InvalidOperationException();
            }

            return(buffer);
        }
Beispiel #3
0
        internal static async Task <HttpContent> Read(HttpStreamReader reader, int length, CancellationToken cancellationToken)
        {
            var buffer = new byte[length];

            int offset = 0;

            while (offset < length)
            {
                cancellationToken.ThrowIfCancellationRequested();
                var len = Math.Min(16384, length - offset);
                var ret = await reader.ReadAsync(buffer, offset, len, cancellationToken);

                if (ret <= 0)
                {
                    throw new InvalidOperationException();
                }

                offset += ret;
            }

            return(new BinaryContent(buffer));
        }
Beispiel #4
0
        public static async Task <ChunkedContent> Read(HttpStreamReader reader, CancellationToken cancellationToken)
        {
            var chunks = new List <string> ();

            do
            {
                cancellationToken.ThrowIfCancellationRequested();
                var header = await reader.ReadLineAsync(cancellationToken);

                var length = int.Parse(header, NumberStyles.HexNumber);
                if (length == 0)
                {
                    break;
                }

                cancellationToken.ThrowIfCancellationRequested();

                var buffer = new char [length];
                var ret    = await reader.ReadAsync(buffer, 0, length, cancellationToken);

                if (ret != length)
                {
                    throw new InvalidOperationException();
                }

                chunks.Add(new string (buffer));

                cancellationToken.ThrowIfCancellationRequested();

                var empty = await reader.ReadLineAsync(cancellationToken);

                if (!string.IsNullOrEmpty(empty))
                {
                    throw new InvalidOperationException();
                }
            } while (true);

            return(new ChunkedContent(chunks));
        }
Beispiel #5
0
        internal static async Task <HttpContent> ReadAll(HttpStreamReader reader, CancellationToken cancellationToken)
        {
            using (var ms = new MemoryStream()) {
                var buffer = new byte[16384];
                while (true)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    var ret = await reader.ReadAsync(
                        buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);

                    if (ret == 0)
                    {
                        break;
                    }
                    if (ret < 0)
                    {
                        throw new InvalidOperationException();
                    }
                    ms.Write(buffer, 0, ret);
                }

                return(new BinaryContent(ms.ToArray()));
            }
        }