Flow.Launcher/Flow.Launcher.Infrastructure/Http/Http.cs

166 lines
6.5 KiB
C#
Raw Permalink Normal View History

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;
2014-12-21 14:03:03 +00:00
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using System;
using System.ComponentModel;
2021-01-07 03:04:07 +00:00
using System.Threading;
using System.Windows.Interop;
using Flow.Launcher.Plugin;
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
{
public static class Http
2014-12-21 14:03:03 +00:00
{
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
2021-02-01 08:25:57 +00:00
private static HttpClient client = new HttpClient();
2021-02-22 09:36:06 +00:00
public static IPublicAPI API { get; set; }
static Http()
{
// need to be added so it would work on a win10 machine
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
2020-03-02 01:22:26 +00:00
| SecurityProtocolType.Tls12;
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
2021-02-01 08:03:13 +00:00
HttpClient.DefaultProxy = WebProxy;
}
private static HttpProxy proxy;
public static HttpProxy Proxy
{
private get { return proxy; }
set
{
proxy = value;
proxy.PropertyChanged += UpdateProxy;
2021-02-01 08:03:13 +00:00
UpdateProxy(ProxyProperty.Enabled);
}
}
2020-12-29 09:16:44 +00:00
public static WebProxy WebProxy { get; } = new WebProxy();
/// <summary>
/// Update the Address of the Proxy to modify the client Proxy
/// </summary>
public static void UpdateProxy(ProxyProperty property)
2014-12-21 14:03:03 +00:00
{
if (string.IsNullOrEmpty(Proxy.Server))
return;
try
2014-12-21 14:03:03 +00:00
{
(WebProxy.Address, WebProxy.Credentials) = property switch
2014-12-21 14:03:03 +00:00
{
ProxyProperty.Enabled => Proxy.Enabled switch
{
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}"),
2021-02-22 09:31:00 +00:00
new NetworkCredential(Proxy.UserName, Proxy.Password))
},
_ => (null, null)
},
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)),
_ => throw new ArgumentOutOfRangeException()
};
}
catch(UriFormatException e)
{
2021-02-22 09:36:06 +00:00
API.ShowMsg("Please try again", "Unable to parse Http Proxy");
Log.Exception("Flow.Launcher.Infrastructure.Http", "Unable to parse Uri", e);
}
2015-02-01 14:46:56 +00:00
}
public static async Task DownloadAsync([NotNull] string url, [NotNull] string filePath, CancellationToken token = default)
{
try
{
using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token);
if (response.StatusCode == HttpStatusCode.OK)
{
await using var fileStream = new FileStream(filePath, FileMode.CreateNew);
await response.Content.CopyToAsync(fileStream);
}
else
{
throw new HttpRequestException($"Error code <{response.StatusCode}> returned from <{url}>");
}
}
catch (HttpRequestException e)
{
Log.Exception("Infrastructure.Http", "Http Request Error", e, "DownloadAsync");
throw;
}
}
/// <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
/// </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
{
2017-03-26 23:08:56 +00:00
Log.Debug($"|Http.Get|Url <{url}>");
2021-01-07 03:04:07 +00:00
return GetAsync(new Uri(url.Replace("#", "%23")), token);
}
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)
{
Log.Debug($"|Http.Get|Url <{url}>");
2021-01-07 03:04:07 +00:00
using var response = await client.GetAsync(url, token);
var content = await response.Content.ReadAsStringAsync();
if (response.StatusCode == HttpStatusCode.OK)
2015-01-11 13:52:30 +00:00
{
return content;
}
else
{
throw new HttpRequestException(
$"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
2015-01-11 13:52:30 +00:00
}
2014-12-21 14:03:03 +00:00
}
2020-12-29 09:13:31 +00:00
/// <summary>
/// Asynchrously get the result as stream from url.
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
2021-01-07 03:04:07 +00:00
public static async Task<Stream> GetStreamAsync([NotNull] string url, CancellationToken token = default)
2020-12-29 09:13:31 +00:00
{
Log.Debug($"|Http.Get|Url <{url}>");
var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token);
2020-12-29 09:13:31 +00:00
return await response.Content.ReadAsStreamAsync();
}
/// <summary>
/// Asynchrously send an HTTP request.
/// </summary>
public static async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token = default)
{
return await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
}
2014-12-21 14:03:03 +00:00
}
2020-12-29 06:49:11 +00:00
}