Exemplo 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)
			{

			}
		}
Exemplo n.º 2
0
        private void Worker()
        {
            WaitHandle[] wait = { _ready, _stop };
            while (0 == WaitHandle.WaitAny(wait))
            {
                HttpListenerContext context;
                lock (_queue)
                {
                    if (_queue.Count > 0)
                        context = _queue.Dequeue();
                    else
                    {
                        _ready.Reset();
                        continue;
                    }
                }

                try { ProcessRequest(context); }
                catch (Exception e)
                {
                    try
                    {
                        var actionResult = new ActionResult { Content = Encoding.UTF8.GetBytes(e.Message), ContentType = "text/plain" };
	                    if (context.Response.OutputStream.CanWrite)
	                    {
		                    context.Response.OutputStream.Write(actionResult.Content, 0, actionResult.Content.Length);		             
	                    }
						_logger.Error(e.ToString());
                    }
                    catch (Exception ex)
                    {
						_logger.Error(ex.ToString());
						
                    }
                }

                try
                {
                    context.Response.OutputStream.Close();
                }
                catch (Exception e)
                {
					//_logger.Error(e.ToString());
                }
            }
        }