fix potential uri format issue causing program crash

This commit is contained in:
弘韬 张 2021-02-22 11:46:31 +08:00 committed by Jeremy Wu
parent cc48ed54a3
commit ff5fd5738c

View file

@ -50,25 +50,35 @@ namespace Flow.Launcher.Infrastructure.Http
/// </summary> /// </summary>
public static void UpdateProxy(ProxyProperty property) public static void UpdateProxy(ProxyProperty property)
{ {
(WebProxy.Address, WebProxy.Credentials) = property switch if (string.IsNullOrEmpty(Proxy.Server))
return;
try
{ {
ProxyProperty.Enabled => Proxy.Enabled switch (WebProxy.Address, WebProxy.Credentials) = property switch
{ {
true => Proxy.UserName switch ProxyProperty.Enabled => Proxy.Enabled switch
{ {
var userName when !string.IsNullOrEmpty(userName) => true when !string.IsNullOrEmpty(Proxy.Server) => Proxy.UserName switch
(new Uri($"http://{Proxy.Server}:{Proxy.Port}"), null), {
_ => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), var userName when string.IsNullOrEmpty(userName) =>
new NetworkCredential(Proxy.UserName, Proxy.Password)) (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), null),
_ => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"),
new NetworkCredential(Proxy.UserName, Proxy.Password))
},
_ => (null, null)
}, },
false => (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.Server => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), WebProxy.Credentials), ProxyProperty.UserName => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)),
ProxyProperty.Port => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), WebProxy.Credentials), ProxyProperty.Password => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)),
ProxyProperty.UserName => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)), _ => throw new ArgumentOutOfRangeException()
ProxyProperty.Password => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)), };
_ => throw new ArgumentOutOfRangeException() }
}; catch(UriFormatException e)
{
Log.Exception("Http", "Unable to parse Uri", e);
}
} }
public static async Task DownloadAsync([NotNull] string url, [NotNull] string filePath, CancellationToken token = default) public static async Task DownloadAsync([NotNull] string url, [NotNull] string filePath, CancellationToken token = default)