passthrow raw content

This commit is contained in:
Pilzinsel64
2024-12-12 11:37:52 +01:00
parent 2e68e6802b
commit ddc0f33e90
2 changed files with 16 additions and 8 deletions

View File

@@ -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);

View File

@@ -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);