1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Plugin endpoints - Added exception event

Plugin endpoints - Handle wildcard values for Accept-Charset
This commit is contained in:
Robert 2021-02-25 19:42:13 +01:00
parent 291eabd20e
commit a9d6b17aa4
4 changed files with 50 additions and 6 deletions

View File

@ -0,0 +1,20 @@
using System;
namespace Artemis.Core.Services
{
/// <summary>
/// Provides data about endpoint exception related events
/// </summary>
public class EndpointExceptionEventArgs : EventArgs
{
internal EndpointExceptionEventArgs(Exception exception)
{
Exception = exception;
}
/// <summary>
/// Gets the exception that occurred
/// </summary>
public Exception Exception { get; }
}
}

View File

@ -52,15 +52,37 @@ namespace Artemis.Core.Services
/// </summary>
public string? Returns { get; protected set; }
/// <summary>
/// Occurs whenever a request threw an unhandled exception
/// </summary>
public event EventHandler<EndpointExceptionEventArgs>? RequestException;
/// <summary>
/// Called whenever the end point has to process a request
/// </summary>
/// <param name="context">The HTTP context of the request</param>
protected abstract Task ProcessRequest(IHttpContext context);
/// <summary>
/// Invokes the <see cref="RequestException" /> event
/// </summary>
/// <param name="e">The exception that occurred during the request</param>
protected virtual void OnRequestException(Exception e)
{
RequestException?.Invoke(this, new EndpointExceptionEventArgs(e));
}
internal async Task InternalProcessRequest(IHttpContext context)
{
await ProcessRequest(context);
try
{
await ProcessRequest(context);
}
catch (Exception e)
{
OnRequestException(e);
throw;
}
}
private void OnDisabled(object? sender, EventArgs e)

View File

@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EmbedIO;
using Newtonsoft.Json;
namespace Artemis.Core.Services
{
@ -67,6 +64,12 @@ namespace Artemis.Core.Services
if (!endPoints.TryGetValue(pathParts[1], out PluginEndPoint? endPoint))
throw HttpException.NotFound($"Found no endpoint called {pathParts[1]} for plugin with ID {pathParts[0]}.");
// If Accept-Charset contains a wildcard, remove the header so we default to UTF8
// This is a workaround for an EmbedIO ehh issue
string? acceptCharset = context.Request.Headers["Accept-Charset"];
if (acceptCharset != null && acceptCharset.Contains("*"))
context.Request.Headers.Remove("Accept-Charset");
// It is up to the registration how the request is eventually handled, it might even set a response here
await endPoint.InternalProcessRequest(context);

View File

@ -28,7 +28,6 @@ namespace Artemis.Core.Services
_webServerPortSetting.SettingChanged += WebServerPortSettingOnSettingChanged;
PluginsModule = new PluginsModule("/plugins");
StartWebServer();
}