|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNet.Builder; |
| 7 | +using Microsoft.AspNet.Hosting; |
| 8 | +using Microsoft.AspNet.Http; |
| 9 | +using Microsoft.Extensions.PlatformAbstractions; |
| 10 | + |
| 11 | +namespace Benchmarks |
| 12 | +{ |
| 13 | + public class DebugInfoPageMiddleware |
| 14 | + { |
| 15 | + private static readonly PathString _path = new PathString("/debug"); |
| 16 | +#if DEBUG |
| 17 | + private static readonly string _configurationName = "Debug"; |
| 18 | +#elif RELEASE |
| 19 | + private static readonly string _configurationName = "Release"; |
| 20 | +#else |
| 21 | + private static readonly string _configurationName = ""; |
| 22 | +#endif |
| 23 | + |
| 24 | + private readonly IApplicationEnvironment _appEnv; |
| 25 | + private readonly IHostingEnvironment _hostingEnv; |
| 26 | + private readonly RequestDelegate _next; |
| 27 | + |
| 28 | + public DebugInfoPageMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, IApplicationEnvironment appEnv) |
| 29 | + { |
| 30 | + _next = next; |
| 31 | + _hostingEnv = hostingEnv; |
| 32 | + _appEnv = appEnv; |
| 33 | + } |
| 34 | + |
| 35 | + public async Task Invoke(HttpContext httpContext) |
| 36 | + { |
| 37 | + // We check Ordinal explicitly first because it's faster than OrdinalIgnoreCase |
| 38 | + if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal) || |
| 39 | + httpContext.Request.Path.StartsWithSegments(_path, StringComparison.OrdinalIgnoreCase)) |
| 40 | + { |
| 41 | + httpContext.Response.ContentType = "text/html"; |
| 42 | + |
| 43 | + await httpContext.Response.WriteAsync("<h1>Application Information</h1>"); |
| 44 | + await httpContext.Response.WriteAsync("<ul>"); |
| 45 | + |
| 46 | + await httpContext.Response.WriteAsync($"<li>Environment: {_hostingEnv.EnvironmentName}</li>"); |
| 47 | + await httpContext.Response.WriteAsync($"<li>Framework: {_appEnv.RuntimeFramework.FullName}</li>"); |
| 48 | + await httpContext.Response.WriteAsync($"<li>Configuration: {_configurationName}</li>"); |
| 49 | + await httpContext.Response.WriteAsync($"<li>Server: {_hostingEnv.Configuration["server"]}</li>"); |
| 50 | + await httpContext.Response.WriteAsync($"<li>Server URLs: {_hostingEnv.Configuration["server.urls"]}</li>"); |
| 51 | + |
| 52 | + await httpContext.Response.WriteAsync($"<li>Server features:<ol>"); |
| 53 | + foreach (var feature in httpContext.Features) |
| 54 | + { |
| 55 | + await httpContext.Response.WriteAsync($"<li>{feature.Key.Name}</li>"); |
| 56 | + } |
| 57 | + await httpContext.Response.WriteAsync($"</ol></li>"); |
| 58 | + |
| 59 | + await httpContext.Response.WriteAsync("</ul>"); |
| 60 | + |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + await _next(httpContext); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static class DebugInfoPageMiddlewareExtensions |
| 69 | + { |
| 70 | + public static IApplicationBuilder UseDebugInfoPage(this IApplicationBuilder builder) |
| 71 | + { |
| 72 | + return builder.UseMiddleware<DebugInfoPageMiddleware>(); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments