Esempio n. 1
0
        async Task InternalRead(HttpStreamReader reader, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var header = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);

            if (header == null)
            {
                throw new IOException("Connection has been closed.");
            }

            var fields = header.Split(new char[] { ' ' }, StringSplitOptions.None);

            if (fields.Length != 3)
            {
                throw new InvalidOperationException();
            }

            Method   = fields [0];
            Protocol = ProtocolFromString(fields [2]);
            if (Method.Equals("CONNECT"))
            {
                Path = fields [1];
            }
            else
            {
                Path = fields [1].StartsWith("/", StringComparison.Ordinal) ? fields [1] : new Uri(fields [1]).AbsolutePath;
            }

            cancellationToken.ThrowIfCancellationRequested();
            await ReadHeaders(reader, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();
            Body = await ReadBody(reader, cancellationToken);
        }
Esempio n. 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);
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        public static async Task <ChunkedContent> ReadNonChunked(HttpStreamReader reader, CancellationToken cancellationToken)
        {
            var chunks = new List <string> ();

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);

                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                chunks.Add(line);
            }

            return(new ChunkedContent(chunks));
        }
Esempio n. 5
0
        async Task InternalRead(TestContext ctx, HttpStreamReader reader, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var header = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);

            var fields = header.Split(new char[] { ' ' }, StringSplitOptions.None);

            if (fields.Length < 2 || fields.Length > 3)
            {
                throw new InvalidOperationException();
            }

            Protocol      = ProtocolFromString(fields [0]);
            StatusCode    = (HttpStatusCode)int.Parse(fields [1]);
            StatusMessage = fields.Length == 3 ? fields [2] : string.Empty;

            cancellationToken.ThrowIfCancellationRequested();
            await ReadHeaders(ctx, reader, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();
            Body = await ReadBody(ctx, reader, false, cancellationToken);
        }
Esempio n. 6
0
        protected async Task ReadHeaders(TestContext ctx, HttpStreamReader reader, CancellationToken cancellationToken)
        {
            string line;

            while ((line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false)) != null)
            {
                cancellationToken.ThrowIfCancellationRequested();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }
                var pos = line.IndexOf(':');
                if (pos < 0)
                {
                    throw new InvalidOperationException();
                }

                var headerName  = line.Substring(0, pos);
                var headerValue = line.Substring(pos + 1).Trim();
                headers.Add(headerName, headerValue);
            }
        }