passthrow raw content
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace Pilz.Net.Api;
|
namespace Pilz.Net.Api;
|
||||||
|
|
||||||
@@ -8,4 +9,5 @@ public record class ApiRequestInfo(
|
|||||||
bool IsAuthenticated,
|
bool IsAuthenticated,
|
||||||
string? AuthKey,
|
string? AuthKey,
|
||||||
string Url,
|
string Url,
|
||||||
string Method);
|
string Method,
|
||||||
|
HttpListenerContext HttpContext);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using System.Reflection;
|
|||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
|
using System.Xml;
|
||||||
using static Pilz.Net.Api.IApiServer;
|
using static Pilz.Net.Api.IApiServer;
|
||||||
|
|
||||||
namespace Pilz.Net.Api;
|
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 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;
|
public string ApiUrl { get; } = apiUrl;
|
||||||
|
|
||||||
@@ -232,7 +233,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
|||||||
|
|
||||||
// Handle message
|
// Handle message
|
||||||
Log.Debug("Handle mssage");
|
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");
|
Log.Warn("Request couldn't be handled");
|
||||||
close();
|
close();
|
||||||
@@ -245,19 +246,24 @@ public class ApiServer(string apiUrl) : IApiServer
|
|||||||
|
|
||||||
// Write response content
|
// Write response content
|
||||||
Log.Debug("Create response");
|
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";
|
context.Response.ContentType = "application/json";
|
||||||
using StreamWriter output = new(context.Response.OutputStream);
|
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");
|
Log.Debug("Finish response");
|
||||||
close();
|
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
|
// Check authentication
|
||||||
Log.Debug("Check authentication");
|
Log.Debug("Check authentication");
|
||||||
@@ -284,7 +290,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
|||||||
|
|
||||||
// Invoke handler
|
// Invoke handler
|
||||||
Log.Debug("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)
|
if (handler.Handler.DynamicInvoke(parameters) is not ApiResult result)
|
||||||
return new(ApiResult.InternalServerError(), null);
|
return new(ApiResult.InternalServerError(), null);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user