public ServiceContext(ServiceContext context)
 {
     AppShutdown = context.AppShutdown;
     Memory = context.Memory;
     Log = context.Log;
     DateHeaderValueManager = context.DateHeaderValueManager;
 }
 public void Create(Func<IFeatureCollection, Task> app, ServiceContext context, string serverAddress)
 {
     _engine = new KestrelEngine(context);
     _engine.Start(1);
     _server = _engine.CreateServer(
         ServerAddress.FromUrl(serverAddress),
         app);
 }
 public ServiceContext(ServiceContext context)
 {
     AppLifetime = context.AppLifetime;
     Memory = context.Memory;
     Log = context.Log;
     DateHeaderValueManager = context.DateHeaderValueManager;
     ConnectionFilter = context.ConnectionFilter;
     NoDelay = context.NoDelay;
 }
 public void Create(RequestDelegate app, ServiceContext context, string serverAddress)
 {
     context.FrameFactory = (connectionContext, remoteEP, localEP, prepareRequest) => 
     {
         return new Frame<HttpContext>(new DummyApplication(app), connectionContext, remoteEP, localEP, prepareRequest);
     };
     _engine = new KestrelEngine(context);
     _engine.Start(1);
     _server = _engine.CreateServer(
         ServerAddress.FromUrl(serverAddress));
 }
 public ServiceContext(ServiceContext context)
 {
     AppLifetime = context.AppLifetime;
     Log = context.Log;
     ThreadPool = context.ThreadPool;
     FrameFactory = context.FrameFactory;
     DateHeaderValueManager = context.DateHeaderValueManager;
     ConnectionFilter = context.ConnectionFilter;
     NoDelay = context.NoDelay;
     ReuseStreams = context.ReuseStreams;
 }
 public async Task Http10KeepAliveTransferEncoding(ServiceContext testContext)
 {
     using (var server = new TestServer(AppChunked, testContext))
     {
         using (var connection = new TestConnection())
         {
             await connection.SendEnd(
                 "POST / HTTP/1.0",
                 "Transfer-Encoding: chunked",
                 "Connection: keep-alive",
                 "",
                 "5", "Hello", "6", " World", "0",
                 "POST / HTTP/1.0",
                 "",
                 "Goodbye");
             await connection.Receive(
                 "HTTP/1.0 200 OK",
                 "Content-Length: 11",
                 "Connection: keep-alive",
                 "",
                 "Hello World");
             await connection.ReceiveEnd(
                 "HTTP/1.0 200 OK",
                 "Content-Length: 7",
                 "",
                 "Goodbye");
         }
     }
 }
