Ejemplo n.º 1
0
 protected async Task <HttpContent> ReadBody(TestContext ctx, HttpStreamReader reader,
                                             bool readNonChunked, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     if (TransferEncoding != null)
     {
         if (!TransferEncoding.Equals("chunked"))
         {
             throw new InvalidOperationException();
         }
         if (readNonChunked)
         {
             return(await ChunkedContent.ReadNonChunked(reader, cancellationToken));
         }
         return(await ChunkedContent.Read(ctx, reader, cancellationToken));
     }
     if (ContentType != null && ContentType.Equals("application/octet-stream"))
     {
         return(await BinaryContent.Read(reader, ContentLength.Value, cancellationToken));
     }
     if (ContentLength != null)
     {
         return(await StringContent.Read(ctx, reader, ContentLength.Value, cancellationToken));
     }
     return(null);
 }
Ejemplo n.º 2
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)));
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 internal HttpRequest(HttpProtocol protocol, string method, string path, HttpStreamReader reader)
     : base(protocol)
 {
     Method      = method;
     Path        = path;
     this.reader = reader;
 }
Ejemplo n.º 5
0
        public static async Task <HttpRequest> Read(HttpStreamReader reader, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var request = new HttpRequest();
            await request.InternalRead(reader, cancellationToken);

            return(request);
        }
Ejemplo n.º 6
0
        internal static async Task <HttpResponse> Read(TestContext ctx, HttpStreamReader reader, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            try {
                var response = new HttpResponse();
                await response.InternalRead(ctx, reader, cancellationToken).ConfigureAwait(false);

                return(response);
            } catch (Exception ex) {
                return(CreateError(ex));
            }
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        public static async Task <ChunkedContent> Read(TestContext ctx, HttpStreamReader reader, CancellationToken cancellationToken)
        {
            var chunks = new List <string> ();

            do
            {
                cancellationToken.ThrowIfCancellationRequested();
                var chunk = await ReadChunk(ctx, reader, cancellationToken).ConfigureAwait(false);

                if (chunk == null)
                {
                    break;
                }

                chunks.Add(Encoding.UTF8.GetString(chunk, 0, chunk.Length));
            } while (true);

            return(new ChunkedContent(chunks));
        }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
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);
            }
        }
Ejemplo n.º 12
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));
        }
Ejemplo n.º 13
0
        internal async Task Read(TestContext ctx, CancellationToken cancellationToken)
        {
            if (bodyRead)
            {
                return;
            }
            bodyRead = true;

            await ReadHeaders(ctx, cancellationToken).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();
            if (listenerRequest != null)
            {
                using (var bodyReader = new HttpStreamReader(listenerRequest.InputStream)) {
                    Body = await ReadBody(ctx, bodyReader, true, cancellationToken);
                }
            }
            else
            {
                Body = await ReadBody(ctx, reader, false, cancellationToken);
            }
        }
Ejemplo n.º 14
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));
        }
Ejemplo n.º 15
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()));
            }
        }