public override bool Process(HttpServer server, HttpRequest request, HttpResponse response) { if (request.Page.StartsWith("/status")) { response.ContentType = (string)SubstitutingFileReader.MimeTypes[".html"]; response.ReturnCode = 200; StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("twain_source"); jsonWriter.WriteValue(Properties.Settings.Default.twain_source); jsonWriter.WriteComment("ID of selected TWAIN source"); jsonWriter.WritePropertyName("x"); jsonWriter.WriteValue(Properties.Settings.Default.x); jsonWriter.WriteComment("The x-coordinate of the upper-left corner of the crop rectangle"); jsonWriter.WritePropertyName("y"); jsonWriter.WriteValue(Properties.Settings.Default.y); jsonWriter.WriteComment("The y-coordinate of the upper-left corner of the crop rectangle"); jsonWriter.WritePropertyName("w"); jsonWriter.WriteValue(Properties.Settings.Default.w); jsonWriter.WriteComment("The width of the crop rectangle"); jsonWriter.WritePropertyName("h"); jsonWriter.WriteValue(Properties.Settings.Default.h); jsonWriter.WriteComment("The height of the crop rectangle"); jsonWriter.WritePropertyName("encode"); jsonWriter.WriteValue(Properties.Settings.Default.encode); jsonWriter.WriteComment("Encoding setup"); jsonWriter.WritePropertyName("server_autostart"); jsonWriter.WriteValue(Properties.Settings.Default.server_autostart); jsonWriter.WriteComment("Shall web server be started automatically"); jsonWriter.WritePropertyName("start_minimized"); jsonWriter.WriteValue(Properties.Settings.Default.start_minimized); jsonWriter.WriteComment("Shall application start minimized"); jsonWriter.WritePropertyName("port"); jsonWriter.WriteValue(Properties.Settings.Default.port); jsonWriter.WriteComment("Web server listening port"); jsonWriter.WritePropertyName("dpi"); jsonWriter.WriteValue(Properties.Settings.Default.dpi); jsonWriter.WriteComment("DPI"); jsonWriter.WritePropertyName("colour"); jsonWriter.WriteValue(Properties.Settings.Default.colour == ColourSetting.Colour ? "full" : Properties.Settings.Default.colour == ColourSetting.GreyScale ? "greyscale" : "blackandwhite"); jsonWriter.WriteComment("Colouring setup"); jsonWriter.WritePropertyName("adf"); jsonWriter.WriteValue(Properties.Settings.Default.useADF); jsonWriter.WriteComment("Shall ADF be used?"); jsonWriter.WriteEndObject(); } response.Content = sb.ToString(); return true; } return false; }
public override bool Process(HttpServer server, HttpRequest request, HttpResponse response) { if (request.Page.StartsWith("/sources")) { response.ContentType = (string)SubstitutingFileReader.MimeTypes[".html"]; response.ReturnCode = 200; StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); foreach (string name in this.mainForm.twain.SourceNames) { jsonWriter.WritePropertyName(name); jsonWriter.WriteValue(System.Web.HttpUtility.UrlEncode(name)); } jsonWriter.WriteEndObject(); } response.Content = sb.ToString(); return true; } return false; }
public override bool Process(HttpServer server, HttpRequest request, HttpResponse response) { if (request.Page.StartsWith("/scan")) { string source = Properties.Settings.Default.twain_source; int x = Properties.Settings.Default.x; int y = Properties.Settings.Default.y; int w = Properties.Settings.Default.w; int h = Properties.Settings.Default.h; string encode = Properties.Settings.Default.encode; int dpi = Properties.Settings.Default.dpi; bool adf = Properties.Settings.Default.useADF; bool pdf = Properties.Settings.Default.pdf; ColourSetting colour = Properties.Settings.Default.colour; try { if (request.Query.ContainsKey("source")) { source = System.Web.HttpUtility.UrlDecode(request.Query["source"]); if (!this.mainForm.twain.SourceNames.Contains(source)) { throw new Exception("Unknown source: " + source); } } if (request.Query.ContainsKey("encode")) { encode = request.Query["encode"]; if (!"base64".Equals(encode) && !"".Equals(encode)) throw new Exception("Unknown encoding: " + encode); } x = ScanHandler.getNumber(request, x, "x"); y = ScanHandler.getNumber(request, y, "y"); w = ScanHandler.getNumber(request, w, "w"); h = ScanHandler.getNumber(request, h, "h"); dpi = ScanHandler.getNumber(request, dpi, "dpi"); if (!this.mainForm.dpiComboBox.Items.Contains("" + dpi)) throw new Exception("Unsupported DPI value: " + dpi); adf = request.Query.ContainsKey("adf") ? "1".Equals(request.Query["adf"]) : adf; pdf = request.Query.ContainsKey("pdf") ? "1".Equals(request.Query["pdf"]) : adf; if (request.Query.ContainsKey("colour")) { string col = request.Query["colour"]; if ("full".Equals(col)) colour = ColourSetting.Colour; else if ("greyscale".Equals(col)) colour = ColourSetting.GreyScale; else if ("blackandwhite".Equals(col)) colour = ColourSetting.BlackAndWhite; else throw new Exception("Unknown colour settings: " + col); } } catch (Exception e) { response.ReturnCode = 500; response.Content = "Invalid URL arguments: " + e.Message; return true; } bool testing = request.Query.ContainsKey("test") && "1".Equals(request.Query["test"]); if ("".Equals(source) && !testing) { request.Host = "."; request.Page = "/noscanner.html"; response.ReturnCode = 500; return base.Process(server, request, response); } if (this.mainForm.scanSemaphore.WaitOne(0)) { this.mainForm.scanData.Clear(); this.mainForm.dataReady = false; if (testing) { try { this.mainForm.scanData.Add(new Bitmap("./TestImage.png")); } catch (Exception) { // left blank intentionally } this.mainForm.dataReady = true; this.mainForm.scanSemaphore.Release(); } else { this.mainForm.scanTarget = ScanTarget.WEB; this.mainForm.Invoke(new ScanDelegate(Scan), new object[] { source, dpi, colour, adf }); DateTime startTime = DateTime.Now; TimeSpan elapsed; do { Thread.Sleep(100); elapsed = DateTime.Now.Subtract(startTime); } while (!this.mainForm.dataReady && elapsed.TotalMinutes < 5); } if (this.mainForm.dataReady && this.mainForm.scanData.Count > 0) { response.ReturnCode = 200; using (MemoryStream ms = new MemoryStream()) { if (!adf) { ScanHandler.SaveImage(x, y, w, h, ms, this.mainForm.scanData.First(), pdf); } else { ScanHandler.SaveImage(x, y, w, h, ms, this.mainForm.scanData, pdf); } if ("base64".Equals(encode)) { response.ContentType = "text/plain"; response.Encoding = "base64"; response.Content = Convert.ToBase64String(ms.ToArray()); } else { response.ContentType = (adf || pdf) ? "application/octet-stream" : (string)SubstitutingFileReader.MimeTypes[".png"]; response.Encoding = null; response.RawContent = ms.ToArray(); } } } else { request.Host = "."; request.Page = "/failed.html"; response.ReturnCode = 500; return base.Process(server, request, response); } } else { request.Host = "."; request.Page = "/blocked.html"; response.ReturnCode = 500; return base.Process(server, request, response); } return true; } return false; }
private static int getNumber(HttpRequest request, int x, string attribute) { if (request.Query.ContainsKey(attribute)) { string value = request.Query[attribute]; if (!"".Equals(value)) { x = Int32.Parse(value); if (x < 0) { throw new Exception(attribute + " has to be positive, or zero: " + x); } } } return x; }
public override bool Process(HttpServer server, HttpRequest request, HttpResponse response) { request.Host = "."; request.Page = "/home.html"; return base.Process(server, request, response); }