private void HttpWebListenerCallback(IAsyncResult result)
{
HttpListener listener = result.AsyncState as HttpListener;
HttpListenerContext context;
try{
context = listener.EndGetContext(result);
} catch(Exception){
// HttpListener.Close() fires the callback for the final time for whatever reason.
// Just handle it gracefully.
return;
}
if (Debug)
{
Console.WriteLine(DateTime.Now.ToString() + "Handling request");
Console.WriteLine("\tRequest url - " + context.Request.Url);
Console.WriteLine("\tHost address - " + context.Request.UserHostAddress);
Console.WriteLine("\tServlet type - " + this.GetType().FullName);
}
try
{
HandleRequest(context);
}
catch (Exception e)
{
string exceptionText = e.ToString();
if (Debug)
{
Console.WriteLine("Exception encountered: " + exceptionText);
}
if (RethrowExceptions)
{
throw;
}
WriteResponse(context.Response, exceptionText);
}
try
{
context.Response.OutputStream.Close();
}
catch { }
}