private void button2_Click(object sender, EventArgs e) { if (!isActivated) { Thread wsThread = new Thread(WebScriptManager.RunExecutionQueue); wsThread.IsBackground = true; wsThread.Start(); Thread servThread = new Thread(() => { HttpExchange.Run(); }); servThread.IsBackground = true; servThread.Start(); serverThread = servThread; try { IDEServer = new SimpleWebServer.WebServer(Utils.HandleIDE, "http://localhost:" + ConfigurationManager.SETTINGS["IDE_PORT"] + "/"); IDEServer.Run(); } catch { MessageBox.Show("Failed to initialize IDE server!"); } isActivated = true; button2.Text = "Stop server"; } else { try { IDEServer.Stop(); } catch { MessageBox.Show("Failed to stop IDE server!"); } WebScriptManager.StopExecutionQueue(); try { HttpExchange._server.Close(); serverThread.Abort(); serverThread.Interrupt(); } catch { MessageBox.Show("LWASP was unable to stop the server!\r\nPlease try again or restart LWASP"); } isActivated = false; button2.Text = "Start server"; } }
void ProcessRequest() { try { if (ConfigurationManager.SETTINGS["REGEX_URL"].ToUpper().Contains("FALSE")) { requestedResource = ResourceLoader.GetFileByURL(context); if (requestedResource == null) { context.Response.Headers.Add("Content-Type", "text/html; charset=utf-8"); oWrite("<h1>404 Shayse!</h1>We couldn't find the resource for you"); oClose(); } else if (ConfigurationManager.SETTINGS["WEBSCRIPT_EXTENSION"].ToUpper().Contains(Path.GetExtension(requestedResource).ToUpper())) { inputStream = context.Request.InputStream; requestCookies = context.Request.Cookies; responseCookies = new CookieCollection(); queryString = ProcessQueryString(context.Request.RawUrl); formData = context.Request.HttpMethod.ToUpper() == "POST" && context.Request.ContentType.Contains("application/x-www-form-urlencoded") ? ProcessFormData(context.Request.InputStream) : new Dictionary <string, string>(); requestHeaders = new Dictionary <string, string>(); foreach (string k in context.Request.Headers.AllKeys) { requestHeaders[k] = context.Request.Headers[k]; } responseHeaders = new Dictionary <string, string>() { { "Content-Type", "text/html; charset=utf-8" }, { "Connection", "close" } }; HttpMethod = context.Request.HttpMethod; HttpURL = context.Request.Url; HttpVersion = context.Request.ProtocolVersion.ToString(); WebScriptManager.Enqueue(this); // Our script request was sent to the configuration manager for further care } else { // Our static file request will be immediately handeled byte[] buf = File.ReadAllBytes(requestedResource); string type = Mime.GetMimeType(Path.GetExtension(requestedResource)); if (type.Contains("text")) { context.Response.Headers.Add("Content-Type", type + "; charset=utf-8"); } else { context.Response.Headers.Add("Content-Type", type); } context.Response.ContentLength64 += buf.Length; context.Response.OutputStream.Write(buf, 0, buf.Length); oClose(); } } else { //Not implemented! } } catch (Exception e) { context.Response.Headers.Add("Content-Type", "text/html; charset=utf-8"); oWrite("<h1>500 Internal Server Error</h1><br/>Server error message was: " + e.Message); oClose(); } }