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:
parent
291eabd20e
commit
a9d6b17aa4
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -52,16 +52,38 @@ namespace Artemis.Core.Services
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string? Returns { get; protected set; }
|
public string? Returns { get; protected set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Occurs whenever a request threw an unhandled exception
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<EndpointExceptionEventArgs>? RequestException;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called whenever the end point has to process a request
|
/// Called whenever the end point has to process a request
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context">The HTTP context of the request</param>
|
/// <param name="context">The HTTP context of the request</param>
|
||||||
protected abstract Task ProcessRequest(IHttpContext context);
|
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)
|
internal async Task InternalProcessRequest(IHttpContext context)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
await ProcessRequest(context);
|
await ProcessRequest(context);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
OnRequestException(e);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void OnDisabled(object? sender, EventArgs e)
|
private void OnDisabled(object? sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,10 +1,7 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using EmbedIO;
|
using EmbedIO;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Artemis.Core.Services
|
namespace Artemis.Core.Services
|
||||||
{
|
{
|
||||||
@ -67,6 +64,12 @@ namespace Artemis.Core.Services
|
|||||||
if (!endPoints.TryGetValue(pathParts[1], out PluginEndPoint? endPoint))
|
if (!endPoints.TryGetValue(pathParts[1], out PluginEndPoint? endPoint))
|
||||||
throw HttpException.NotFound($"Found no endpoint called {pathParts[1]} for plugin with ID {pathParts[0]}.");
|
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
|
// It is up to the registration how the request is eventually handled, it might even set a response here
|
||||||
await endPoint.InternalProcessRequest(context);
|
await endPoint.InternalProcessRequest(context);
|
||||||
|
|
||||||
|
|||||||
@ -28,7 +28,6 @@ namespace Artemis.Core.Services
|
|||||||
_webServerPortSetting.SettingChanged += WebServerPortSettingOnSettingChanged;
|
_webServerPortSetting.SettingChanged += WebServerPortSettingOnSettingChanged;
|
||||||
|
|
||||||
PluginsModule = new PluginsModule("/plugins");
|
PluginsModule = new PluginsModule("/plugins");
|
||||||
|
|
||||||
StartWebServer();
|
StartWebServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user