2020-04-21 15:36:22 +02:00
|
|
|
using System;
|
2020-04-21 17:09:05 +02:00
|
|
|
using System.IO;
|
2020-04-23 15:29:28 +02:00
|
|
|
using System.Net.Mime;
|
2020-04-24 05:23:29 +02:00
|
|
|
using System.Net.Sockets;
|
2020-04-21 15:36:22 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-04-21 17:09:05 +02:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2020-04-24 05:23:29 +02:00
|
|
|
using MediaBrowser.Controller.Authentication;
|
2020-04-21 17:09:05 +02:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Net;
|
2020-05-26 16:55:27 +02:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2020-04-21 15:36:22 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-05-26 16:55:27 +02:00
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-04-21 15:36:22 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
namespace Jellyfin.Server.Middleware
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Exception Middleware.
|
|
|
|
/// </summary>
|
|
|
|
public class ExceptionMiddleware
|
|
|
|
{
|
|
|
|
private readonly RequestDelegate _next;
|
|
|
|
private readonly ILogger<ExceptionMiddleware> _logger;
|
2020-04-21 17:09:05 +02:00
|
|
|
private readonly IServerConfigurationManager _configuration;
|
2020-05-26 16:55:27 +02:00
|
|
|
private readonly IWebHostEnvironment _hostEnvironment;
|
2020-04-21 15:36:22 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ExceptionMiddleware"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="next">Next request delegate.</param>
|
2020-04-23 15:29:28 +02:00
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger{ExceptionMiddleware}"/> interface.</param>
|
2020-04-21 17:09:05 +02:00
|
|
|
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
2020-05-26 16:55:27 +02:00
|
|
|
/// <param name="hostEnvironment">Instance of the <see cref="IWebHostEnvironment"/> interface.</param>
|
2020-04-21 17:09:05 +02:00
|
|
|
public ExceptionMiddleware(
|
|
|
|
RequestDelegate next,
|
2020-04-23 15:29:28 +02:00
|
|
|
ILogger<ExceptionMiddleware> logger,
|
2020-05-26 16:55:27 +02:00
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
IWebHostEnvironment hostEnvironment)
|
2020-04-21 15:36:22 +02:00
|
|
|
{
|
2020-04-21 17:09:05 +02:00
|
|
|
_next = next;
|
2020-04-23 15:29:28 +02:00
|
|
|
_logger = logger;
|
2020-04-21 17:09:05 +02:00
|
|
|
_configuration = serverConfigurationManager;
|
2020-05-26 16:55:27 +02:00
|
|
|
_hostEnvironment = hostEnvironment;
|
2020-04-21 15:36:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Invoke request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">Request context.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
public async Task Invoke(HttpContext context)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await _next(context).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
if (context.Response.HasStarted)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("The response has already started, the exception middleware will not be executed.");
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:09:05 +02:00
|
|
|
ex = GetActualException(ex);
|
2020-04-24 05:23:29 +02:00
|
|
|
|
|
|
|
bool ignoreStackTrace =
|
|
|
|
ex is SocketException
|
|
|
|
|| ex is IOException
|
|
|
|
|| ex is OperationCanceledException
|
|
|
|
|| ex is SecurityException
|
|
|
|
|| ex is AuthenticationException
|
|
|
|
|| ex is FileNotFoundException;
|
|
|
|
|
|
|
|
if (ignoreStackTrace)
|
|
|
|
{
|
|
|
|
_logger.LogError(
|
|
|
|
"Error processing request: {ExceptionMessage}. URL {Method} {Url}.",
|
|
|
|
ex.Message.TrimEnd('.'),
|
|
|
|
context.Request.Method,
|
|
|
|
context.Request.Path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.LogError(
|
|
|
|
ex,
|
|
|
|
"Error processing request. URL {Method} {Url}.",
|
|
|
|
context.Request.Method,
|
|
|
|
context.Request.Path);
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:09:05 +02:00
|
|
|
context.Response.StatusCode = GetStatusCode(ex);
|
2020-04-23 15:29:28 +02:00
|
|
|
context.Response.ContentType = MediaTypeNames.Text.Plain;
|
2020-05-26 16:55:27 +02:00
|
|
|
|
|
|
|
// Don't send exception unless the server is in a Development environment
|
2020-06-02 15:04:55 +02:00
|
|
|
var errorContent = _hostEnvironment.IsDevelopment()
|
|
|
|
? NormalizeExceptionMessage(ex.Message)
|
|
|
|
: "Error processing request.";
|
2020-04-21 17:09:05 +02:00
|
|
|
await context.Response.WriteAsync(errorContent).ConfigureAwait(false);
|
2020-04-21 15:36:22 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 17:09:05 +02:00
|
|
|
|
|
|
|
private static Exception GetActualException(Exception ex)
|
|
|
|
{
|
|
|
|
if (ex is AggregateException agg)
|
|
|
|
{
|
|
|
|
var inner = agg.InnerException;
|
|
|
|
if (inner != null)
|
|
|
|
{
|
|
|
|
return GetActualException(inner);
|
|
|
|
}
|
|
|
|
|
|
|
|
var inners = agg.InnerExceptions;
|
|
|
|
if (inners.Count > 0)
|
|
|
|
{
|
|
|
|
return GetActualException(inners[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int GetStatusCode(Exception ex)
|
|
|
|
{
|
|
|
|
switch (ex)
|
|
|
|
{
|
|
|
|
case ArgumentException _: return StatusCodes.Status400BadRequest;
|
|
|
|
case SecurityException _: return StatusCodes.Status401Unauthorized;
|
|
|
|
case DirectoryNotFoundException _:
|
|
|
|
case FileNotFoundException _:
|
|
|
|
case ResourceNotFoundException _: return StatusCodes.Status404NotFound;
|
|
|
|
case MethodNotAllowedException _: return StatusCodes.Status405MethodNotAllowed;
|
|
|
|
default: return StatusCodes.Status500InternalServerError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string NormalizeExceptionMessage(string msg)
|
|
|
|
{
|
|
|
|
if (msg == null)
|
|
|
|
{
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip any information we don't want to reveal
|
2020-04-23 15:29:28 +02:00
|
|
|
return msg.Replace(
|
|
|
|
_configuration.ApplicationPaths.ProgramSystemPath,
|
|
|
|
string.Empty,
|
|
|
|
StringComparison.OrdinalIgnoreCase)
|
|
|
|
.Replace(
|
|
|
|
_configuration.ApplicationPaths.ProgramDataPath,
|
|
|
|
string.Empty,
|
|
|
|
StringComparison.OrdinalIgnoreCase);
|
2020-04-21 17:09:05 +02:00
|
|
|
}
|
2020-04-21 15:36:22 +02:00
|
|
|
}
|
|
|
|
}
|