From 6c60d271ade791e0aae44466a6377482b06c56a7 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Fri, 6 Dec 2024 11:52:57 +0100 Subject: [PATCH] support queries --- Pilz.Net/Api/ApiServer.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Pilz.Net/Api/ApiServer.cs b/Pilz.Net/Api/ApiServer.cs index 522080b..3b68918 100644 --- a/Pilz.Net/Api/ApiServer.cs +++ b/Pilz.Net/Api/ApiServer.cs @@ -178,7 +178,8 @@ public class ApiServer(string apiUrl) : IApiServer // Parse url Log.Debug("Parse url"); - var path = context.Request.Url?.PathAndQuery.Replace(ApiUrl, string.Empty); + var path = context.Request.Url?.AbsolutePath; + var query = context.Request.Url?.Query; if (string.IsNullOrWhiteSpace(path)) { Log.Info("Request has no path"); @@ -188,7 +189,7 @@ public class ApiServer(string apiUrl) : IApiServer // Find handler Log.Debug("Find handler"); - if (!TryGetHandler(path, context.Request.HttpMethod, out var handler)) + if (!TryGetHandler(path, query, context.Request.HttpMethod, out var handler)) { Log.Info("Request handler couldn't be found"); close(); @@ -230,7 +231,7 @@ public class ApiServer(string apiUrl) : IApiServer // Handle message Log.Debug("Handle mssage"); - if (HandleMessage(path, context.Request.HttpMethod, handler, contentJson, authKey) is not PrivateApiResult result) + if (HandleMessage(path, query, context.Request.HttpMethod, handler, contentJson, authKey) is not PrivateApiResult result) { Log.Warn("Request couldn't be handled"); close(); @@ -255,7 +256,7 @@ public class ApiServer(string apiUrl) : IApiServer close(); } - protected virtual PrivateApiResult? HandleMessage(string url, string method, PrivateMessageHandler handler, string? json, string? authKey) + protected virtual PrivateApiResult? HandleMessage(string url, string? query, string method, PrivateMessageHandler handler, string? json, string? authKey) { // Check authentication Log.Debug("Check authentication"); @@ -282,7 +283,7 @@ public class ApiServer(string apiUrl) : IApiServer // Invoke handler Log.Debug("Invoke handler"); - var parameters = BuildParameters(url, handler, () => message, () => new(message, isAuthenticated, authKeyDecoded, url, method)); + var parameters = BuildParameters(url, query, handler, () => message, () => new(message, isAuthenticated, authKeyDecoded, url, method)); if (handler.Handler.DynamicInvoke(parameters) is not ApiResult result) return new(ApiResult.InternalServerError(), null); @@ -301,7 +302,7 @@ public class ApiServer(string apiUrl) : IApiServer return new(result, resultStr); } - protected virtual bool TryGetHandler(string url, string method, [NotNullWhen(true)] out PrivateMessageHandler? handler) + protected virtual bool TryGetHandler(string url, string? query, string method, [NotNullWhen(true)] out PrivateMessageHandler? handler) { handler = handlers.FirstOrDefault(handler => { @@ -317,10 +318,11 @@ public class ApiServer(string apiUrl) : IApiServer return handler != null; } - protected virtual object?[]? BuildParameters(string url, PrivateMessageHandler handler, Func getMessage, Func getRequestInfo) + protected virtual object?[]? BuildParameters(string url, string? query, PrivateMessageHandler handler, Func getMessage, Func getRequestInfo) { var infos = handler.Handler.Method.GetParameters(); var objs = new List(); + var queryparams = query == null ? [] : HttpUtility.ParseQueryString(query); foreach (var info in infos) { @@ -331,6 +333,8 @@ public class ApiServer(string apiUrl) : IApiServer else if (handler.Parameters.FirstOrDefault(p => p.Name.Equals(info.Name, StringComparison.InvariantCultureIgnoreCase)) is PrivateParameterInfo parameterInfo && url.Split('/').ElementAtOrDefault(parameterInfo.Index) is string parameterValue) objs.Add(Convert.ChangeType(HttpUtility.UrlDecode(parameterValue), info.ParameterType)); // or Uri.UnescapeDataString(); maybe run this line twice? + else if (queryparams.AllKeys.FirstOrDefault(n => n != null && n.Equals(info.Name, StringComparison.InvariantCultureIgnoreCase)) is string querykey) + objs.Add(Convert.ChangeType(HttpUtility.HtmlDecode(queryparams.Get(querykey)), info.ParameterType)); else objs.Add(null); }