Skip to content

Commit f36629b

Browse files
authored
Merge pull request #220 from longbai/proxy
add proxy
2 parents 66fefe2 + f7c851c commit f36629b

File tree

5 files changed

+85
-26
lines changed

5 files changed

+85
-26
lines changed

src/main/java/com/qiniu/common/Config.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.qiniu.common;
22

3+
import com.qiniu.http.ProxyConfiguration;
34
import qiniu.happydns.DnsClient;
45

56
import java.nio.charset.Charset;
@@ -55,11 +56,14 @@ public final class Config {
5556
* 上传失败重试次数
5657
*/
5758
public static int RETRY_MAX = 5;
58-
5959
/**
6060
* 外部dns
6161
*/
6262
public static DnsClient dns = null;
63+
/**
64+
* proxy
65+
*/
66+
public static ProxyConfiguration proxy = null;
6367

6468
private Config() {
6569
}

src/main/java/com/qiniu/http/Client.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ public List<InetAddress> lookup(String hostname) throws UnknownHostException {
7474
}
7575
});
7676
}
77+
final ProxyConfiguration p = Config.proxy;
78+
if (p != null) {
79+
builder.proxy(p.proxy());
80+
if (p.user != null && p.password != null) {
81+
builder.proxyAuthenticator(p.authenticator());
82+
}
83+
}
7784
builder.connectTimeout(Config.CONNECT_TIMEOUT, TimeUnit.SECONDS);
7885
builder.readTimeout(Config.RESPONSE_TIMEOUT, TimeUnit.SECONDS);
7986
builder.writeTimeout(Config.WRITE_TIMEOUT, TimeUnit.SECONDS);

src/main/java/com/qiniu/http/Proxy.java

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.qiniu.http;
2+
3+
import okhttp3.Authenticator;
4+
import okhttp3.Credentials;
5+
import okhttp3.Route;
6+
7+
import java.io.IOException;
8+
import java.net.InetSocketAddress;
9+
import java.net.Proxy;
10+
11+
/**
12+
* http 代理
13+
*/
14+
public final class ProxyConfiguration {
15+
16+
public final String hostAddress;
17+
public final int port;
18+
public final String user;
19+
public final String password;
20+
public final Proxy.Type type;
21+
22+
/**
23+
* @param hostAddress 服务器域名或IP,比如proxy.com, 192.168.1.1
24+
* @param port 端口
25+
* @param user 用户名,无则填null
26+
* @param password 用户密码,无则填null
27+
* @param type
28+
*/
29+
public ProxyConfiguration(String hostAddress, int port, String user, String password, Proxy.Type type) {
30+
this.hostAddress = hostAddress;
31+
this.port = port;
32+
this.user = user;
33+
this.password = password;
34+
this.type = type;
35+
}
36+
37+
public ProxyConfiguration(String hostAddress, int port) {
38+
this(hostAddress, port, null, null, Proxy.Type.HTTP);
39+
}
40+
41+
Proxy proxy() {
42+
return new Proxy(type, new InetSocketAddress(hostAddress, port));
43+
}
44+
45+
Authenticator authenticator() {
46+
return new Authenticator() {
47+
@Override
48+
public okhttp3.Request authenticate(Route route, okhttp3.Response response) throws IOException {
49+
String credential = Credentials.basic(user, password);
50+
return response.request().newBuilder().
51+
header("Proxy-Authorization", credential).
52+
header("Proxy-Connection", "Keep-Alive").build();
53+
}
54+
};
55+
}
56+
}

src/test/java/com/qiniu/HttpTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.qiniu.common.Config;
44
import com.qiniu.common.QiniuException;
55
import com.qiniu.http.Client;
6+
import com.qiniu.http.ProxyConfiguration;
67
import com.qiniu.http.Response;
78
import org.junit.Assert;
89
import org.junit.Test;
@@ -59,7 +60,9 @@ public void testDns() {
5960
Assert.fail();
6061
} catch (QiniuException e) {
6162
Assert.assertNotNull(e.response.reqId);
63+
Assert.assertEquals(e.response.statusCode, 400);
6264
}
65+
Config.dns = null;
6366
}
6467

6568
@Test
@@ -94,4 +97,18 @@ public void testPost5() {
9497
Assert.fail();
9598
}
9699
}
100+
101+
@Test
102+
public void testProxy() {
103+
Config.proxy = new ProxyConfiguration("115.231.183.168", 80);
104+
Response r = null;
105+
try {
106+
r = new Client().post("http://upproxy1.qiniu.com", "hello", null);
107+
Assert.fail();
108+
} catch (QiniuException e) {
109+
Assert.assertNotNull(e.response.reqId);
110+
Assert.assertEquals(e.response.statusCode, 400);
111+
}
112+
Config.proxy = null;
113+
}
97114
}

0 commit comments

Comments
 (0)