diff --git a/app/XDM/BrowserMonitor/IpcHttpHandler.cs b/app/XDM/BrowserMonitor/IpcHttpHandler.cs index 720ab19..d076c5d 100644 --- a/app/XDM/BrowserMonitor/IpcHttpHandler.cs +++ b/app/XDM/BrowserMonitor/IpcHttpHandler.cs @@ -7,6 +7,7 @@ using XDM.Core.Lib.Common; using XDM.Core.Lib.Util; using HttpServer; using System.Threading; +using TraceLog; namespace BrowserMonitoring { @@ -52,7 +53,7 @@ namespace BrowserMonitoring switch (context.RequestPath) { case "/download": - Console.WriteLine(Encoding.UTF8.GetString(context.RequestBody!)); + Log.Debug(Encoding.UTF8.GetString(context.RequestBody!)); var message = Message.ParseMessage(Encoding.UTF8.GetString(context.RequestBody!)); if (!(Helpers.IsBlockedHost(message.Url) || Helpers.IsCompressedJSorCSS(message.Url))) { diff --git a/app/XDM/HttpServer/HttpParser.cs b/app/XDM/HttpServer/HttpParser.cs index dcc8db5..2a823aa 100644 --- a/app/XDM/HttpServer/HttpParser.cs +++ b/app/XDM/HttpServer/HttpParser.cs @@ -18,7 +18,7 @@ namespace HttpServer var arr = statusLine.Split(' '); if (arr.Length > 2) { - return arr[0]; + return arr[1]; } } catch (Exception ex) @@ -42,7 +42,8 @@ namespace HttpServer internal static long ParseContentLength(Dictionary> headers) { - return Int64.Parse(headers.GetValueOrDefault("Content-Length")?[0] ?? "-1"); + var value = headers.GetValueOrDefault("Content-Length")?[0]; + return Int64.Parse(value ?? "-1"); } private static bool ShouldKeepAlive(Dictionary> headers) @@ -73,7 +74,7 @@ namespace HttpServer } ParseHeader(line, out string headerName, out string headerValue); var values = headers.GetValueOrDefault(headerName, new List()); - values.Add(headerName); + values.Add(headerValue); headers[headerName] = values; } var contentLength = ParseContentLength(headers); @@ -91,7 +92,7 @@ namespace HttpServer { byte[] buffer = new byte[8192]; int read; - while ((read = stream.Read(buffer, 0, (int)Math.Min(buffer.Length, limit))) != 0) + while (limit > 0 && (read = stream.Read(buffer, 0, (int)Math.Min(buffer.Length, limit))) != 0) { destination.Write(buffer, 0, read); limit -= read; diff --git a/app/XDM/XDM.Common.UI/NewDownloadDialogHelper.cs b/app/XDM/XDM.Common.UI/NewDownloadDialogHelper.cs index 3ffd9ba..92ae5b4 100644 --- a/app/XDM/XDM.Common.UI/NewDownloadDialogHelper.cs +++ b/app/XDM/XDM.Common.UI/NewDownloadDialogHelper.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using XDM.Core.Lib.Common; -using XDM.Core.Lib.Common; using XDM.Core.Lib.Common.Segmented; using XDM.Core.Lib.Util; @@ -10,8 +9,10 @@ namespace XDM.Common.UI public class NewDownloadDialogHelper { public static void CreateAndShowDialog(IApp app, IAppUI appUi, - INewDownloadDialogSkeleton window, Message message = null) + INewDownloadDialogSkeleton window, Message? message = null, + Action? destroyCallback = null) { + window.DestroyEvent += (_, _) => destroyCallback?.Invoke(); window.SetFolderValues(CommonUtils.GetFolderValues()); window.SeletedFolderIndex = Config.Instance.FolderSelectionMode == FolderSelectionMode.Auto ? 0 : 2; diff --git a/app/XDM/XDM.WinForm.UI/NewDownloadDialogView.cs b/app/XDM/XDM.WinForm.UI/NewDownloadDialogView.cs index ff0a4f1..06c1920 100644 --- a/app/XDM/XDM.WinForm.UI/NewDownloadDialogView.cs +++ b/app/XDM/XDM.WinForm.UI/NewDownloadDialogView.cs @@ -117,11 +117,16 @@ namespace XDM.WinForm.UI #if !NET35 linkLabel1.Margin = new Padding(5); #endif + + this.FormClosed += (a, b) => + { + this.DestroyEvent?.Invoke(this, EventArgs.Empty); + }; } public void DisposeWindow() { - this.Dispose(); + this.Close(); } //private string? SelectFile() diff --git a/app/XDM/XDMApp/AppWin.cs b/app/XDM/XDMApp/AppWin.cs index 4e6c86b..174c6a8 100644 --- a/app/XDM/XDMApp/AppWin.cs +++ b/app/XDM/XDMApp/AppWin.cs @@ -221,9 +221,16 @@ namespace XDMApp public void ShowNewDownloadDialog(Message message) { + var url = message.Url; + if (NewDownloadPromptTracker.IsPromptAlreadyOpen(url)) + { + return; + } RunOnUiThread(() => { - NewDownloadDialogHelper.CreateAndShowDialog(this.App, this, this.CreateNewDownloadDialog(false), message); + NewDownloadPromptTracker.PromptOpen(url); + NewDownloadDialogHelper.CreateAndShowDialog(this.App, this, this.CreateNewDownloadDialog(false), message, + () => NewDownloadPromptTracker.PromptClosed(url)); }); } diff --git a/app/XDM/XDMApp/NewDownloadPromptTracker.cs b/app/XDM/XDMApp/NewDownloadPromptTracker.cs new file mode 100644 index 0000000..ca07a6a --- /dev/null +++ b/app/XDM/XDMApp/NewDownloadPromptTracker.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace XDMApp +{ + public static class NewDownloadPromptTracker + { + private static readonly HashSet newDownloadPrompts = new(); + + public static bool IsPromptAlreadyOpen(string url) + { + lock (newDownloadPrompts) + { + return newDownloadPrompts.Contains(url); + } + } + + public static void PromptOpen(string url) + { + lock (newDownloadPrompts) + { + newDownloadPrompts.Add(url); + } + } + + public static void PromptClosed(string url) + { + lock (newDownloadPrompts) + { + newDownloadPrompts.Remove(url); + } + } + } +} diff --git a/app/XDM/XDMApp/XDMApp.cs b/app/XDM/XDMApp/XDMApp.cs index 9fe798a..0810d04 100644 --- a/app/XDM/XDMApp/XDMApp.cs +++ b/app/XDM/XDMApp/XDMApp.cs @@ -39,7 +39,7 @@ namespace XDMApp private NativeMessagingHostHandler nativeMessaging; private bool isClipboardMonitorActive = false; private string lastClipboardText; - private Timer AwakePingTimer; + private Timer awakePingTimer; private System.Threading.Timer UpdateCheckTimer; public IList? Updates { get; private set; } @@ -66,11 +66,11 @@ namespace XDMApp Config.DataDir = configPath; Config.LoadConfig(); - AwakePingTimer = new Timer(60000) + awakePingTimer = new Timer(60000) { AutoReset = true }; - AwakePingTimer.Elapsed += (a, b) => Helpers.SendKeepAlivePing(); + awakePingTimer.Elapsed += (a, b) => Helpers.SendKeepAlivePing(); //UpdateCheckTimer = new System.Threading.Timer( // callback: a => CheckForUpdate(), @@ -207,10 +207,10 @@ namespace XDMApp ProxyInfo? proxyInfo, int maxSpeedLimit) { - if (!AwakePingTimer.Enabled) + if (!awakePingTimer.Enabled) { Log.Debug("Starting keep awaik timer"); - AwakePingTimer.Start(); + awakePingTimer.Start(); } var id = download.Id; var startType = DownloadStartType.Waiting; @@ -501,10 +501,10 @@ namespace XDMApp public void ResumeDownload(Dictionary list, bool nonInteractive = false) { - if (!AwakePingTimer.Enabled) + if (!awakePingTimer.Enabled) { Log.Debug("Starting keep awake timer"); - AwakePingTimer.Start(); + awakePingTimer.Start(); } AppUI.RunOnUiThread((Action)(() => @@ -906,10 +906,10 @@ namespace XDMApp { Helpers.ShutDownPC(); } - if (AwakePingTimer.Enabled) + if (awakePingTimer.Enabled) { Log.Debug("Stopping keep awake timer"); - AwakePingTimer.Stop(); + awakePingTimer.Stop(); } if (Config.Instance.RunCommandAfterCompletion) {