Ejemplo n.º 1
0
        void httpServer_ProcessRequest(System.Net.HttpListenerContext ctx)
        {
            ActionResult actionResult;

            if (ctx.Request.HttpMethod == "POST")
            {
                var segments = new Uri(ctx.Request.Url.AbsoluteUri).Segments;
                if (segments.Length > 1 && segments[segments.Length - 2] == "TWAIN@Web/" && segments[segments.Length - 1] == "ajax")
                {
                    var scanFormModelBinder = new ModelBinder(GetPostData(ctx.Request));
                    var method         = scanFormModelBinder.BindAjaxMethod();
                    var scanController = new ScanController(_markerAsynchrone);
                    switch (method)
                    {
                    case "GetScannerParameters":
                        actionResult = scanController.GetScannerParameters(_scannerManager, _cacheSettings, scanFormModelBinder.BindSourceIndex());
                        break;

                    case "Scan":
                        actionResult = scanController.Scan(scanFormModelBinder.BindScanForm(), _scannerManager);
                        break;

                    case "RestartWia":
                        actionResult = scanController.RestartWia();
                        break;

                    case "Restart":
                        actionResult = scanController.Restart();
                        break;

                    default:
                        actionResult = new ActionResult {
                            Content = new byte[0]
                        };
                        ctx.Response.Redirect("/TWAIN@Web/");
                        break;
                    }
                }
                else
                {
                    actionResult = new ActionResult {
                        Content = new byte[0]
                    };
                    ctx.Response.Redirect("/TWAIN@Web/");
                }
            }
            else if (ctx.Request.Url.AbsolutePath.Length < 11)
            {
                actionResult = new ActionResult {
                    Content = new byte[0]
                };
                ctx.Response.Redirect("/TWAIN@Web/");
            }
            else
            {
                var contr            = new HomeController();
                var requestParameter = ctx.Request.Url.AbsolutePath.Substring(11);
                if (requestParameter != "download")
                {
                    // /twain@web/ — это 11 символов, а дальше — имя файла
                    if (requestParameter == "")
                    {
                        requestParameter = "index.html";
                    }

                    actionResult = contr.StaticFile(requestParameter);
                }
                else
                {
                    var fileParam = new ModelBinder(GetGetData(ctx.Request)).BindDownloadFile();
                    actionResult = contr.DownloadFile(fileParam);
                }
            }

            if (actionResult.FileNameToDownload != null)
            {
                ctx.Response.AddHeader("Content-Disposition", "attachment; filename*=UTF-8''" + Uri.EscapeDataString(Uri.UnescapeDataString(actionResult.FileNameToDownload)));
            }

            if (actionResult.ContentType != null)
            {
                ctx.Response.ContentType = actionResult.ContentType;
            }

            try
            {
                ctx.Response.OutputStream.Write(actionResult.Content, 0, actionResult.Content.Length);
            }
            catch (Exception)
            {
            }
        }
Ejemplo n.º 2
0
        private void httpServer_ProcessRequest(System.Net.HttpListenerContext ctx)
        {
            ActionResult actionResult;

            ctx.Response.AppendHeader("Access-Control-Allow-Origin", "*");
            ctx.Response.AppendHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
            if (ctx.Request.HttpMethod == "POST")
            {
                var segments = new Uri(ctx.Request.Url.AbsoluteUri).Segments;
                if (segments.Length > 1 && segments[segments.Length - 2] == (ServiceName + "/") && segments[segments.Length - 1] == "ajax")
                {
                    var scanFormModelBinder = new ModelBinder(GetPostData(ctx.Request));
                    var method         = scanFormModelBinder.BindAjaxMethod();
                    var scanController = new ScanController(_markerAsynchrone);
                    switch (method)
                    {
                    case "GetScannerParameters":
                        actionResult = scanController.GetScannerParameters(_scannerManager, _cacheSettings, scanFormModelBinder.BindSourceIndex());
                        break;

                    case "Scan":
                        actionResult = scanController.Scan(scanFormModelBinder.BindScanForm(), _scannerManager);
                        break;

                    case "RestartWia":
                        actionResult = scanController.RestartWia();
                        break;

                    case "Restart":
                        actionResult = scanController.Restart();
                        break;

                    default:
                        actionResult = DefaultResponce(ctx);
                        break;
                    }
                }
                else
                {
                    actionResult = DefaultResponce(ctx);
                }
            }
            else if (ctx.Request.HttpMethod == "GET")
            {
                if (ctx.Request.Url.AbsolutePath.Length >= _procesName.Length)
                {
                    var homeCtrl = new HomeController();
                    var method   = ctx.Request.Url.AbsolutePath.Substring(_procesName.Length);

                    switch (method)
                    {
                    case "download":
                        ModelBinder MB        = new ModelBinder(GetGetData(ctx.Request));
                        var         fileParam = MB.BindDownloadFile();

                        if (MB.IsBase64)
                        {
                            actionResult = homeCtrl.ConvertToBase64File(fileParam);
                        }
                        else
                        {
                            actionResult = homeCtrl.DownloadFile(fileParam);
                        }
                        break;

                    case "version":
                        actionResult = homeCtrl.GetVersionProgram(
                            Assembly.GetEntryAssembly().GetName().Version.ToString());
                        break;

                    default:
                        if (method == "")
                        {
                            method = "index.html";
                        }
                        actionResult = homeCtrl.StaticFile(method);
                        break;
                    }
                }
                else
                {
                    actionResult = DefaultResponce(ctx);
                }
            }
            else
            {
                actionResult = DefaultResponce(ctx);
            }


            if (actionResult.FileNameToDownload != null)
            {
                ctx.Response.AddHeader("Content-Disposition", "attachment; filename*=UTF-8''" + Uri.EscapeDataString(Uri.UnescapeDataString(actionResult.FileNameToDownload)));
            }

            if (actionResult.ContentType != null)
            {
                ctx.Response.ContentType = actionResult.ContentType;
            }

            try
            {
                ctx.Response.OutputStream.Write(actionResult.Content, 0, actionResult.Content.Length);
            }
            catch (Exception)
            {
            }
        }