private ResponseCode _handleGet(HttpListenerRequest req, HttpListenerResponse resp, string accesslog) { string html = "<html>"; html += "<head><style>"; html += ".indent {margin-left: 1.5em;}"; html += "</style>"; html += "<script>"; html += "function ready() {\n"; html += "var radios = document.querySelectorAll('input[type=radio][name=testmode]');\n"; html += "function changeHandler(event) {\n"; html += "var newValue = Number(event.target.value);"; html += "var xhttp = new XMLHttpRequest();"; html += "xhttp.onreadystatechange = function() {"; html += "if (this.readyState == 4 && this.status == 200) {"; html += "console.log('settings changed');"; html += "}};\n"; html += "xhttp.open(\"POST\", \"/settings\", true);"; html += "xhttp.setRequestHeader( 'Content-Type', 'application/json' );\n"; html += "var formData = {}; formData.testingMode = newValue;"; html += "xhttp.send(JSON.stringify(formData));\n"; html += "}\n"; html += "Array.prototype.forEach.call(radios, function(radio) {"; html += "radio.addEventListener('change', changeHandler);"; html += "});}"; html += "function removeDomain(domain, idx) {"; html += "var xhttp = new XMLHttpRequest();"; html += "xhttp.onreadystatechange = function() {"; html += "if (this.readyState == 4 && this.status == 200) {"; html += "var domainEl = document.getElementById(\"domain\" + idx);"; html += "document.getElementById(\"domainlist\").removeChild(domainEl);"; html += "}};"; html += "xhttp.open(\"POST\", \"/permissions\", true);"; html += "xhttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );"; html += "var formData = 'host=' + encodeURIComponent(domain) + '&status=remove';"; html += "xhttp.send(formData);"; html += "}</script>"; html += "</head>"; html += "<body>"; html += "<h1>" + System.Environment.MachineName + "\\" + Environment.UserName + "</h1>"; html += "<h2>Listen Port</h2>"; html += "<p class=indent>" + server.config.port + "</p>"; html += "<h2>Testing Mode</h2>"; html += "<p class=indent>"; html += "<input type=radio id=printer name=testmode value=0"; if (server.config.testingMode == 0) { html += " checked"; } html += "><label for=printer> Printer Only </label>"; html += "<input type=radio id=file name=testmode value=1"; if (server.config.testingMode == 1) { html += " checked"; } html += "><label for=file> File Only</label>"; html += "<input type=radio id=both name=testmode value=2"; if (server.config.testingMode == 2) { html += " checked"; } html += "><label for=both> Both</label>"; html += "</p>"; html += "<h2>Printers</h2>"; html += "<div class=indent>"; html += "<ul>"; foreach (string printer in ServerConfig.listPrinters()) { html += "<li>" + printer + "</li>"; } html += "</ul>"; html += "</div>"; html += "<h2>Allowed Domains</h2>"; html += "<div class=indent>"; html += "<ul id=domainlist>"; int ctr = 0; foreach (string domain in server.config.allowedDomains) { html += "<li id=domain" + ctr + ">" + domain + " <button onclick=\"removeDomain('" + domain + "', " + ctr + ");\">Remove</button></li>"; ctr++; } html += "</ul>"; html += "</div>"; html += "<script>"; html += "(function() { ready(); })();"; html += "</script>"; html += "</body>"; html += "</html>"; ServerConfig.appendLog(accesslog); server.responseHTML(resp, html); return(ResponseCode.OK); }
private ResponseCode _handlePost(HttpListenerRequest req, HttpListenerResponse resp, string accesslog, string origin) { if (origin != server.config.URL) { accesslog += "\tfailed\torigin not " + server.config.URL; ServerConfig.appendLog(accesslog); return(ResponseCode.Forbidden); } if (!req.HasEntityBody) { accesslog += "\tfailed\tbody required"; ServerConfig.appendLog(accesslog); return(ResponseCode.NotFound); } try { using (Stream body = req.InputStream) { Encoding encoding = req.ContentEncoding; using (StreamReader reader = new StreamReader(body, encoding)) { string host = null; string status = null; string[] keyvalues = reader.ReadLine().Split('&'); foreach (string query in keyvalues) { string[] keyvalue = query.Split('='); if (keyvalue.Length > 0) { if (keyvalue[0].ToLower() == "host") { host = Uri.UnescapeDataString(keyvalue[1]); } else if (keyvalue[0].ToLower() == "status") { status = Uri.UnescapeDataString(keyvalue[1]); } } } body.Close(); reader.Close(); string html = "<html><body>"; if (host != null) { accesslog += "\tsuccess"; if (status == "allow") { if (!server.config.allowedDomains.Contains(host)) { server.config.allowedDomains.Add(host); server.config.save(); } } else if (status == "remove") { if (server.config.allowedDomains.Contains(host)) { server.config.allowedDomains.Remove(host); server.config.save(); } } html += "<script>window.opener.postMessage('success', '" + host + "'); window.close();</script>"; } else { accesslog += "\tfailed"; html += "<script>window.opener.postMessage('failed', '" + host + "'); window.close();</script>"; } html += "</body></html>"; ServerConfig.appendLog(accesslog); server.responseHTML(resp, html); } } } catch (Exception e) { ServerConfig.appendLog("Error: " + e.Message + "\n" + e.StackTrace); accesslog += "\tfailed"; ServerConfig.appendLog(accesslog); } return(ResponseCode.OK); }