2025-04-13 09:11:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2014-12-21 14:03:03 +00:00
|
|
|
|
using System.Net;
|
2017-03-07 18:57:47 +00:00
|
|
|
|
using System.Net.Http;
|
2025-04-13 09:11:36 +00:00
|
|
|
|
using System.Threading;
|
2016-05-09 01:32:47 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Logger;
|
|
|
|
|
|
using Flow.Launcher.Infrastructure.UserSettings;
|
2025-04-13 09:11:36 +00:00
|
|
|
|
using JetBrains.Annotations;
|
2014-12-21 14:03:03 +00:00
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Infrastructure.Http
|
2014-12-21 14:03:03 +00:00
|
|
|
|
{
|
2016-05-10 19:26:47 +00:00
|
|
|
|
public static class Http
|
2014-12-21 14:03:03 +00:00
|
|
|
|
{
|
2025-04-13 09:11:36 +00:00
|
|
|
|
private static readonly string ClassName = nameof(Http);
|
|
|
|
|
|
|
2016-07-27 13:25:16 +00:00
|
|
|
|
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
|
|
|
|
|
|
|
2025-04-13 08:36:34 +00:00
|
|
|
|
private static readonly HttpClient client = new();
|
2020-10-13 12:54:00 +00:00
|
|
|
|
|
2020-01-07 00:34:46 +00:00
|
|
|
|
static Http()
|
|
|
|
|
|
{
|
|
|
|
|
|
// need to be added so it would work on a win10 machine
|
|
|
|
|
|
ServicePointManager.Expect100Continue = true;
|
|
|
|
|
|
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls
|
2025-04-13 08:36:34 +00:00
|
|
|
|
| SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
|
2020-10-13 12:54:00 +00:00
|
|
|
|
|
|
|
|
|
|
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
|
2021-02-01 08:03:13 +00:00
|
|
|
|
HttpClient.DefaultProxy = WebProxy;
|
2020-10-13 12:54:00 +00:00
|
|
|
|
}
|
2020-12-21 16:31:00 +00:00
|
|
|
|
|
2020-10-13 12:54:00 +00:00
|
|
|
|
private static HttpProxy proxy;
|
2020-12-21 16:31:00 +00:00
|
|
|
|
|
2020-10-13 12:54:00 +00:00
|
|
|
|
public static HttpProxy Proxy
|
|
|
|
|
|
{
|
2025-04-13 09:11:36 +00:00
|
|
|
|
private get => proxy;
|
2020-10-13 12:54:00 +00:00
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
proxy = value;
|
2020-12-21 16:31:00 +00:00
|
|
|
|
proxy.PropertyChanged += UpdateProxy;
|
2021-02-01 08:03:13 +00:00
|
|
|
|
UpdateProxy(ProxyProperty.Enabled);
|
2020-10-13 12:54:00 +00:00
|
|
|
|
}
|
2020-01-07 00:34:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-29 09:16:44 +00:00
|
|
|
|
public static WebProxy WebProxy { get; } = new WebProxy();
|
2020-10-13 12:54:00 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Update the Address of the Proxy to modify the client Proxy
|
|
|
|
|
|
/// </summary>
|
2020-12-21 16:31:00 +00:00
|
|
|
|
public static void UpdateProxy(ProxyProperty property)
|
2014-12-21 14:03:03 +00:00
|
|
|
|
{
|
2021-02-22 03:46:31 +00:00
|
|
|
|
if (string.IsNullOrEmpty(Proxy.Server))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
2014-12-21 14:03:03 +00:00
|
|
|
|
{
|
2021-02-22 03:46:31 +00:00
|
|
|
|
(WebProxy.Address, WebProxy.Credentials) = property switch
|
2014-12-21 14:03:03 +00:00
|
|
|
|
{
|
2021-02-22 03:46:31 +00:00
|
|
|
|
ProxyProperty.Enabled => Proxy.Enabled switch
|
2020-12-21 16:31:00 +00:00
|
|
|
|
{
|
2021-02-22 03:46:31 +00:00
|
|
|
|
true when !string.IsNullOrEmpty(Proxy.Server) => Proxy.UserName switch
|
|
|
|
|
|
{
|
|
|
|
|
|
var userName when string.IsNullOrEmpty(userName) =>
|
|
|
|
|
|
(new Uri($"http://{Proxy.Server}:{Proxy.Port}"), null),
|
|
|
|
|
|
_ => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"),
|
2022-10-30 19:23:04 +00:00
|
|
|
|
new NetworkCredential(Proxy.UserName, Proxy.Password))
|
2021-02-22 03:46:31 +00:00
|
|
|
|
},
|
|
|
|
|
|
_ => (null, null)
|
2020-12-21 16:31:00 +00:00
|
|
|
|
},
|
2021-02-22 03:46:31 +00:00
|
|
|
|
ProxyProperty.Server => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), WebProxy.Credentials),
|
|
|
|
|
|
ProxyProperty.Port => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), WebProxy.Credentials),
|
|
|
|
|
|
ProxyProperty.UserName => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)),
|
|
|
|
|
|
ProxyProperty.Password => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)),
|
2025-04-13 08:36:34 +00:00
|
|
|
|
_ => throw new ArgumentOutOfRangeException(null)
|
2021-02-22 03:46:31 +00:00
|
|
|
|
};
|
|
|
|
|
|
}
|
2022-10-30 19:23:04 +00:00
|
|
|
|
catch (UriFormatException e)
|
2021-02-22 03:46:31 +00:00
|
|
|
|
{
|
2025-09-23 09:40:54 +00:00
|
|
|
|
PublicApi.Instance.ShowMsgError(Localize.pleaseTryAgain(), Localize.parseProxyFailed());
|
2025-04-13 09:11:36 +00:00
|
|
|
|
Log.Exception(ClassName, "Unable to parse Uri", e);
|
2021-02-22 03:46:31 +00:00
|
|
|
|
}
|
2015-02-01 14:46:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-11 05:24:41 +00:00
|
|
|
|
public static async Task DownloadAsync([NotNull] string url, [NotNull] string filePath, Action<double> reportProgress = null, CancellationToken token = default)
|
2016-05-10 19:26:47 +00:00
|
|
|
|
{
|
2021-01-04 02:31:05 +00:00
|
|
|
|
try
|
2020-10-14 03:29:12 +00:00
|
|
|
|
{
|
2021-02-14 14:36:05 +00:00
|
|
|
|
using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token);
|
2025-01-11 05:24:41 +00:00
|
|
|
|
|
2021-01-04 02:31:05 +00:00
|
|
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
|
|
|
|
{
|
2025-01-11 05:24:41 +00:00
|
|
|
|
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
|
|
|
|
|
|
var canReportProgress = totalBytes != -1;
|
|
|
|
|
|
|
|
|
|
|
|
if (canReportProgress && reportProgress != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
await using var contentStream = await response.Content.ReadAsStreamAsync(token);
|
2025-01-19 07:15:02 +00:00
|
|
|
|
await using var fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, 8192, true);
|
2025-01-11 05:24:41 +00:00
|
|
|
|
|
|
|
|
|
|
var buffer = new byte[8192];
|
|
|
|
|
|
long totalRead = 0;
|
|
|
|
|
|
int read;
|
|
|
|
|
|
double progressValue = 0;
|
|
|
|
|
|
|
|
|
|
|
|
reportProgress(0);
|
|
|
|
|
|
|
|
|
|
|
|
while ((read = await contentStream.ReadAsync(buffer, token)) > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
await fileStream.WriteAsync(buffer.AsMemory(0, read), token);
|
|
|
|
|
|
totalRead += read;
|
|
|
|
|
|
|
|
|
|
|
|
progressValue = totalRead * 100.0 / totalBytes;
|
|
|
|
|
|
|
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
|
return;
|
|
|
|
|
|
else
|
|
|
|
|
|
reportProgress(progressValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (progressValue < 100)
|
|
|
|
|
|
reportProgress(100);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await using var fileStream = new FileStream(filePath, FileMode.CreateNew);
|
|
|
|
|
|
await response.Content.CopyToAsync(fileStream, token);
|
|
|
|
|
|
}
|
2021-01-04 02:31:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new HttpRequestException($"Error code <{response.StatusCode}> returned from <{url}>");
|
|
|
|
|
|
}
|
2020-10-14 03:29:12 +00:00
|
|
|
|
}
|
2021-01-04 02:31:05 +00:00
|
|
|
|
catch (HttpRequestException e)
|
2020-10-14 03:29:12 +00:00
|
|
|
|
{
|
2025-04-13 09:11:36 +00:00
|
|
|
|
Log.Exception(ClassName, "Http Request Error", e, "DownloadAsync");
|
2021-01-04 02:31:05 +00:00
|
|
|
|
throw;
|
2020-10-14 03:29:12 +00:00
|
|
|
|
}
|
2016-05-10 19:26:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-29 09:50:56 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Asynchrously get the result as string from url.
|
2021-01-04 02:28:33 +00:00
|
|
|
|
/// When supposing the result larger than 83kb, try using GetStreamAsync to avoid reading as string
|
2020-12-29 09:50:56 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="url"></param>
|
2021-01-07 03:04:07 +00:00
|
|
|
|
/// <returns>The Http result as string. Null if cancellation requested</returns>
|
|
|
|
|
|
public static Task<string> GetAsync([NotNull] string url, CancellationToken token = default)
|
2015-02-01 14:46:56 +00:00
|
|
|
|
{
|
2025-04-13 09:11:36 +00:00
|
|
|
|
Log.Debug(ClassName, $"Url <{url}>");
|
2022-10-30 19:23:04 +00:00
|
|
|
|
return GetAsync(new Uri(url), token);
|
2020-12-29 09:50:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-07 03:04:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
///
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
|
/// <returns>The Http result as string. Null if cancellation requested</returns>
|
|
|
|
|
|
public static async Task<string> GetAsync([NotNull] Uri url, CancellationToken token = default)
|
2020-12-29 09:50:56 +00:00
|
|
|
|
{
|
2025-04-13 09:11:36 +00:00
|
|
|
|
Log.Debug(ClassName, $"Url <{url}>");
|
2021-01-07 03:04:07 +00:00
|
|
|
|
using var response = await client.GetAsync(url, token);
|
2022-10-30 19:23:04 +00:00
|
|
|
|
var content = await response.Content.ReadAsStringAsync(token);
|
|
|
|
|
|
if (response.StatusCode != HttpStatusCode.OK)
|
2020-10-13 12:54:00 +00:00
|
|
|
|
{
|
2020-12-21 16:31:00 +00:00
|
|
|
|
throw new HttpRequestException(
|
|
|
|
|
|
$"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
|
2015-01-11 13:52:30 +00:00
|
|
|
|
}
|
2022-10-30 19:23:04 +00:00
|
|
|
|
|
|
|
|
|
|
return content;
|
2014-12-21 14:03:03 +00:00
|
|
|
|
}
|
2020-12-29 09:13:31 +00:00
|
|
|
|
|
2020-12-29 09:50:56 +00:00
|
|
|
|
/// <summary>
|
2022-10-30 19:23:04 +00:00
|
|
|
|
/// Send a GET request to the specified Uri with an HTTP completion option and a cancellation token as an asynchronous operation.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="url">The Uri the request is sent to.</param>
|
|
|
|
|
|
/// <param name="completionOption">An HTTP completion option value that indicates when the operation should be considered completed.</param>
|
|
|
|
|
|
/// <param name="token">A cancellation token that can be used by other objects or threads to receive notice of cancellation</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static Task<Stream> GetStreamAsync([NotNull] string url,
|
|
|
|
|
|
CancellationToken token = default) => GetStreamAsync(new Uri(url), token);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Send a GET request to the specified Uri with an HTTP completion option and a cancellation token as an asynchronous operation.
|
2020-12-29 09:50:56 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="url"></param>
|
2022-10-30 19:23:04 +00:00
|
|
|
|
/// <param name="token"></param>
|
2020-12-29 09:50:56 +00:00
|
|
|
|
/// <returns></returns>
|
2022-10-30 19:23:04 +00:00
|
|
|
|
public static async Task<Stream> GetStreamAsync([NotNull] Uri url,
|
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
|
{
|
2025-04-13 09:11:36 +00:00
|
|
|
|
Log.Debug(ClassName, $"Url <{url}>");
|
2022-10-30 19:23:04 +00:00
|
|
|
|
return await client.GetStreamAsync(url, token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task<HttpResponseMessage> GetResponseAsync(string url, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead,
|
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
|
=> await GetResponseAsync(new Uri(url), completionOption, token);
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task<HttpResponseMessage> GetResponseAsync([NotNull] Uri url, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead,
|
|
|
|
|
|
CancellationToken token = default)
|
2020-12-29 09:13:31 +00:00
|
|
|
|
{
|
2025-04-13 09:11:36 +00:00
|
|
|
|
Log.Debug(ClassName, $"Url <{url}>");
|
2022-10-30 19:23:04 +00:00
|
|
|
|
return await client.GetAsync(url, completionOption, token);
|
2020-12-29 09:13:31 +00:00
|
|
|
|
}
|
2022-01-11 14:07:25 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Asynchrously send an HTTP request.
|
|
|
|
|
|
/// </summary>
|
2022-10-30 19:23:04 +00:00
|
|
|
|
public static async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, CancellationToken token = default)
|
2022-01-11 14:07:25 +00:00
|
|
|
|
{
|
2025-06-01 11:32:15 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return await client.SendAsync(request, completionOption, token);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (System.Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
|
|
|
|
|
|
}
|
2022-01-11 14:07:25 +00:00
|
|
|
|
}
|
2025-06-06 15:07:01 +00:00
|
|
|
|
|
|
|
|
|
|
public static async Task<string> GetStringAsync(string url, CancellationToken token = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Debug(ClassName, $"Url <{url}>");
|
|
|
|
|
|
return await client.GetStringAsync(url, token);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-12-21 14:03:03 +00:00
|
|
|
|
}
|
2020-12-29 06:49:11 +00:00
|
|
|
|
}
|