better error handling in HTTP server
This commit is contained in:
parent
198b04c2e3
commit
8992065a10
1 changed files with 51 additions and 30 deletions
|
@ -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}");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue