support raw messages on apiclient->apiserver
This commit is contained in:
@@ -436,37 +436,9 @@ public class ApiServer : IApiServer
|
||||
if (context.Request.Headers.Get("API-AUTH-KEY") is not string authKey)
|
||||
authKey = null!;
|
||||
|
||||
// Read input content
|
||||
Log.Debug("Read input content");
|
||||
string? contentJson;
|
||||
if (context.Request.ContentType is string contentType
|
||||
&& contentType.Contains("application/json")
|
||||
&& context.Request.ContentLength64 > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
using StreamReader input = new(context.Request.InputStream);
|
||||
contentJson = input.ReadToEnd();
|
||||
}
|
||||
catch (OutOfMemoryException)
|
||||
{
|
||||
Log.Error("Error reading remote data due to missing memory");
|
||||
close(true);
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error("Error reading remote data", ex);
|
||||
close(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
contentJson = null;
|
||||
|
||||
// Handle message
|
||||
Log.Debug("Handle mssage");
|
||||
if (HandleMessage(context, path, query, handler, contentJson, authKey) is not PrivateApiResult result)
|
||||
if (HandleMessage(context, path, query, handler, authKey) is not PrivateApiResult result)
|
||||
{
|
||||
Log.Warn("Request couldn't be handled");
|
||||
close(true);
|
||||
@@ -514,7 +486,7 @@ public class ApiServer : IApiServer
|
||||
return;
|
||||
}
|
||||
|
||||
protected virtual PrivateApiResult? HandleMessage(HttpListenerContext context, string url, string? query, PrivateMessageHandler handler, string? json, string? authKey)
|
||||
protected virtual PrivateApiResult? HandleMessage(HttpListenerContext context, string url, string? query, PrivateMessageHandler handler, string? authKey)
|
||||
{
|
||||
// Check authentication
|
||||
Log.Debug("Check authentication");
|
||||
@@ -530,13 +502,46 @@ public class ApiServer : IApiServer
|
||||
var targetType = handler.Handler.Method.GetParameters().FirstOrDefault(p => p.ParameterType.IsAssignableTo(typeof(ApiMessage)))?.ParameterType;
|
||||
var serializer = GetSerializer(handler.Attribute.Serializer);
|
||||
|
||||
// Deserialize
|
||||
Log.Debug("Deserialize message");
|
||||
ApiMessage? message;
|
||||
if (json != null && targetType != null)
|
||||
message = serializer.Deserialize(json, targetType);
|
||||
else
|
||||
message = null;
|
||||
// Read input content
|
||||
Log.Debug("Read input content");
|
||||
ApiMessage? message = null;
|
||||
switch (context.Request.ContentType)
|
||||
{
|
||||
case "application/json":
|
||||
try
|
||||
{
|
||||
Log.Debug("Deserialize message");
|
||||
using StreamReader input = new(context.Request.InputStream);
|
||||
var contentJson = input.ReadToEnd();
|
||||
if (targetType == null)
|
||||
return null;
|
||||
message = serializer.Deserialize(contentJson, targetType);
|
||||
}
|
||||
catch (OutOfMemoryException)
|
||||
{
|
||||
Log.Error("Error reading remote data due to missing memory");
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error("Error reading remote data", ex);
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
case "application/octet-stream":
|
||||
Log.Debug("Process raw message");
|
||||
if (targetType == null)
|
||||
return null;
|
||||
else if (targetType.IsAssignableTo(typeof(ApiRawByteMessage)))
|
||||
{
|
||||
var bytes = new byte[context.Request.ContentLength64];
|
||||
context.Request.InputStream.Read(bytes, 0, bytes.Length);
|
||||
message = new ApiRawByteMessage(bytes);
|
||||
}
|
||||
else if (targetType.IsAssignableTo(typeof(ApiRawStreamMessage)))
|
||||
message = new ApiRawInputStreamMessage(context.Request.InputStream, context.Request.ContentLength64);
|
||||
break;
|
||||
}
|
||||
|
||||
// Invoke handler
|
||||
Log.Debug("Invoke handler");
|
||||
|
||||
Reference in New Issue
Block a user