diff --git a/src/Artemis.Core/Services/WebServer/EndPoints/EndpointExceptionEventArgs.cs b/src/Artemis.Core/Services/WebServer/EndPoints/EndpointExceptionEventArgs.cs new file mode 100644 index 000000000..1e17e1a91 --- /dev/null +++ b/src/Artemis.Core/Services/WebServer/EndPoints/EndpointExceptionEventArgs.cs @@ -0,0 +1,20 @@ +using System; + +namespace Artemis.Core.Services +{ + /// + /// Provides data about endpoint exception related events + /// + public class EndpointExceptionEventArgs : EventArgs + { + internal EndpointExceptionEventArgs(Exception exception) + { + Exception = exception; + } + + /// + /// Gets the exception that occurred + /// + public Exception Exception { get; } + } +} \ No newline at end of file diff --git a/src/Artemis.Core/Services/WebServer/EndPoints/PluginEndPoint.cs b/src/Artemis.Core/Services/WebServer/EndPoints/PluginEndPoint.cs index c456ff220..504fd0eff 100644 --- a/src/Artemis.Core/Services/WebServer/EndPoints/PluginEndPoint.cs +++ b/src/Artemis.Core/Services/WebServer/EndPoints/PluginEndPoint.cs @@ -52,15 +52,37 @@ namespace Artemis.Core.Services /// public string? Returns { get; protected set; } + /// + /// Occurs whenever a request threw an unhandled exception + /// + public event EventHandler? RequestException; + /// /// Called whenever the end point has to process a request /// /// The HTTP context of the request protected abstract Task ProcessRequest(IHttpContext context); + /// + /// Invokes the event + /// + /// The exception that occurred during the request + 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) diff --git a/src/Artemis.Core/Services/WebServer/PluginsModule.cs b/src/Artemis.Core/Services/WebServer/PluginsModule.cs index 78de97e66..d7f9d49de 100644 --- a/src/Artemis.Core/Services/WebServer/PluginsModule.cs +++ b/src/Artemis.Core/Services/WebServer/PluginsModule.cs @@ -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); diff --git a/src/Artemis.Core/Services/WebServer/WebServerService.cs b/src/Artemis.Core/Services/WebServer/WebServerService.cs index 3d2724ec8..e19e6a4a9 100644 --- a/src/Artemis.Core/Services/WebServer/WebServerService.cs +++ b/src/Artemis.Core/Services/WebServer/WebServerService.cs @@ -28,7 +28,6 @@ namespace Artemis.Core.Services _webServerPortSetting.SettingChanged += WebServerPortSettingOnSettingChanged; PluginsModule = new PluginsModule("/plugins"); - StartWebServer(); }