From ef1b6bad2e11e16459efcb81a73098118447df37 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Wed, 22 Oct 2025 07:13:19 +0200 Subject: [PATCH] api: bad request on fail --- Pilz.Net/Api/ApiServer.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Pilz.Net/Api/ApiServer.cs b/Pilz.Net/Api/ApiServer.cs index 739565f..a7681c0 100644 --- a/Pilz.Net/Api/ApiServer.cs +++ b/Pilz.Net/Api/ApiServer.cs @@ -349,7 +349,7 @@ public class ApiServer : IApiServer } catch (MissingDataManagerException mdmex) { - Log.Error("DataManager is not supported by this server instnace!", mdmex); + Log.Error("DataManager is not supported by this server instance!", mdmex); if (DebugMode) throw; } catch (Exception ex) @@ -387,9 +387,11 @@ public class ApiServer : IApiServer { Log.Debug("Start handling request"); - void close() + void close(bool badRequest) { Log.Debug("End handling request"); + if (badRequest) + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; context.Response.OutputStream.Close(); } @@ -400,7 +402,7 @@ public class ApiServer : IApiServer if (string.IsNullOrWhiteSpace(path)) { Log.Warn("Request has no path"); - close(); + close(true); return; } @@ -409,7 +411,7 @@ public class ApiServer : IApiServer if (!TryGetHandler(path, query, context.Request.HttpMethod, out var handler)) { Log.Warn("Request handler couldn't be found"); - close(); + close(true); return; } @@ -433,13 +435,13 @@ public class ApiServer : IApiServer catch (OutOfMemoryException) { Log.Error("Error reading remote data due to missing memory"); - close(); + close(true); return; } catch (Exception ex) { Log.Error("Error reading remote data", ex); - close(); + close(true); return; } } @@ -451,7 +453,7 @@ public class ApiServer : IApiServer if (HandleMessage(context, path, query, handler, contentJson, authKey) is not PrivateApiResult result) { Log.Warn("Request couldn't be handled"); - close(); + close(true); return; } @@ -482,7 +484,8 @@ public class ApiServer : IApiServer } Log.Debug("Finish response"); - close(); + close(false); + return; } protected virtual PrivateApiResult? HandleMessage(HttpListenerContext context, string url, string? query, PrivateMessageHandler handler, string? json, string? authKey)