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,60 +33,54 @@ 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
- private static WebProxy _proxy = new WebProxy ( ) ;
51
- public static WebProxy WebProxy {
52
- get
53
- {
54
- UpdateProxy ( ) ;
55
- return _proxy ;
56
- }
50
+ private static readonly WebProxy _proxy = new WebProxy ( ) ;
51
+
52
+ public static WebProxy WebProxy
53
+ {
54
+ get { return _proxy ; }
57
55
}
58
56
59
57
/// <summary>
60
58
/// Update the Address of the Proxy to modify the client Proxy
61
59
/// </summary>
62
- public static void UpdateProxy ( )
63
- // TODO: need test with a proxy
60
+ public static void UpdateProxy ( ProxyProperty property )
64
61
{
65
- if ( Proxy != null && Proxy . Enabled && ! string . IsNullOrEmpty ( Proxy . Server ) )
62
+ ( _proxy . Address , _proxy . Credentials ) = property switch
66
63
{
67
- if ( string . IsNullOrEmpty ( Proxy . UserName ) || string . IsNullOrEmpty ( Proxy . Password ) )
64
+ ProxyProperty . Enabled => ( Proxy . Enabled ) switch
68
65
{
69
- WebProxy . Address = new Uri ( $ "http:// { Proxy . Server } : { Proxy . Port } " ) ;
70
- WebProxy . Credentials = null ;
71
- }
72
- else
73
- {
74
- WebProxy . Address = new Uri ( $ "http:// { Proxy . Server } : { Proxy . Port } " ) ;
75
- WebProxy . Credentials = new NetworkCredential ( Proxy . UserName , Proxy . Password ) ;
76
- }
77
- }
78
- else
79
- {
80
- WebProxy . Address = new WebProxy ( ) . Address ;
81
- WebProxy . Credentials = null ;
82
- }
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
+ } ;
83
80
}
84
81
85
82
public static async Task Download ( [ NotNull ] string url , [ NotNull ] string filePath )
86
83
{
87
- UpdateProxy ( ) ;
88
84
using var response = await client . GetAsync ( url ) ;
89
85
if ( response . StatusCode == HttpStatusCode . OK )
90
86
{
@@ -99,7 +95,6 @@ public static async Task Download([NotNull] string url, [NotNull] string filePat
99
95
100
96
public static async Task < string > Get ( [ NotNull ] string url , string encoding = "UTF-8" )
101
97
{
102
- UpdateProxy ( ) ;
103
98
Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
104
99
var response = await client . GetAsync ( url ) ;
105
100
await using var stream = await response . Content . ReadAsStreamAsync ( ) ;
@@ -111,7 +106,8 @@ public static async Task<string> Get([NotNull] string url, string encoding = "UT
111
106
}
112
107
else
113
108
{
114
- 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 } >") ;
115
111
}
116
112
}
117
113
}
0 commit comments