From 13312109ffa576688cc300e727bfc2b242b1a033 Mon Sep 17 00:00:00 2001 From: jhkimnew Date: Mon, 6 Nov 2017 13:47:59 -0800 Subject: [PATCH] Test: Added a new MiddleWare to test the Gracefulshutdown message (#233) Added TestMiddleWare to handle the Gracefulshutdown message instead of IISMiddleware --- .../IISSetupFilter.cs | 31 +++++++++++++ .../Program.cs | 15 +++++-- .../Startup.cs | 38 ++++++---------- .../TestMiddleWareBeforeIISMiddleWare.cs | 43 +++++++++++++++++++ 4 files changed, 97 insertions(+), 30 deletions(-) create mode 100644 test/AspNetCoreModule.TestSites.Standard/IISSetupFilter.cs create mode 100644 test/AspNetCoreModule.TestSites.Standard/TestMiddleWareBeforeIISMiddleWare.cs diff --git a/test/AspNetCoreModule.TestSites.Standard/IISSetupFilter.cs b/test/AspNetCoreModule.TestSites.Standard/IISSetupFilter.cs new file mode 100644 index 0000000000..7c9a92a664 --- /dev/null +++ b/test/AspNetCoreModule.TestSites.Standard/IISSetupFilter.cs @@ -0,0 +1,31 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Server.IISIntegration; + +namespace AspnetCoreModule.TestSites.Standard +{ + internal class IISSetupFilter : IStartupFilter + { + private readonly string _pairingToken; + + internal IISSetupFilter(string pairingToken) + { + _pairingToken = pairingToken; + } + + public Action Configure(Action next) + { + return app => + { + app.UseMiddleware(); + app.UseMiddleware(_pairingToken); + next(app); + }; + } + } +} \ No newline at end of file diff --git a/test/AspNetCoreModule.TestSites.Standard/Program.cs b/test/AspNetCoreModule.TestSites.Standard/Program.cs index 89f464b0b2..f7e3a9483a 100644 --- a/test/AspNetCoreModule.TestSites.Standard/Program.cs +++ b/test/AspNetCoreModule.TestSites.Standard/Program.cs @@ -2,13 +2,13 @@ // Licensed under the MIT License. See License.txt in the project root for license information. using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Server.Kestrel.Https; using Microsoft.Extensions.Configuration; using System; using System.IO; using System.Security.Cryptography.X509Certificates; using System.Threading; -using System.Net; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; namespace AspnetCoreModule.TestSites.Standard { @@ -88,8 +88,15 @@ namespace AspnetCoreModule.TestSites.Standard else { builder = new WebHostBuilder() + .ConfigureServices(services => + { + const string PairingToken = "TOKEN"; + string paringToken = builder.GetSetting(PairingToken) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{PairingToken}"); + services.AddSingleton( + new IISSetupFilter(paringToken) + ); + }) .UseConfiguration(config) - .UseIISIntegration() .UseStartup(); } @@ -139,7 +146,7 @@ namespace AspnetCoreModule.TestSites.Standard ); try { - host.Run(); + host.Run(); } catch { diff --git a/test/AspNetCoreModule.TestSites.Standard/Startup.cs b/test/AspNetCoreModule.TestSites.Standard/Startup.cs index a86e1eb3f8..af68d68aa4 100644 --- a/test/AspNetCoreModule.TestSites.Standard/Startup.cs +++ b/test/AspNetCoreModule.TestSites.Standard/Startup.cs @@ -252,16 +252,16 @@ namespace AspnetCoreModule.TestSites.Standard parameter = "1024"; if (item.Length > action.Length) { - parameter = item.Substring(action.Length); + parameter = item.Substring(action.Length); } - long size = Convert.ToInt32(parameter); - var rnd = new Random(); - byte[] b = new byte[size*1024]; + long size = Convert.ToInt32(parameter); + var rnd = new Random(); + byte[] b = new byte[size * 1024]; b[rnd.Next(0, b.Length)] = byte.MaxValue; - MemoryLeakList.Add(b); + MemoryLeakList.Add(b); response = "MemoryLeak, size:" + size.ToString() + " KB, total: " + MemoryLeakList.Count.ToString(); } - + action = "ExpandEnvironmentVariables"; if (item.StartsWith(action)) { @@ -269,7 +269,7 @@ namespace AspnetCoreModule.TestSites.Standard { parameter = item.Substring(action.Length); response = Environment.ExpandEnvironmentVariables("%" + parameter + "%"); - } + } } action = "GetEnvironmentVariables"; @@ -285,9 +285,9 @@ namespace AspnetCoreModule.TestSites.Standard response = String.Empty; foreach (DictionaryEntry de in Environment.GetEnvironmentVariables()) - { + { response += de.Key + ":" + de.Value + "
"; - } + } } action = "GetRequestHeaderValue"; @@ -312,7 +312,7 @@ namespace AspnetCoreModule.TestSites.Standard if (item.StartsWith(action)) { response = String.Empty; - + foreach (var de in context.Request.Headers) { response += de.Key + ":" + de.Value + "
"; @@ -326,7 +326,7 @@ namespace AspnetCoreModule.TestSites.Standard if (item.Length > action.Length) { // this is the default response which is valid for chunked encoding - response = "10\r\nManually Chunked\r\n0\r\n\r\n"; + response = "10\r\nManually Chunked\r\n0\r\n\r\n"; parameter = item.Substring(action.Length); // valid encoding value: "chunked" or "chunked,gzip" @@ -334,7 +334,7 @@ namespace AspnetCoreModule.TestSites.Standard var tokens = parameter.Split("_"); // if respons body value was also given after "_" delimeter, use the value as response data. - if (tokens.Length == 2) + if (tokens.Length == 2) { encoding = tokens[0]; response = tokens[1]; @@ -344,20 +344,6 @@ namespace AspnetCoreModule.TestSites.Standard } } } - - // Handle shutdown event from ANCM - if (HttpMethods.IsPost(context.Request.Method) && - //context.Request.Path.Equals(ANCMRequestPath) && - string.Equals("shutdown", context.Request.Headers["MS-ASPNETCORE-EVENT"], StringComparison.OrdinalIgnoreCase)) - { - response = "shutdown"; - string shutdownMode = Environment.GetEnvironmentVariable("GracefulShutdown"); - if (String.IsNullOrEmpty(shutdownMode) || !shutdownMode.ToLower().StartsWith("disabled")) - { - context.Response.StatusCode = StatusCodes.Status202Accepted; - Program.AappLifetime.StopApplication(); - } - } return context.Response.WriteAsync(response); }); } diff --git a/test/AspNetCoreModule.TestSites.Standard/TestMiddleWareBeforeIISMiddleWare.cs b/test/AspNetCoreModule.TestSites.Standard/TestMiddleWareBeforeIISMiddleWare.cs new file mode 100644 index 0000000000..f2b4e87e08 --- /dev/null +++ b/test/AspNetCoreModule.TestSites.Standard/TestMiddleWareBeforeIISMiddleWare.cs @@ -0,0 +1,43 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using Microsoft.AspNetCore.Http; +using System; +using System.Security.Principal; +using System.Threading.Tasks; + + +namespace AspnetCoreModule.TestSites.Standard + +{ + public class TestMiddleWareBeforeIISMiddleWare + { + private readonly RequestDelegate next; + + public TestMiddleWareBeforeIISMiddleWare(RequestDelegate next) + { + this.next = next; + } + + public async Task Invoke(HttpContext context) + { + // if the given request is shutdown message from ANCM and the value of GracefulShutdown environment variable is set, + // the shutdown message is handled by this middleware instead of IISMiddleware. + + if (HttpMethods.IsPost(context.Request.Method) && + context.Request.Path.ToString().EndsWith("/iisintegration") && + string.Equals("shutdown", context.Request.Headers["MS-ASPNETCORE-EVENT"], StringComparison.OrdinalIgnoreCase)) + { + string shutdownMode = Environment.GetEnvironmentVariable("GracefulShutdown"); + if (!string.IsNullOrWhiteSpace(shutdownMode) && shutdownMode.ToLower().StartsWith("disabled")) + { + //ignore shutdown Message returning 200 instead of 202 because the gracefulshutdown is disabled + context.Response.StatusCode = StatusCodes.Status200OK; + await context.Response.WriteAsync("Called ShutdownMessage with disabled of GracefulShutdown"); + return; + } + } + await next.Invoke(context); + } + } +}