Example #7
0
 public KestrelEngine(ServiceContext context)
     : this(new Libuv(), context)
 {
 }
        public async Task RequestBodyIsConsumedAutomaticallyIfAppDoesntConsumeItFully(ServiceContext testContext)
        {
            using (var server = new TestServer(async frame =>
            {
                var response = frame.Get<IHttpResponseFeature>();
                var request = frame.Get<IHttpRequestFeature>();

                Assert.Equal("POST", request.Method);

                response.Headers.Clear();
                response.Headers["Content-Length"] = new[] { "11" };

                await response.Body.WriteAsync(Encoding.ASCII.GetBytes("Hello World"), 0, 11);
            }, testContext))
            {
                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "POST / HTTP/1.1",
                        "Content-Length: 5",
                        "",
                        "HelloPOST / HTTP/1.1",
                        "Transfer-Encoding: chunked",
                        "",
                        "C", "HelloChunked", "0",
                        "POST / HTTP/1.1",
                        "Content-Length: 7",
                        "",
                        "Goodbye");
                    await connection.ReceiveEnd(
                        "HTTP/1.1 200 OK",
                        "Content-Length: 11",
                        "",
                        "Hello WorldHTTP/1.1 200 OK",
                        "Content-Length: 11",
                        "",
                        "Hello WorldHTTP/1.1 200 OK",
                        "Content-Length: 11",
                        "",
                        "Hello World");
                }
            }
        }
 public void EngineCanStartAndStop(ServiceContext testContext)
 {
     var engine = new KestrelEngine(testContext);
     engine.Start(1);
     engine.Dispose();
 }
        public async Task ConnectionClosesWhenFinReceivedBeforeRequestCompletes(ServiceContext testContext)
        {
            using (var server = new TestServer(AppChunked, testContext))
            {
                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "GET /");
                    await connection.ReceiveEnd();
                }

                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "GET / HTTP/1.1",
                        "",
                        "Post / HTTP/1.1");
                    await connection.ReceiveEnd(
                        "HTTP/1.1 200 OK",
                        "Content-Length: 0",
                        "",
                        "");
                }

                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "GET / HTTP/1.1",
                        "",
                        "Post / HTTP/1.1",
                        "Content-Length: 7");
                    await connection.ReceiveEnd(
                        "HTTP/1.1 200 OK",
                        "Content-Length: 0",
                        "",
                        "");
                }
            }
        }
        public async Task ThrowingResultsIn500Response(ServiceContext testContext)
        {
            bool onStartingCalled = false;

            var testLogger = new TestApplicationErrorLogger();
            testContext.Log = new KestrelTrace(testLogger);

            using (var server = new TestServer(frame =>
            {
                var response = frame.Get<IHttpResponseFeature>();
                response.OnStarting(_ =>
                {
                    onStartingCalled = true;
                    return Task.FromResult<object>(null);
                }, null);

                // Anything added to the ResponseHeaders dictionary is ignored
                response.Headers.Clear();
                response.Headers["Content-Length"] = "11";
                throw new Exception();
            }, testContext))
            {
                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "GET / HTTP/1.1",
                        "",
                        "GET / HTTP/1.1",
                        "Connection: close",
                        "",
                        "");
                    await connection.Receive(
                        "HTTP/1.1 500 Internal Server Error",
                        "");
                    await connection.ReceiveStartsWith("Date:");
                    await connection.Receive(
                        "Content-Length: 0",
                        "Server: Kestrel",
                        "",
                        "HTTP/1.1 500 Internal Server Error",
                        "");
                    await connection.ReceiveStartsWith("Date:");
                    await connection.ReceiveEnd(
                        "Content-Length: 0",
                        "Server: Kestrel",
                        "Connection: close",
                        "",
                        "");

                    Assert.False(onStartingCalled);
                    Assert.Equal(2, testLogger.ApplicationErrorsLogged);
                }
            }
        }
 public async Task ZeroContentLengthNotSetAutomaticallyForHeadRequests(ServiceContext testContext)
 {
     using (var server = new TestServer(EmptyApp, testContext))
     {
         using (var connection = new TestConnection())
         {
             await connection.SendEnd(
                 "HEAD / HTTP/1.1",
                 "",
                 "");
             await connection.ReceiveEnd(
                 "HTTP/1.1 200 OK",
                 "",
                 "");
         }
     }
 }
 public async Task ZeroContentLengthSetAutomaticallyAfterNoWrites(ServiceContext testContext)
 {
     using (var server = new TestServer(EmptyApp, testContext))
     {
         using (var connection = new TestConnection())
         {
             await connection.SendEnd(
                 "GET / HTTP/1.1",
                 "",
                 "GET / HTTP/1.0",
                 "Connection: keep-alive",
                 "",
                 "");
             await connection.ReceiveEnd(
                 "HTTP/1.1 200 OK",
                 "Content-Length: 0",
                 "",
                 "HTTP/1.0 200 OK",
                 "Content-Length: 0",
                 "Connection: keep-alive",
                 "",
                 "");
         }
     }
 }
 public async Task Expect100ContinueForBody(ServiceContext testContext)
 {
     using (var server = new TestServer(AppChunked, testContext))
     {
         using (var connection = new TestConnection())
         {
             await connection.Send(
                 "POST / HTTP/1.1",
                 "Expect: 100-continue",
                 "Content-Length: 11",
                 "Connection: close",
                 "\r\n");
             await connection.Receive("HTTP/1.1 100 Continue", "\r\n");
             await connection.SendEnd("Hello World");
             await connection.Receive(
                 "HTTP/1.1 200 OK",
                 "Content-Length: 11",
                 "Connection: close",
                 "",
                 "Hello World");
         }
     }
 }
 public TestServer(Func<IFeatureCollection, Task> app, ServiceContext context, string serverAddress)
 {
     Create(app, context, serverAddress);
 }
 public TestServer(Func<IFeatureCollection, Task> app, ServiceContext context)
     : this(app, context, "http://localhost:54321/")
 {
 }
