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();
ThreadPool.QueueUserWorkItem(_ =>
{
Dictionary<string, Func<HttpListenerContext, byte[]>> 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<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;
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}");
});
}
}