From ddc0f33e90667825960f09fa483af38a375563be Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Thu, 12 Dec 2024 11:37:52 +0100 Subject: [PATCH] passthrow raw content --- Pilz.Net/Api/ApiRequestInfo.cs | 4 +++- Pilz.Net/Api/ApiServer.cs | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Pilz.Net/Api/ApiRequestInfo.cs b/Pilz.Net/Api/ApiRequestInfo.cs index 2a09d79..780f272 100644 --- a/Pilz.Net/Api/ApiRequestInfo.cs +++ b/Pilz.Net/Api/ApiRequestInfo.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using System.Net; namespace Pilz.Net.Api; @@ -8,4 +9,5 @@ public record class ApiRequestInfo( bool IsAuthenticated, string? AuthKey, string Url, - string Method); + string Method, + HttpListenerContext HttpContext); diff --git a/Pilz.Net/Api/ApiServer.cs b/Pilz.Net/Api/ApiServer.cs index 5ef22a4..a946618 100644 --- a/Pilz.Net/Api/ApiServer.cs +++ b/Pilz.Net/Api/ApiServer.cs @@ -8,6 +8,7 @@ using System.Reflection; using System.Text.Encodings.Web; using System.Text.RegularExpressions; using System.Web; +using System.Xml; using static Pilz.Net.Api.IApiServer; namespace Pilz.Net.Api; @@ -26,7 +27,7 @@ public class ApiServer(string apiUrl) : IApiServer protected record PrivateMessageHandler(string Url, bool UseRegEx, Delegate Handler, PrivateParameterInfo[] Parameters, ApiMessageHandlerAttribute Attribute); - protected record PrivateApiResult(ApiResult Original, string? ResultJson); + protected record PrivateApiResult(ApiResult Original, object? ResultContent); public string ApiUrl { get; } = apiUrl; @@ -232,7 +233,7 @@ public class ApiServer(string apiUrl) : IApiServer // Handle message Log.Debug("Handle mssage"); - if (HandleMessage(path, query, context.Request.HttpMethod, handler, contentJson, authKey) is not PrivateApiResult result) + if (HandleMessage(context, path, query, context.Request.HttpMethod, handler, contentJson, authKey) is not PrivateApiResult result) { Log.Warn("Request couldn't be handled"); close(); @@ -245,19 +246,24 @@ public class ApiServer(string apiUrl) : IApiServer // Write response content Log.Debug("Create response"); - if (result.ResultJson is not null) + if (result.ResultContent is string resultJson) { - Log.Info("Sending response for " + context.Request.RawUrl); + Log.Info("Sending json response for " + context.Request.RawUrl); context.Response.ContentType = "application/json"; using StreamWriter output = new(context.Response.OutputStream); - output.Write(result.ResultJson); + output.Write(resultJson); + } + else if (result.ResultContent is byte[] resultBytes) + { + Log.Info("Sending raw bytes response for " + context.Request.RawUrl); + context.Response.OutputStream.Write(resultBytes, 0, resultBytes.Length); } Log.Debug("Finish response"); close(); } - protected virtual PrivateApiResult? HandleMessage(string url, string? query, string method, PrivateMessageHandler handler, string? json, string? authKey) + protected virtual PrivateApiResult? HandleMessage(HttpListenerContext context, string url, string? query, string method, PrivateMessageHandler handler, string? json, string? authKey) { // Check authentication Log.Debug("Check authentication"); @@ -284,7 +290,7 @@ public class ApiServer(string apiUrl) : IApiServer // Invoke handler Log.Debug("Invoke handler"); - var parameters = BuildParameters(url, query, handler, () => message, () => new(message, isAuthenticated, authKeyDecoded, url, method)); + var parameters = BuildParameters(url, query, handler, () => message, () => new(message, isAuthenticated, authKeyDecoded, url, method, context)); if (handler.Handler.DynamicInvoke(parameters) is not ApiResult result) return new(ApiResult.InternalServerError(), null);