better error handling in HTTP server

This commit is contained in:
Elijah 2024-07-02 20:48:40 -04:00
parent 198b04c2e3
commit 8992065a10

View file

@ -48,44 +48,65 @@ namespace SAPIServer
var context = listener.GetContext(); var context = listener.GetContext();
ThreadPool.QueueUserWorkItem(_ => ThreadPool.QueueUserWorkItem(_ =>
{ {
Dictionary<string, Func<HttpListenerContext, byte[]>> handlerDict;
// TODO: Make query params parsable
var uri = context.Request.RawUrl.Split('?')[0]; var uri = context.Request.RawUrl.Split('?')[0];
byte[] response; var ip = context.Request.RemoteEndPoint.Address;
switch (context.Request.HttpMethod) try
{ {
case "GET": Dictionary<string, Func<HttpListenerContext, byte[]>> 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; response = handler(context);
break; } catch (Exception e)
}
case "POST":
{ {
handlerDict = POSTListeners; response = Encoding.UTF8.GetBytes("Internal Server Error");
break; context.Response.StatusCode = 500;
}
default:
{
response = Encoding.UTF8.GetBytes($"Method not allowed: {context.Request.HttpMethod}");
context.Response.StatusCode = 405;
context.Response.ContentType = "text/plain"; context.Response.ContentType = "text/plain";
context.Response.ContentLength64 = response.Length; Console.Error.WriteLine($"[{DateTime.Now:u}] {ip} - {context.Request.HttpMethod} {context.Request.RawUrl} - Handler Failed: {e.Message}");
context.Response.OutputStream.Write(response, 0, response.Length);
context.Response.OutputStream.Close();
return;
} }
} }
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}"); Console.Error.WriteLine($"[{DateTime.Now:u}] {ip} - {context.Request.HttpMethod} {context.Request.RawUrl} - Exception: {e.Message}");
context.Response.StatusCode = 404;
context.Response.ContentType = "text/plain";
} }
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}");
}); });
} }
} }