From be029a74f2e7a37bd1412fd0d2a304e92d6250cf Mon Sep 17 00:00:00 2001 From: VoidX Date: Wed, 4 Sep 2024 10:54:31 +0200 Subject: [PATCH] Use VoidX.WPF for HTTP --- .../CavernizeGUI/CavernizeGUI.csproj | 7 +- .../Tools => VoidX.WPF}/FFmpeg.cs | 0 CavernSamples/VoidX.WPF/HTTP.cs | 67 +++++++++++++++++++ .../Tools => VoidX.WPF}/UpdateCheck.cs | 14 ++-- 4 files changed, 76 insertions(+), 12 deletions(-) rename CavernSamples/{CavernizeGUI/Tools => VoidX.WPF}/FFmpeg.cs (100%) create mode 100644 CavernSamples/VoidX.WPF/HTTP.cs rename CavernSamples/{CavernizeGUI/Tools => VoidX.WPF}/UpdateCheck.cs (83%) diff --git a/CavernSamples/CavernizeGUI/CavernizeGUI.csproj b/CavernSamples/CavernizeGUI/CavernizeGUI.csproj index e5706f63..534e4640 100644 --- a/CavernSamples/CavernizeGUI/CavernizeGUI.csproj +++ b/CavernSamples/CavernizeGUI/CavernizeGUI.csproj @@ -24,7 +24,10 @@ - + + + + @@ -33,7 +36,7 @@ - + diff --git a/CavernSamples/CavernizeGUI/Tools/FFmpeg.cs b/CavernSamples/VoidX.WPF/FFmpeg.cs similarity index 100% rename from CavernSamples/CavernizeGUI/Tools/FFmpeg.cs rename to CavernSamples/VoidX.WPF/FFmpeg.cs diff --git a/CavernSamples/VoidX.WPF/HTTP.cs b/CavernSamples/VoidX.WPF/HTTP.cs new file mode 100644 index 00000000..287fd896 --- /dev/null +++ b/CavernSamples/VoidX.WPF/HTTP.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Runtime.CompilerServices; + +namespace VoidX.WPF { + /// + /// HTTP utilities. + /// + static class HTTP { + /// + /// Gets a HTTP resource with a timeout. + /// + public static string GET(string url, int timeoutSeconds = 5) { + HttpClient client = new HttpClient { + Timeout = TimeSpan.FromSeconds(timeoutSeconds) + }; + + try { + HttpResponseMessage response = client.GetAsync(url).Result; + if (response.IsSuccessStatusCode) { + return response.Content.ReadAsStringAsync().Result; + } + } catch { } + return null; + } + + /// + /// Sends a POST request of key-value pairs with a timeout. + /// + public static string POST(string url, KeyValuePair[] data, int timeoutSeconds = 5) { + using FormUrlEncodedContent content = new FormUrlEncodedContent(data); + return POST(url, content, timeoutSeconds); + } + + /// + /// Sends a POST request of large binary data with a timeout. + /// + public static string POST(string url, (string key, byte[] value)[] data, int timeoutSeconds = 5) { + using MultipartFormDataContent form = new MultipartFormDataContent(); + for (int i = 0; i < data.Length; i++) { + form.Add(new ByteArrayContent(data[i].value), data[i].key); + } + return POST(url, form, timeoutSeconds); + } + + /// + /// Sends an arbitrary POST request. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string POST(string url, HttpContent content, int timeoutSeconds = 5) { + using HttpClient client = new HttpClient() { + Timeout = TimeSpan.FromSeconds(timeoutSeconds) + }; + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url) { + Content = content + }; + try { + HttpResponseMessage response = client.SendAsync(request).Result; + if (response.IsSuccessStatusCode) { + return response.Content.ReadAsStringAsync().Result; + } + } catch { } + return null; + } + } +} \ No newline at end of file diff --git a/CavernSamples/CavernizeGUI/Tools/UpdateCheck.cs b/CavernSamples/VoidX.WPF/UpdateCheck.cs similarity index 83% rename from CavernSamples/CavernizeGUI/Tools/UpdateCheck.cs rename to CavernSamples/VoidX.WPF/UpdateCheck.cs index 97ab8ec0..a4a46514 100644 --- a/CavernSamples/CavernizeGUI/Tools/UpdateCheck.cs +++ b/CavernSamples/VoidX.WPF/UpdateCheck.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.Net.Http; using System.Threading.Tasks; using System.Windows; @@ -21,22 +20,17 @@ public static class UpdateCheck { /// /// Last time an update check was performed /// When the check was performed, call this function - used to keep track of the last check - static async void CheckForUpdate(DateTime lastCheck, Action onChecked) { + static void CheckForUpdate(DateTime lastCheck, Action onChecked) { if (DateTime.Now < lastCheck + TimeSpan.FromDays(7)) { return; } - HttpClient client = new(); - HttpResponseMessage response; - try { - response = await client.GetAsync(updateLocation); - } catch { + string body = HTTP.GET(updateLocation); + if (!int.TryParse(body, out int version)) { return; } - response.EnsureSuccessStatusCode(); - string body = await response.Content.ReadAsStringAsync(); - if (thisRevision < int.Parse(body)) { + if (thisRevision < version) { if (MessageBox.Show("A new version is available! Do you want to download it?", "Update available", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { Process.Start(new ProcessStartInfo {