private void Start() { if (this.root.EndsWith(@"\", StringComparison.Ordinal)) { this.root = StringHelper.TrimEnd(this.root); } if (!Directory.Exists(this.root)) { Directory.CreateDirectory(this.root); } this.Connections = new List <HttpConnection>(); this.Sessions = new Dictionary <string, HttpSession>(); this.Cache = new HttpCache(this); HttpController.LoadControllers(); HttpWebSocket.LoadControllers(); this.connectionWaitHandle = new AutoResetEvent(false); this.listenerLoopThread = new Thread(this.ListenerLoop); this.listenerLoopThread.Name = "HttpServer Listener"; this.listenerLoopThread.IsBackground = true; this.listenerLoopThread.Start(); }
private static void OnDisconnectedClient(HttpWebSocket client) { try { ClientDisconnectedHandler handler; if (null != (handler = (ClientDisconnectedHandler)clientDisconnected)) { handler(client); } } catch { } }
private static void OnNewClient(HttpWebSocket client) { try { ConnectedClientHandler handler; if (null != (handler = (ConnectedClientHandler)clientConnected)) { handler(client); } } catch { client.Open = false; } }
public HttpResponse(HttpRequest request) { this.request = request; const string PDF_IDENT = "-pdf"; try { if (this.request.Path == @"") { if (HttpController.Primary == null) { throw new ApplicationException("Primary Controller Not Defined"); } if (HttpController.Primary.PrimaryAction == null) { throw new ApplicationException("Primary Action Not Defined"); } this.content = HttpController.RequestPrimary(this.request); } else if (this.request.Path.ToLower().EndsWith(PDF_IDENT, StringComparison.CurrentCulture)) { string url = "http://localhost:" + request.Server.Port.ToString() + this.request.FullUrl; url = url.Replace(this.request.Path, StringHelper.TrimEnd(this.request.Path, PDF_IDENT.Length)); this.content = new HttpContent(HtmlToPdfHelper.ReadPDF(url), "application/pdf"); } else if (this.request.IsWebSocket && HttpWebSocket.WebSocketControllerExists(this.request)) { var type = HttpWebSocket.GetWebSocketController(this.request); this.request.WebSocket = (HttpWebSocket)Activator.CreateInstance(type, this.request); } else if (HttpController.ActionExists(this.request)) { this.content = HttpController.RequestAction(this.request); } else { this.content = HttpContent.Read(this.request.Server, this.request.Path); } if (this.content is HttpErrorContent) { this.bytes = this.ConstructResponse(HttpStatusCode.SERVERERROR); } else if (this.request.IsWebSocket) { this.bytes = this.ConstructResponse(HttpStatusCode.SWITCHING_PROTOCOLS); } else if (this.content.ETag == this.request.ETag && !this.content.ParsingRequired) { this.bytes = this.ConstructResponse(HttpStatusCode.NOT_MODIFIED); } else { this.bytes = this.ConstructResponse(HttpStatusCode.OK); } } catch (FileNotFoundException ex) { Logger.Log(ex); this.content = ErrorController.Display(this.request, ex, HttpStatusCode.NOTFOUND); this.bytes = this.ConstructResponse(HttpStatusCode.SERVERERROR); } catch (Exception ex) { Logger.Log(ex); this.content = ErrorController.Display(this.request, ex, HttpStatusCode.SERVERERROR); this.bytes = this.ConstructResponse(HttpStatusCode.SERVERERROR); } }