7
7
using Flow . Launcher . Infrastructure . Logger ;
8
8
using Flow . Launcher . Infrastructure . UserSettings ;
9
9
using System ;
10
+ using System . ComponentModel ;
10
11
11
12
namespace Flow . Launcher . Infrastructure . Http
12
13
{
@@ -15,6 +16,7 @@ public static class Http
15
16
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko" ;
16
17
17
18
private static HttpClient client ;
19
+
18
20
private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler ( )
19
21
{
20
22
UseProxy = true ,
@@ -31,56 +33,58 @@ static Http()
31
33
32
34
client = new HttpClient ( socketsHttpHandler , false ) ;
33
35
client . DefaultRequestHeaders . Add ( "User-Agent" , UserAgent ) ;
34
-
35
36
}
37
+
36
38
private static HttpProxy proxy ;
39
+
37
40
public static HttpProxy Proxy
38
41
{
39
- private get
40
- {
41
- return proxy ;
42
- }
42
+ private get { return proxy ; }
43
43
set
44
44
{
45
45
proxy = value ;
46
- UpdateProxy ( ) ;
46
+ proxy . PropertyChanged += UpdateProxy ;
47
47
}
48
48
}
49
49
50
- public static WebProxy WebProxy { get ; private set ; } = new WebProxy ( ) ;
50
+ private static readonly WebProxy _proxy = new WebProxy ( ) ;
51
+
52
+ public static WebProxy WebProxy
53
+ {
54
+ get { return _proxy ; }
55
+ }
51
56
52
57
/// <summary>
53
58
/// Update the Address of the Proxy to modify the client Proxy
54
59
/// </summary>
55
- public static void UpdateProxy ( )
56
- // TODO: need test with a proxy
60
+ public static void UpdateProxy ( ProxyProperty property )
57
61
{
58
- if ( Proxy != null && Proxy . Enabled && ! string . IsNullOrEmpty ( Proxy . Server ) )
62
+ ( _proxy . Address , _proxy . Credentials ) = property switch
59
63
{
60
- if ( string . IsNullOrEmpty ( Proxy . UserName ) || string . IsNullOrEmpty ( Proxy . Password ) )
64
+ ProxyProperty . Enabled => ( Proxy . Enabled ) switch
61
65
{
62
- WebProxy . Address = new Uri ( $ "http:// { Proxy . Server } : { Proxy . Port } " ) ;
63
- WebProxy . Credentials = null ;
64
- }
65
- else
66
- {
67
- WebProxy . Address = new Uri ( $ "http:// { Proxy . Server } : { Proxy . Port } " ) ;
68
- WebProxy . Credentials = new NetworkCredential ( Proxy . UserName , Proxy . Password ) ;
69
- }
70
- }
71
- else
72
- {
73
- WebProxy . Address = new WebProxy ( ) . Address ;
74
- WebProxy . Credentials = null ;
75
- }
66
+ true => Proxy . UserName switch
67
+ {
68
+ var userName when ! string . IsNullOrEmpty ( userName ) =>
69
+ ( new Uri ( $ "http:// { Proxy . Server } : { Proxy . Port } " ) , null ) ,
70
+ _ => ( new Uri ( $ "http:// { Proxy . Server } : { Proxy . Port } " ) ,
71
+ new NetworkCredential ( Proxy . UserName , Proxy . Password ) )
72
+ } ,
73
+ false => ( null , null )
74
+ } ,
75
+ ProxyProperty . Server => ( new Uri ( $ "http:// { Proxy . Server } : { Proxy . Port } " ) , _proxy . Credentials ) ,
76
+ ProxyProperty . Port => ( new Uri ( $ "http:// { Proxy . Server } : { Proxy . Port } " ) , _proxy . Credentials ) ,
77
+ ProxyProperty . UserName => ( _proxy . Address , new NetworkCredential ( Proxy . UserName , Proxy . Password ) ) ,
78
+ ProxyProperty . Password => ( _proxy . Address , new NetworkCredential ( Proxy . UserName , Proxy . Password ) )
79
+ } ;
76
80
}
77
81
78
- public async static Task Download ( [ NotNull ] string url , [ NotNull ] string filePath )
82
+ public static async Task Download ( [ NotNull ] string url , [ NotNull ] string filePath )
79
83
{
80
84
using var response = await client . GetAsync ( url ) ;
81
85
if ( response . StatusCode == HttpStatusCode . OK )
82
86
{
83
- using var fileStream = new FileStream ( filePath , FileMode . CreateNew ) ;
87
+ await using var fileStream = new FileStream ( filePath , FileMode . CreateNew ) ;
84
88
await response . Content . CopyToAsync ( fileStream ) ;
85
89
}
86
90
else
@@ -93,7 +97,7 @@ public static async Task<string> Get([NotNull] string url, string encoding = "UT
93
97
{
94
98
Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
95
99
var response = await client . GetAsync ( url ) ;
96
- using var stream = await response . Content . ReadAsStreamAsync ( ) ;
100
+ await using var stream = await response . Content . ReadAsStreamAsync ( ) ;
97
101
using var reader = new StreamReader ( stream , Encoding . GetEncoding ( encoding ) ) ;
98
102
var content = await reader . ReadToEndAsync ( ) ;
99
103
if ( response . StatusCode == HttpStatusCode . OK )
@@ -102,7 +106,8 @@ public static async Task<string> Get([NotNull] string url, string encoding = "UT
102
106
}
103
107
else
104
108
{
105
- throw new HttpRequestException ( $ "Error code <{ response . StatusCode } > with content <{ content } > returned from <{ url } >") ;
109
+ throw new HttpRequestException (
110
+ $ "Error code <{ response . StatusCode } > with content <{ content } > returned from <{ url } >") ;
106
111
}
107
112
}
108
113
}
0 commit comments