Пример #1
0
 public StatusCodePagesMiddleware(RequestDelegate next, IOptions <StatusCodePagesOptions> options)
 {
     _next    = next;
     _options = options.Value;
     if (_options.HandleAsync == null)
     {
         throw new ArgumentException("Missing options.HandleAsync implementation.");
     }
 }
Пример #2
0
 public StatusCodeContext(HttpContext context, StatusCodePagesOptions options, RequestDelegate next)
 {
     HttpContext = context;
     Options     = options;
     Next        = next;
 }
Пример #3
0
 public ErrorPageMiddleware(RequestDelegate next, ILoggerFactory logFac, IOptions <StatusCodePagesOptions> options)
 {
     _next    = next;
     _logger  = logFac.CreateLogger <ErrorPageMiddleware>();
     _options = options.Value;
 }
Пример #4
0
    /// <summary>
    /// Adds a StatusCodePages middleware with the given options that checks for responses with status codes
    /// between 400 and 599 that do not have a body.
    /// </summary>
    /// <param name="app"></param>
    /// <param name="options"></param>
    /// <returns></returns>
    public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app, StatusCodePagesOptions options)
    {
        if (app == null)
        {
            throw new ArgumentNullException(nameof(app));
        }
        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

        return(app.UseMiddleware <StatusCodePagesMiddleware>(Options.Create(options)));
    }
Пример #5
0
 /// <summary>
 /// Adds a StatusCodePages middleware with the given options that checks for responses with status codes
 /// between 400 and 599 that do not have a body.
 /// </summary>
 /// <param name="app"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static IApplicationBuilder UseStatusCodePages([NotNull] this IApplicationBuilder app, StatusCodePagesOptions options)
 {
     return(app.UseMiddleware <StatusCodePagesMiddleware>(options));
 }
Пример #6
0
        public void Configure(IApplicationBuilder app, IApplicationLifetime lifetime, ILoggerFactory loggerFactory)
        {
            var webListenerInfo = app.ServerFeatures.Get <WebListener>();

            if (webListenerInfo != null)
            {
                webListenerInfo.Settings.Authentication.Schemes = AuthenticationSchemes.None;
            }

            //loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddConsole();
            loggerFactory.AddDebug();

            var log2 = loggerFactory.CreateLogger("test");

            log2.LogWarning("log test");


            var o = new StatusCodePagesOptions();

            o.HandleAsync = async statusCodeContext => {
                var request = statusCodeContext.HttpContext.Request;
                var context = statusCodeContext.HttpContext;
                var log     = loggerFactory.CreateLogger <Startup>();
                log.LogWarning($"{request.Path} wasn't handled.");
                var uri = request.GetUri();

                var body = "<not decoded>";
                if (!uri.ToString().Contains("secure") && request.Method.ToLower() == "post")
                {
                    //body = request.Form.Keys.First();
                }

                var isFavIcon = uri.AbsolutePath.EndsWith("favicon.ico");

                //not the singleton instance var musicCastHost = (MusicCastHost) app.ApplicationServices.GetService(typeof (MusicCastHost));

                if (!isFavIcon)
                {
                    if (context.Response.HasStarted)
                    {
                        log.LogCritical("Too late");
                    }
                    else
                    {
                        var manInTheMiddleResponse = UriService.GetManInTheMiddleResult(_musicCastHost.RelayHost, uri, ActivityLogMiddleware.MapPortToReal);
                        log.LogInformation($"Man in the middle! {manInTheMiddleResponse.RequestUri}");
                        context.Response.StatusCode = 200;
                        await context.Response.WriteAsync(manInTheMiddleResponse.ResponseBody);

                        var logService = new LogService(_environmentService);
                        logService.LogToDisk(-1, manInTheMiddleResponse);
                    }
                }
            };

            // So we can decode the formurlencoded content that isn't properly encoded
            app.Use((context, next) => { context.Request.EnableRewind(); return(next()); });

            app.UseStatusCodePages(o);

            app.UseHeadersMiddleware();

            app.UseActivityLogMiddleware();
            app.UseStaticFiles();
            app.UseMvc();
        }