Example #17
0
 // For testing
 internal KestrelEngine(Libuv uv, ServiceContext context)
     : base(context)
 {
     Libuv   = uv;
     Threads = new List <KestrelThread>();
 }
        public async Task ReuseStreamsOff(ServiceContext testContext)
        {
            testContext.ReuseStreams = false;

            var streamCount = 0;
            var loopCount = 20;
            Stream lastStream = null;

            using (var server = new TestServer(
                context =>
                {
                    if (context.Request.Body != lastStream)
                    {
                        lastStream = context.Request.Body;
                        streamCount++;
                    }
                    context.Response.Headers.Clear();
                    return context.Request.Body.CopyToAsync(context.Response.Body);
                },
                    testContext))
            {

                using (var connection = new TestConnection())
                {
                    var requestData =
                        Enumerable.Repeat("GET / HTTP/1.1\r\n", loopCount)
                            .Concat(new[] { "GET / HTTP/1.1\r\nConnection: close\r\n\r\nGoodbye" });

                    var responseData =
                        Enumerable.Repeat("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n", loopCount)
                            .Concat(new[] { "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nGoodbye" });

                    await connection.SendEnd(requestData.ToArray());

                    await connection.ReceiveEnd(responseData.ToArray());
                }

                Assert.Equal(loopCount + 1, streamCount);
            }
        }
        public async Task DisconnectingClient(ServiceContext testContext)
        {
            using (var server = new TestServer(App, testContext))
            {
                var socket = new Socket(SocketType.Stream, ProtocolType.IP);
                socket.Connect(IPAddress.Loopback, 54321);
                await Task.Delay(200);
                socket.Dispose();

                await Task.Delay(200);
                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "GET / HTTP/1.0",
                        "\r\n");
                    await connection.ReceiveEnd(
                        "HTTP/1.0 200 OK",
                        "\r\n");
                }
            }
        }
 public KestrelEngine(ServiceContext context)
     : this(new Libuv(), context)
 { }
        public async Task ZeroContentLengthNotSetAutomaticallyForNonKeepAliveRequests(ServiceContext testContext)
        {
            using (var server = new TestServer(EmptyApp, testContext))
            {
                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "GET / HTTP/1.1",
                        "Connection: close",
                        "",
                        "");
                    await connection.ReceiveEnd(
                        "HTTP/1.1 200 OK",
                        "Connection: close",
                        "",
                        "");
                }

                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "GET / HTTP/1.0",
                        "",
                        "");
                    await connection.ReceiveEnd(
                        "HTTP/1.0 200 OK",
                        "",
                        "");
                }
            }
        }
 // For testing
 internal KestrelEngine(Libuv uv, ServiceContext context)
    : base(context)
 {
     Libuv = uv;
     Threads = new List<KestrelThread>();
 }
        public async Task ZeroContentLengthNotSetAutomaticallyForCertainStatusCodes(ServiceContext testContext)
        {
            using (var server = new TestServer(async frame =>
            {
                var request = frame.Get<IHttpRequestFeature>();
                var response = frame.Get<IHttpResponseFeature>();
                response.Headers.Clear();

                using (var reader = new StreamReader(request.Body, Encoding.ASCII))
                {
                    var statusString = await reader.ReadLineAsync();
                    response.StatusCode = int.Parse(statusString);
                }
            }, testContext))
            {
                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "POST / HTTP/1.1",
                        "Content-Length: 3",
                        "",
                        "101POST / HTTP/1.1",
                        "Content-Length: 3",
                        "",
                        "204POST / HTTP/1.1",
                        "Content-Length: 3",
                        "",
                        "205POST / HTTP/1.1",
                        "Content-Length: 3",
                        "",
                        "304POST / HTTP/1.1",
                        "Content-Length: 3",
                        "",
                        "200");
                    await connection.ReceiveEnd(
                        "HTTP/1.1 101 Switching Protocols",
                        "",
                        "HTTP/1.1 204 No Content",
                        "",
                        "HTTP/1.1 205 Reset Content",
                        "",
                        "HTTP/1.1 304 Not Modified",
                        "",
                        "HTTP/1.1 200 OK",
                        "Content-Length: 0",
                        "",
                        "");
                }
            }
        }
        public void ConnectionCanReadAndWrite(ServiceContext testContext)
        {
            var engine = new KestrelEngine(testContext);
            engine.Start(1);
            var address = ServerAddress.FromUrl("http://localhost:54321/");
            var started = engine.CreateServer(address, App);

            Console.WriteLine("Started");
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(new IPEndPoint(IPAddress.Loopback, 54321));
            socket.Send(Encoding.ASCII.GetBytes("POST / HTTP/1.0\r\n\r\nHello World"));
            socket.Shutdown(SocketShutdown.Send);
            var buffer = new byte[8192];
            while (true)
            {
                var length = socket.Receive(buffer);
                if (length == 0) { break; }
                var text = Encoding.ASCII.GetString(buffer, 0, length);
            }
            started.Dispose();
            engine.Dispose();
        }
        public async Task ThrowingAfterPartialWriteKillsConnection(ServiceContext testContext)
        {
            bool onStartingCalled = false;

            var testLogger = new TestApplicationErrorLogger();
            testContext.Log = new KestrelTrace(testLogger);

            using (var server = new TestServer(async frame =>
            {
                var response = frame.Get<IHttpResponseFeature>();
                response.OnStarting(_ =>
                {
                    onStartingCalled = true;
                    return Task.FromResult<object>(null);
                }, null);

                response.Headers.Clear();
                response.Headers["Content-Length"] = new[] { "11" };
                await response.Body.WriteAsync(Encoding.ASCII.GetBytes("Hello"), 0, 5);
                throw new Exception();
            }, testContext))
            {
                using (var connection = new TestConnection())
                {
                    await connection.Send(
                        "GET / HTTP/1.1",
                        "",
                        "");
                    await connection.ReceiveEnd(
                        "HTTP/1.1 200 OK",
                        "Content-Length: 11",
                        "",
                        "Hello");

                    Assert.True(onStartingCalled);
                    Assert.Equal(1, testLogger.ApplicationErrorsLogged);
                }
            }
        }
 public async Task Http11(ServiceContext testContext)
 {
     using (var server = new TestServer(AppChunked, testContext))
     {
         using (var connection = new TestConnection())
         {
             await connection.SendEnd(
                 "GET / HTTP/1.1",
                 "",
                 "GET / HTTP/1.1",
                 "Connection: close",
                 "",
                 "Goodbye");
             await connection.ReceiveEnd(
                 "HTTP/1.1 200 OK",
                 "Content-Length: 0",
                 "",
                 "HTTP/1.1 200 OK",
                 "Content-Length: 7",
                 "Connection: close",
                 "",
                 "Goodbye");
         }
     }
 }
        public async Task ThrowingInOnStartingResultsInFailedWritesAnd500Response(ServiceContext testContext)
        {
            var onStartingCallCount1 = 0;
            var onStartingCallCount2 = 0;
            var failedWriteCount = 0;

            var testLogger = new TestApplicationErrorLogger();
            testContext.Log = new KestrelTrace(testLogger);

            using (var server = new TestServer(async frame =>
            {
                var onStartingException = new Exception();

                var response = frame.Get<IHttpResponseFeature>();
                response.OnStarting(_ =>
                {
                    onStartingCallCount1++;
                    throw onStartingException;
                }, null);
                response.OnStarting(_ =>
                {
                    onStartingCallCount2++;
                    throw onStartingException;
                }, null);

                response.Headers.Clear();
                response.Headers["Content-Length"] = new[] { "11" };

                var writeException = await Assert.ThrowsAsync<ObjectDisposedException>(async () =>
                    await response.Body.WriteAsync(Encoding.ASCII.GetBytes("Hello World"), 0, 11));

                Assert.Same(onStartingException, writeException.InnerException);

                failedWriteCount++;
            }, testContext))
            {
                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "GET / HTTP/1.1",
                        "",
                        "GET / HTTP/1.1",
                        "Connection: close",
                        "",
                        "");
                    await connection.Receive(
                        "HTTP/1.1 500 Internal Server Error",
                        "");
                    await connection.ReceiveStartsWith("Date:");
                    await connection.Receive(
                        "Content-Length: 0",
                        "Server: Kestrel",
                        "",
                        "HTTP/1.1 500 Internal Server Error",
                        "");
                    await connection.ReceiveStartsWith("Date:");
                    await connection.ReceiveEnd(
                        "Content-Length: 0",
                        "Server: Kestrel",
                        "Connection: close",
                        "",
                        "");

                    Assert.Equal(2, onStartingCallCount1);
                    // The second OnStarting callback should not be called since the first failed.
                    Assert.Equal(0, onStartingCallCount2);
                    Assert.Equal(2, testLogger.ApplicationErrorsLogged);
                }
            }
        }
 public async Task Http10ContentLength(ServiceContext testContext)
 {
     using (var server = new TestServer(App, testContext))
     {
         using (var connection = new TestConnection())
         {
             await connection.Send(
                 "POST / HTTP/1.0",
                 "Content-Length: 11",
                 "",
                 "Hello World");
             await connection.ReceiveEnd(
                 "HTTP/1.0 200 OK",
                 "",
                 "Hello World");
         }
     }
 }
        public async Task ThrowingInOnCompletedIsLoggedAndClosesConnection(ServiceContext testContext)
        {
            var onCompletedCalled1 = false;
            var onCompletedCalled2 = false;

            var testLogger = new TestApplicationErrorLogger();
            testContext.Log = new KestrelTrace(testLogger);

            using (var server = new TestServer(async frame =>
            {
                var response = frame.Get<IHttpResponseFeature>();
                response.OnCompleted(_ =>
                {
                    onCompletedCalled1 = true;
                    throw new Exception();
                }, null);
                response.OnCompleted(_ =>
                {
                    onCompletedCalled2 = true;
                    throw new Exception();
                }, null);

                response.Headers.Clear();
                response.Headers["Content-Length"] = new[] { "11" };

                await response.Body.WriteAsync(Encoding.ASCII.GetBytes("Hello World"), 0, 11);
            }, testContext))
            {
                using (var connection = new TestConnection())
                {
                    await connection.Send(
                        "GET / HTTP/1.1",
                        "",
                        "");
                    await connection.ReceiveEnd(
                        "HTTP/1.1 200 OK",
                        "Content-Length: 11",
                        "",
                        "Hello World");
                }

                // All OnCompleted callbacks should be called even if they throw.
                Assert.Equal(2, testLogger.ApplicationErrorsLogged);
                Assert.True(onCompletedCalled1);
                Assert.True(onCompletedCalled2);
            }
        }
 public async Task Http10TransferEncoding(ServiceContext testContext)
 {
     using (var server = new TestServer(App, testContext))
     {
         using (var connection = new TestConnection())
         {
             await connection.Send(
                 "POST / HTTP/1.0",
                 "Transfer-Encoding: chunked",
                 "",
                 "5", "Hello", "6", " World", "0\r\n");
             await connection.ReceiveEnd(
                 "HTTP/1.0 200 OK",
                 "",
                 "Hello World");
         }
     }
 }
 public void ListenerCanCreateAndDispose(ServiceContext testContext)
 {
     var engine = new KestrelEngine(testContext);
     engine.Start(1);
     var address = ServerAddress.FromUrl("http://localhost:54321/");
     var started = engine.CreateServer(address, App);
     started.Dispose();
     engine.Dispose();
 }
 public async Task Http10KeepAliveNotUsedIfResponseContentLengthNotSet(ServiceContext testContext)
 {
     using (var server = new TestServer(App, testContext))
     {
         using (var connection = new TestConnection())
         {
             await connection.SendEnd(
                 "GET / HTTP/1.0",
                 "Connection: keep-alive",
                 "",
                 "POST / HTTP/1.0",
                 "Connection: keep-alive",
                 "Content-Length: 7",
                 "",
                 "Goodbye");
             await connection.Receive(
                 "HTTP/1.0 200 OK",
                 "Content-Length: 0",
                 "Connection: keep-alive",
                 "\r\n");
             await connection.ReceiveEnd(
                 "HTTP/1.0 200 OK",
                 "",
                 "Goodbye");
         }
     }
 }