Skip to content

Commit 2b32bb9

Browse files
committed
增加HttpProxy类,移除ProxyInfo
1 parent 9c4a2ef commit 2b32bb9

File tree

4 files changed

+113
-126
lines changed

4 files changed

+113
-126
lines changed

WebApiClient/Attributes/HttpActionAttributes/ProxyAttribute.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,9 @@ namespace WebApiClient.Attributes
1414
public class ProxyAttribute : ApiActionAttribute
1515
{
1616
/// <summary>
17-
/// 域名或ip
17+
/// http代理
1818
/// </summary>
19-
private readonly string host;
20-
21-
/// <summary>
22-
/// 端口
23-
/// </summary>
24-
private readonly int port;
25-
26-
/// <summary>
27-
/// 凭证
28-
/// </summary>
29-
private readonly ICredentials credential;
19+
private readonly HttpProxy httpProxy;
3020

3121
/// <summary>
3222
/// http代理描述
@@ -35,8 +25,8 @@ public class ProxyAttribute : ApiActionAttribute
3525
/// <param name="port">端口</param>
3626
/// <exception cref="ArgumentNullException"></exception>
3727
public ProxyAttribute(string host, int port)
38-
: this(host, port, null, null)
3928
{
29+
this.httpProxy = new HttpProxy(host, port);
4030
}
4131

4232
/// <summary>
@@ -49,18 +39,7 @@ public ProxyAttribute(string host, int port)
4939
/// <exception cref="ArgumentNullException"></exception>
5040
public ProxyAttribute(string host, int port, string userName, string password)
5141
{
52-
if (string.IsNullOrEmpty(host))
53-
{
54-
throw new ArgumentNullException(nameof(host));
55-
}
56-
57-
this.host = host;
58-
this.port = port;
59-
60-
if (string.IsNullOrEmpty(userName) == false && string.IsNullOrEmpty(password) == false)
61-
{
62-
this.credential = new NetworkCredential(userName, password);
63-
}
42+
this.httpProxy = new HttpProxy(host, port, userName, password);
6443
}
6544

6645
/// <summary>
@@ -70,11 +49,7 @@ public ProxyAttribute(string host, int port, string userName, string password)
7049
/// <returns></returns>
7150
public override Task BeforeRequestAsync(ApiActionContext context)
7251
{
73-
var proxy = new WebProxy(this.host, this.port)
74-
{
75-
Credentials = this.credential
76-
};
77-
context.HttpApiConfig.HttpClient.SetProxy(proxy);
52+
context.HttpApiConfig.HttpClient.SetProxy(this.httpProxy);
7853
return ApiTask.CompletedTask;
7954
}
8055
}

WebApiClient/ProxyInfo.cs renamed to WebApiClient/HttpProxy.cs

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Net;
53
using System.Text;
6-
using System.Threading.Tasks;
74

