public HttpServer(int port) { this.port = port; this.listener = new HttpListener(); this.handlers = new Dictionary <string, HttpHandlerProxy>(); this.defaultHandler = null; this.authblocked = false; }
public void AddDefaultHandler(IHttpHandler handler) { if (handler == null) { throw new ArgumentNullException(); } defaultHandler = new HttpHandlerProxy(handler); }
private void ConnectionAccepted(IAsyncResult result) { HttpListenerContext context = null; try { context = listener.EndGetContext(result); long beginTime = System.Diagnostics.Stopwatch.GetTimestamp(); listener.BeginGetContext(this.ConnectionAccepted, null); if (context != null) { if (authblocked) { respond401Unauthorized(context); } else { HttpHandlerProxy handler = FindBestMatch( context.Request.Url.AbsolutePath); if (handler != null) { TaskQueue.EnqueueHigh(handler.Do, new HttpData(context, beginTime)); } else { respond404NotFound(context); } } } } catch (ObjectDisposedException) { logger.Info("Http server disposed. Shutdown server"); } catch (Exception e) { logger.Info("Shutdown server", e); return; } }