Files
Pilz/Pilz.Net/Api/ApiServer.cs
2024-08-16 09:19:03 +02:00

235 lines
7.6 KiB
C#

using Castle.Core.Logging;
using Pilz.Extensions.Reflection;
using System.Net;
using System.Reflection;
using static Pilz.Net.Api.IApiServer;
namespace Pilz.Net.Api;
public class ApiServer(string apiUrl) : IApiServer
{
protected readonly Dictionary<string, Delegate> handlers = [];
protected readonly Dictionary<Type, IMessageSerializer> serializers = [];
protected readonly HttpListener httpListener = new();
public event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
protected record PrivateApiResult(ApiResult Original, string? ResultJson);
public string ApiUrl { get; } = apiUrl;
public virtual bool EnableAuth { get; set; }
public IMessageSerializer Serializer { get; set; } = new DefaultMessageSerializer();
public ILogger Log { get; set; } = NullLogger.Instance;
public virtual void Start()
{
Log.Info("Start listening");
httpListener.Start();
Listen();
}
public virtual void Stop()
{
httpListener.Stop();
Log.Info("Stopped listening");
}
public virtual void RegisterHandler<T>(T instance) where T : class
{
// Get all public instance methods
var methods = typeof(T).GetMethods(BindingFlags.Instance | BindingFlags.Public);
// Register each method
foreach (var method in methods)
RegisterHandler(method.CreateDelegate(instance));
}
public virtual void RegisterHandler(Delegate handler)
{
var method = handler.Method;
// Sanity checks
if (method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute
|| method.GetParameters().FirstOrDefault() is not ParameterInfo parameter
|| !parameter.ParameterType.IsGenericType
|| parameter.ParameterType.GenericTypeArguments.Length != 1
|| !parameter.ParameterType.GenericTypeArguments[0].IsAssignableTo(typeof(ApiMessage))
|| !method.ReturnType.IsAssignableTo(typeof(ApiResult)))
throw new NotSupportedException("The first parameter needs to be of type ApiMessage and must return an ApiResult object and the method must have the MessageHandlerAttribute.");
// Add handler
var fullUrl = ApiUrl + attribute.Route;
Log.InfoFormat("Added handler for {0}", fullUrl);
handlers.Add(fullUrl, handler);
}
protected virtual void Listen()
{
while (httpListener.IsListening)
CheckContext();
}
protected virtual void CheckContext()
{
var context = httpListener.GetContext();
void close()
{
Log.Info("End handling request.");
context.Response.OutputStream.Close();
}
Log.Debug("Sanity checks");
if (context.Request.HttpMethod != HttpMethod.Post.Method
|| context.Request.ContentType is not string contentType
|| !contentType.Contains("application/json"))
{
Log.Info("Request has no json content");
close();
return;
}
// Parse url
Log.Debug("Parse url");
var path = context.Request.Url?.PathAndQuery.Replace(ApiUrl, string.Empty);
if (string.IsNullOrWhiteSpace(path) || context.Request.ContentLength64 <= 0)
{
Log.Info("Request has no content");
close();
return;
}
// Read input content
Log.Debug("Read input content");
using StreamReader input = new(context.Request.InputStream);
var contentJson = input.ReadToEnd();
// Get auth key
Log.Debug("Get auth key");
if (context.Request.Headers.Get("API-AUTH-KEY") is not string authKey)
authKey = null!;
// Handle message
Log.Debug("Handle mssage");
if (HandleMessage(path, contentJson, authKey) is not PrivateApiResult result)
{
Log.Warn("Request couldn't be handled");
close();
return;
}
// Set response parameters
Log.Debug("Set response parameters");
context.Response.StatusCode = (int)result.Original.StatusCode;
// Write response content
Log.Debug("Write response");
if (result.ResultJson is not null)
{
Log.Info("Write response");
context.Response.ContentType = "application/json";
using StreamWriter output = new(context.Response.OutputStream);
output.Write(result);
}
Log.Debug("Finish response");
close();
}
protected virtual PrivateApiResult? HandleMessage(string url, string json, string? authKey)
{
// Get handler
Log.Debug("Find handler");
if (!handlers.TryGetValue(url, out var handler)
|| handler.Method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute)
return null;
// Check authentication
Log.Debug("Check authentication");
var isAuthenticated = false;
if (!string.IsNullOrWhiteSpace(authKey) && DecodeAuthKey(authKey) is string authKeyDecoded)
isAuthenticated = CheckAuthentication(authKeyDecoded);
else
authKeyDecoded = null!;
// Get required infos
Log.Debug("Find other infos");
var targetType = handler.Method.GetParameters().First().ParameterType;
var serializer = GetSerializer(attribute.Serializer);
// Deserialize
Log.Debug("Deserialize message");
if (serializer.Deserialize(json, targetType) is not ApiMessage message)
return null;
// Invoke handler
Log.Debug("Invoke handler");
var parameters = BuildParameters(handler, () => message, () => new(message, isAuthenticated, authKeyDecoded));
if (handler.DynamicInvoke(parameters) is not ApiResult result)
return null;
// Return result without message
Log.Debug("Check message");
if (result.Message is null)
return new(result, null);
// Serializer
Log.Debug("Serialize message");
if (serializer.Serialize(result.Message) is not string resultStr)
return null;
// Return result with message
Log.Debug("Finish result");
return new(result, resultStr);
}
protected virtual object?[]? BuildParameters(Delegate handler, Func<ApiMessage> getMessage, Func<ApiRequestInfo> getRequestInfo)
{
var infos = handler.Method.GetParameters();
var objs = new List<object?>();
foreach (var info in infos)
{
if (info.ParameterType.IsAssignableTo(typeof(ApiMessage)))
objs.Add(getMessage());
else if (info.ParameterType.IsAssignableTo(typeof(ApiRequestInfo)))
objs.Add(getRequestInfo());
}
return [.. objs];
}
protected virtual IMessageSerializer GetSerializer(Type? t)
{
if (t is not null)
{
if (serializers.TryGetValue(t, out var s) && s is not null)
return s;
else if (Activator.CreateInstance(t) is IMessageSerializer ss)
{
serializers.Add(t, ss);
return ss;
}
}
return Serializer;
}
protected virtual bool CheckAuthentication(string authKey)
{
if (OnCheckAuthentication != null)
{
var args = new ApiAuthCheckEventArgs(authKey);
OnCheckAuthentication?.Invoke(this, args);
return args.Valid;
}
return false;
}
protected virtual string? DecodeAuthKey(string authKey)
{
return authKey;
}
}