From 8992065a10dfde1d39669d0d06c792d60fc89969 Mon Sep 17 00:00:00 2001 From: Elijah Date: Tue, 2 Jul 2024 20:48:40 -0400 Subject: [PATCH] better error handling in HTTP server --- SAPIServer/HTTPServer.cs | 81 +++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/SAPIServer/HTTPServer.cs b/SAPIServer/HTTPServer.cs index d67361c..654856a 100644 --- a/SAPIServer/HTTPServer.cs +++ b/SAPIServer/HTTPServer.cs @@ -48,44 +48,65 @@ namespace SAPIServer var context = listener.GetContext(); ThreadPool.QueueUserWorkItem(_ => { - Dictionary> handlerDict; - // TODO: Make query params parsable var uri = context.Request.RawUrl.Split('?')[0]; - byte[] response; - switch (context.Request.HttpMethod) + var ip = context.Request.RemoteEndPoint.Address; + try { - case "GET": + Dictionary> handlerDict; + // TODO: Make query params parsable + byte[] response; + switch (context.Request.HttpMethod) + { + case "GET": + { + handlerDict = GETListeners; + break; + } + case "POST": + { + handlerDict = POSTListeners; + break; + } + default: + { + response = Encoding.UTF8.GetBytes($"Method not allowed: {context.Request.HttpMethod}"); + context.Response.StatusCode = 405; + context.Response.ContentType = "text/plain"; + context.Response.ContentLength64 = response.Length; + context.Response.OutputStream.Write(response, 0, response.Length); + context.Response.OutputStream.Close(); + return; + } + } + if (!handlerDict.TryGetValue(uri, out var handler)) + { + response = Encoding.UTF8.GetBytes($"No route defined for {uri}"); + context.Response.StatusCode = 404; + context.Response.ContentType = "text/plain"; + } + else + { + try { - handlerDict = GETListeners; - break; - } - case "POST": + response = handler(context); + } catch (Exception e) { - handlerDict = POSTListeners; - break; - } - default: - { - response = Encoding.UTF8.GetBytes($"Method not allowed: {context.Request.HttpMethod}"); - context.Response.StatusCode = 405; + response = Encoding.UTF8.GetBytes("Internal Server Error"); + context.Response.StatusCode = 500; context.Response.ContentType = "text/plain"; - context.Response.ContentLength64 = response.Length; - context.Response.OutputStream.Write(response, 0, response.Length); - context.Response.OutputStream.Close(); - return; + Console.Error.WriteLine($"[{DateTime.Now:u}] {ip} - {context.Request.HttpMethod} {context.Request.RawUrl} - Handler Failed: {e.Message}"); } - } - if (!handlerDict.TryGetValue(uri, out var handler)) + } + + context.Response.ContentLength64 = response.Length; + context.Response.OutputStream.Write(response, 0, response.Length); + context.Response.OutputStream.Close(); + Console.WriteLine($"[{DateTime.Now:u}] {ip} - {context.Request.HttpMethod} {context.Request.RawUrl} - {context.Response.StatusCode}"); + } catch (Exception e) { - response = Encoding.UTF8.GetBytes($"No route defined for {uri}"); - context.Response.StatusCode = 404; - context.Response.ContentType = "text/plain"; + Console.Error.WriteLine($"[{DateTime.Now:u}] {ip} - {context.Request.HttpMethod} {context.Request.RawUrl} - Exception: {e.Message}"); } - else response = handler(context); - context.Response.ContentLength64 = response.Length; - context.Response.OutputStream.Write(response, 0, response.Length); - context.Response.OutputStream.Close(); - Console.WriteLine($"[{DateTime.Now:u}] {context.Request.RemoteEndPoint.Address} - {context.Request.HttpMethod} {context.Request.RawUrl} - {context.Response.StatusCode}"); + }); } }