85
namespace WebApiClient
96
{
107
/// <summary>
11-
/// 表示代理信息
8+
/// 表示http代理信息
129
/// </summary>
13-
public class ProxyInfo
10+
public class HttpProxy : IWebProxy
1411
{
1512
/// <summary>
1613
/// 获取代理服务器域名或ip
@@ -25,32 +22,37 @@ public class ProxyInfo
2522
/// <summary>
2623
/// 获取代理服务器账号
2724
/// </summary>
28-
public string UserName { get; set; }
25+
public string UserName { get; private set; }
2926

3027
/// <summary>
3128
/// 获取代理服务器密码
3229
/// </summary>
33-
public string Password { get; set; }
30+
public string Password { get; private set; }
3431

3532
/// <summary>
36-
/// 代理信息
33+
/// 获取或设置授权信息
34+
/// </summary>
35+
ICredentials IWebProxy.Credentials { get; set; }
36+
37+
/// <summary>
38+
/// http代理信息
3739
/// </summary>
3840
/// <param name="proxyAddress">代理服务器地址</param>
3941
/// <exception cref="ArgumentNullException"></exception>
4042
/// <exception cref="ArgumentException"></exception>
4143
/// <exception cref="ArgumentOutOfRangeException"></exception>
4244
/// <exception cref="UriFormatException"></exception>
43-
public ProxyInfo(string proxyAddress)
45+
public HttpProxy(string proxyAddress)
4446
: this(new Uri(proxyAddress ?? throw new ArgumentNullException(nameof(proxyAddress))))
4547
{
4648
}
4749

4850
/// <summary>
49-
/// 代理信息
51+
/// http代理信息
5052
/// </summary>
5153
/// <param name="proxyAddress">代理服务器地址</param>
5254
/// <exception cref="ArgumentNullException"></exception>
53-
public ProxyInfo(Uri proxyAddress)
55+
public HttpProxy(Uri proxyAddress)
5456
{
5557
if (proxyAddress == null)
5658
{
@@ -61,15 +63,35 @@ public ProxyInfo(Uri proxyAddress)
6163
}
6264

6365
/// <summary>
64-
/// 代理信息
66+
/// http代理信息
67+
/// </summary>
68+
/// <param name="host">代理服务器域名或ip</param>
69+
/// <param name="port">代理服务器端口</param>
70+
/// <exception cref="ArgumentNullException"></exception>
71+
public HttpProxy(string host, int port)
72+
: this(host, port, null, null)
73+
{
74+
}
75+
76+
/// <summary>
77+
/// http代理信息
6578
/// </summary>
6679
/// <param name="host">代理服务器域名或ip</param>
6780
/// <param name="port">代理服务器端口</param>
81+
/// <param name="userName">代理服务器账号</param>
82+
/// <param name="password">代理服务器密码</param>
6883
/// <exception cref="ArgumentNullException"></exception>
69-
public ProxyInfo(string host, int port)
84+
public HttpProxy(string host, int port, string userName, string password)
7085
{
7186
this.Host = host ?? throw new ArgumentNullException(nameof(host));
72-
this.Port = Port;
87+
this.Port = port;
88+
this.UserName = userName;
89+
this.Password = password;
90+
91+
if (string.IsNullOrEmpty(userName + password) == false)
92+
{
93+
((IWebProxy)this).Credentials = new NetworkCredential(userName, password);
94+
}
7395
}
7496

7597
/// <summary>
@@ -79,7 +101,7 @@ public ProxyInfo(string host, int port)
79101
/// <param name="targetAddress">目标url地址</param>
80102
/// <exception cref="ArgumentNullException"></exception>
81103
/// <returns></returns>
82-
public static ProxyInfo FromWebProxy(IWebProxy webProxy, Uri targetAddress)
104+
public static HttpProxy FromWebProxy(IWebProxy webProxy, Uri targetAddress)
83105
{
84106
if (webProxy == null)
85107
{
@@ -92,15 +114,15 @@ public static ProxyInfo FromWebProxy(IWebProxy webProxy, Uri targetAddress)
92114
}
93115

94116
var proxyAddress = webProxy.GetProxy(targetAddress);
95-
var proxyInfo = new ProxyInfo(proxyAddress);
117+
var httpProxy = new HttpProxy(proxyAddress);
96118

97119
if (webProxy.Credentials != null)
98120
{
99121
var credentials = webProxy.Credentials.GetCredential(null, null);
100-
proxyInfo.UserName = credentials?.UserName;
101-
proxyInfo.Password = credentials?.Password;
122+
httpProxy.UserName = credentials?.UserName;
123+
httpProxy.Password = credentials?.Password;
102124
}
103-
return proxyInfo;
125+
return httpProxy;
104126
}
105127

106128
/// <summary>
@@ -109,7 +131,7 @@ public static ProxyInfo FromWebProxy(IWebProxy webProxy, Uri targetAddress)
109131
/// <param name="targetAddress">目标url地址</param>
110132
/// <exception cref="ArgumentNullException"></exception>
111133
/// <returns></returns>
112-
public string ToHttpTunnelRequestString(Uri targetAddress)
134+
public string ToTunnelRequestString(Uri targetAddress)
113135
{
114136
if (targetAddress == null)
115137
{
@@ -135,15 +157,23 @@ public string ToHttpTunnelRequestString(Uri targetAddress)
135157
}
136158

137159
/// <summary>
138-
/// 转换为web代理
160+
/// 获取代理服务器地址
139161
/// </summary>
162+
/// <param name="destination"></param>
140163
/// <returns></returns>
141-
public IWebProxy ToWebProxy()
164+
Uri IWebProxy.GetProxy(Uri destination)
142165
{
143-
return new WebProxy(this.Host, this.Port)
144-
{
145-
Credentials = new NetworkCredential(this.UserName, this.Password)
146-
};
166+
return new Uri($"http://{this.Host}:{this.Port}/");
167+
}
168+
169+
/// <summary>
170+
/// 是否忽略代理
171+
/// </summary>
172+
/// <param name="host"></param>
173+
/// <returns></returns>
174+
bool IWebProxy.IsBypassed(Uri host)
175+
{
176+
return false;
147177
}
148178
}
149179
}

WebApiClient/Internal/SocketExtend.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static class SocketExtend
2121
/// <exception cref="ArgumentNullException"></exception>
2222
/// <exception cref="TimeoutException"></exception>
2323
/// <returns></returns>
24-
public static async Task ConnectTaskAsync(this Socket socket, EndPoint remoteEndPoint, TimeSpan timeout)
24+
public static async Task ConnectTaskAsync(this Socket socket, EndPoint remoteEndPoint, TimeSpan? timeout)
2525
{
2626
if (remoteEndPoint == null)
2727
{
@@ -72,7 +72,7 @@ private static void ConnectCompleted(object sender, SocketAsyncEventArgs e)
7272
/// <exception cref="SocketException"></exception>
7373
/// <exception cref="TimeoutException"></exception>
7474
/// <returns></returns>
75-
public static async Task<int> ReceiveTaskAsync(this Socket socket, ArraySegment<byte> arraySegment, TimeSpan timeout)
75+
public static async Task<int> ReceiveTaskAsync(this Socket socket, ArraySegment<byte> arraySegment, TimeSpan? timeout)
7676
{
7777
var taskSetter = new TaskSetter<int>(timeout);
7878
var asyncEventArg = new SocketAsyncEventArgs
@@ -125,17 +125,6 @@ private class TaskSetter<TResult> : IDisposable
125125
/// </summary>
126126
private readonly CancellationTokenSource tokenSource;
127127

128-
/// <summary>
129-
/// 获取任务的返回值类型
130-
/// </summary>
131-
public Type ValueType
132-
{
133-
get
134-
{
135-
return typeof(TResult);
136-
}
137-
}
138-
139128
/// <summary>
140129
/// 获取任务对象
141130
/// </summary>
@@ -150,12 +139,15 @@ public Task<TResult> Task
150139
/// <summary>
151140
/// 任务行为
152141
/// </summary>
153-
public TaskSetter(TimeSpan timeout)
142+
public TaskSetter(TimeSpan? timeout)
154143
{
155144
this.taskSource = new TaskCompletionSource<TResult>();
156-
this.tokenSource = new CancellationTokenSource();
157-
this.tokenSource.Token.Register(() => this.SetException(new TimeoutException()));
158-
this.tokenSource.CancelAfter(timeout);
145+
if (timeout.HasValue == true)
146+
{
147+
this.tokenSource = new CancellationTokenSource();
148+
this.tokenSource.Token.Register(() => this.SetException(new TimeoutException()));
149+
this.tokenSource.CancelAfter(timeout.Value);
150+
}
159151
}
160152

161153
/// <summary>
@@ -165,7 +157,7 @@ public TaskSetter(TimeSpan timeout)
165157
/// <returns></returns>
166158
public bool SetResult(TResult value)
167159
{
168-
this.tokenSource.Dispose();
160+
this.tokenSource?.Dispose();
169161
return this.taskSource.TrySetResult(value);
170162
}
171163

@@ -176,7 +168,7 @@ public bool SetResult(TResult value)
176168
/// <returns></returns>
177169
public bool SetException(Exception ex)
178170
{
179-
this.tokenSource.Dispose();
171+
this.tokenSource?.Dispose();
180172
return this.taskSource.TrySetException(ex);
181173
}
182174

@@ -185,7 +177,7 @@ public bool SetException(Exception ex)
185177
/// </summary>
186178
public void Dispose()
187179
{
188-
this.tokenSource.Dispose();
180+
this.tokenSource?.Dispose();
189181
}
190182
}
191183
}

0 commit comments

Comments
 (0)