passthrow raw content
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user