Skip to content

Commit f52e776

Browse files
authored
Merge pull request #360 from sxci/remove_happly-dns
remove happly-dns [ci skip]
2 parents ce7cfa3 + 9c9a8b7 commit f52e776

File tree

8 files changed

+239
-59
lines changed

8 files changed

+239
-59
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repositories {
1414
dependencies {
1515
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.9.1'
1616
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
17-
compile group: 'com.qiniu', name: 'happy-dns-java', version: '0.1.6'
17+
testCompile group: 'com.qiniu', name: 'happy-dns-java', version: '0.1.6'
1818
testCompile group: 'junit', name: 'junit', version: '4.12'
1919
}
2020

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@
77
import com.qiniu.util.StringUtils;
88
import okhttp3.*;
99
import okio.BufferedSink;
10-
import qiniu.happydns.DnsClient;
11-
import qiniu.happydns.Domain;
1210

1311
import java.io.File;
1412
import java.io.IOException;
1513
import java.net.InetAddress;
1614
import java.net.UnknownHostException;
17-
import java.util.ArrayList;
18-
import java.util.Collections;
1915
import java.util.List;
2016
import java.util.concurrent.TimeUnit;
2117

@@ -43,7 +39,7 @@ public Client() {
4339
* 构建一个自定义配置的 HTTP Client 类
4440
*/
4541
public Client(Configuration cfg) {
46-
this(cfg.dnsClient, cfg.useDnsHostFirst, cfg.proxy,
42+
this(cfg.dns, cfg.useDnsHostFirst, cfg.proxy,
4743
cfg.connectTimeout, cfg.readTimeout, cfg.writeTimeout,
4844
cfg.dispatcherMaxRequests, cfg.dispatcherMaxRequestsPerHost,
4945
cfg.connectionPoolMaxIdleCount, cfg.connectionPoolMaxIdleMinutes);
@@ -52,7 +48,7 @@ public Client(Configuration cfg) {
5248
/**
5349
* 构建一个自定义配置的 HTTP Client 类
5450
*/
55-
public Client(final DnsClient dns, final boolean hostFirst, final ProxyConfiguration proxy,
51+
public Client(final Dns dns, final boolean hostFirst, final ProxyConfiguration proxy,
5652
int connTimeout, int readTimeout, int writeTimeout, int dispatcherMaxRequests,
5753
int dispatcherMaxRequestsPerHost, int connectionPoolMaxIdleCount,
5854
int connectionPoolMaxIdleMinutes) {
@@ -82,23 +78,15 @@ public okhttp3.Response intercept(Chain chain) throws IOException {
8278
}
8379
});
8480
if (dns != null) {
85-
builder.dns(new Dns() {
81+
builder.dns(new okhttp3.Dns() {
8682
@Override
8783
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
88-
InetAddress[] ips;
89-
Domain domain = new Domain(hostname, false, hostFirst);
9084
try {
91-
ips = dns.queryInetAddress(domain);
92-
} catch (IOException e) {
85+
return dns.lookup(hostname);
86+
} catch (Exception e) {
9387
e.printStackTrace();
94-
throw new UnknownHostException(e.getMessage());
9588
}
96-
if (ips == null) {
97-
throw new UnknownHostException(hostname + " resolve failed");
98-
}
99-
List<InetAddress> l = new ArrayList<>();
100-
Collections.addAll(l, ips);
101-
return l;
89+
return okhttp3.Dns.SYSTEM.lookup(hostname);
10290
}
10391
});
10492
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.qiniu.http;
2+
3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
import java.util.List;
6+
7+
public interface Dns {
8+
List<InetAddress> lookup(String hostname) throws UnknownHostException;
9+
}

src/main/java/com/qiniu/storage/Configuration.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import com.qiniu.common.QiniuException;
55
import com.qiniu.common.Zone;
66
import com.qiniu.common.ZoneReqInfo;
7+
import com.qiniu.http.Dns;
78
import com.qiniu.http.ProxyConfiguration;
8-
import qiniu.happydns.DnsClient;
99

1010
/**
1111
* 该类封装了SDK相关配置参数
@@ -59,7 +59,7 @@ public final class Configuration implements Cloneable {
5959
/**
6060
* 外部dns
6161
*/
62-
public DnsClient dnsClient;
62+
public Dns dns;
6363
/*
6464
* 解析域名时,优先使用host配置,主要针对内部局域网配置
6565
*/
@@ -76,13 +76,11 @@ public final class Configuration implements Cloneable {
7676
public static String defaultApiHost = "api.qiniu.com";
7777

7878
public Configuration() {
79-
this.zone = null;
80-
this.dnsClient = null;
79+
8180
}
8281

8382
public Configuration(Zone zone) {
8483
this.zone = zone;
85-
this.dnsClient = null;
8684
}
8785

8886
public Configuration clone() {

src/main/java/com/qiniu/util/Hex.java

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one or more
4+
* contributor license agreements. See the NOTICE file distributed with
5+
* this work for additional information regarding copyright ownership.
6+
* The ASF licenses this file to You under the Apache License, Version 2.0
7+
* (the "License"); you may not use this file except in compliance with
8+
* the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.qiniu.util;
20+
21+
import java.io.IOException;
22+
23+
/**
24+
* Converts hexadecimal Strings.
25+
* This class is thread-safe.
26+
*
27+
* @version $Id: Hex.java 1619948 2014-08-22 22:53:55Z ggregory $
28+
* @since 1.1
29+
*/
30+
public final class Hex {
31+
32+
/**
33+
* Used to build output as Hex
34+
*/
35+
private static final char[] DIGITS_LOWER =
36+
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
37+
38+
/**
39+
* Used to build output as Hex
40+
*/
41+
private static final char[] DIGITS_UPPER =
42+
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
43+
44+
private Hex() {
45+
}
46+
47+
/**
48+
* Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The
49+
* returned array will be half the length of the passed array, as it takes two characters to represent any given
50+
* byte. An exception is thrown if the passed char array has an odd number of elements.
51+
*
52+
* @param data An array of characters containing hexadecimal digits
53+
* @return A byte array containing binary data decoded from the supplied char array.
54+
* @throws HexDecodeException Thrown if an odd number or illegal of characters is supplied
55+
*/
56+
public static byte[] decodeHex(final char[] data) throws HexDecodeException {
57+
58+
final int len = data.length;
59+
60+
if ((len & 0x01) != 0) {
61+
throw new HexDecodeException("Odd number of characters.");
62+
}
63+
64+
final byte[] out = new byte[len >> 1];
65+
66+
// two characters form the hex value.
67+
for (int i = 0, j = 0; j < len; i++) {
68+
int f = toDigit(data[j], j) << 4;
69+
j++;
70+
f = f | toDigit(data[j], j);
71+
j++;
72+
out[i] = (byte) (f & 0xFF);
73+
}
74+
75+
return out;
76+
}
77+
78+
/**
79+
* Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
80+
* The returned array will be double the length of the passed array, as it takes two characters to represent any
81+
* given byte.
82+
*
83+
* @param data a byte[] to convert to Hex characters
84+
* @return A char[] containing hexadecimal characters
85+
*/
86+
public static char[] encodeHex(final byte[] data) {
87+
return encodeHex(data, true);
88+
}
89+
90+
/**
91+
* Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
92+
* The returned array will be double the length of the passed array, as it takes two characters to represent any
93+
* given byte.
94+
*
95+
* @param data a byte[] to convert to Hex characters
96+
* @param toLowerCase <code>true</code> converts to lowercase, <code>false</code> to uppercase
97+
* @return A char[] containing hexadecimal characters
98+
* @since 1.4
99+
*/
100+
public static char[] encodeHex(final byte[] data, final boolean toLowerCase) {
101+
return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
102+
}
103+
104+
/**
105+
* Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
106+
* The returned array will be double the length of the passed array, as it takes two characters to represent any
107+
* given byte.
108+
*
109+
* @param data a byte[] to convert to Hex characters
110+
* @param toDigits the output alphabet
111+
* @return A char[] containing hexadecimal characters
112+
* @since 1.4
113+
*/
114+
private static char[] encodeHex(final byte[] data, final char[] toDigits) {
115+
final int l = data.length;
116+
final char[] out = new char[l << 1];
117+
// two characters form the hex value.
118+
for (int i = 0, j = 0; i < l; i++) {
119+
out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
120+
out[j++] = toDigits[0x0F & data[i]];
121+
}
122+
return out;
123+
}
124+
125+
/**
126+
* Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned
127+
* String will be double the length of the passed array, as it takes two characters to represent any given byte.
128+
*
129+
* @param data a byte[] to convert to Hex characters
130+
* @return A String containing hexadecimal characters
131+
* @since 1.4
132+
*/
133+
public static String encodeHexString(final byte[] data) {
134+
return new String(encodeHex(data));
135+
}
136+
137+
/**
138+
* Converts a hexadecimal character to an integer.
139+
*
140+
* @param ch A character to convert to an integer digit
141+
* @param index The index of the character in the source
142+
* @return An integer
143+
* @throws HexDecodeException Thrown if ch is an illegal hex character
144+
*/
145+
protected static int toDigit(final char ch, final int index) throws HexDecodeException {
146+
final int digit = Character.digit(ch, 16);
147+
if (digit == -1) {
148+
throw new HexDecodeException("Illegal hexadecimal character " + ch + " at index " + index);
149+
}
150+
return digit;
151+
}
152+
153+
public static class HexDecodeException extends IOException {
154+
public HexDecodeException(String msg) {
155+
super(msg);
156+
}
157+
}
158+
}
159+

src/main/java/com/qiniu/util/StringUtils.java

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

33
import com.qiniu.common.Constants;
4-
import qiniu.happydns.util.Hex;
54

65
import java.nio.charset.Charset;
76
import java.security.MessageDigest;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package test.com.qiniu;
2+
3+
import com.qiniu.common.Zone;
4+
import com.qiniu.http.Dns;
5+
import com.qiniu.storage.Configuration;
6+
import com.qiniu.storage.UploadManager;
7+
import org.junit.Test;
8+
import qiniu.happydns.DnsClient;
9+
import qiniu.happydns.Domain;
10+
import qiniu.happydns.IResolver;
11+
import qiniu.happydns.local.Resolver;
12+
import qiniu.happydns.local.SystemDnsServer;
13+
14+
import java.io.IOException;
15+
import java.net.InetAddress;
16+
import java.net.UnknownHostException;
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
import java.util.Collections;
20+
21+
// example
22+
public class DnsTest {
23+
private Configuration config;
24+
private UploadManager uploadManager;
25+
26+
public void setUp() throws UnknownHostException {
27+
IResolver[] resolvers = new IResolver[2];
28+
resolvers[0] = SystemDnsServer.defaultResolver();
29+
resolvers[1] = new Resolver(InetAddress.getByName("119.29.29.29"));
30+
final DnsClient dnsClient = new DnsClient(resolvers);
31+
32+
config = new Configuration();
33+
config.zone = Zone.zone0();
34+
config.dns = new Dns() {
35+
@Override
36+
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
37+
InetAddress[] ips;
38+
try{
39+
Domain domain = new Domain(hostname, true, true);
40+
ips = dnsClient.queryInetAddress(domain);
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
throw new UnknownHostException(e.getMessage());
44+
}
45+
if (ips == null) {
46+
throw new UnknownHostException(hostname + " resolve failed.");
47+
}
48+
List<InetAddress> l = new ArrayList<>(ips.length);
49+
Collections.addAll(l, ips);
50+
return l;
51+
}
52+
};
53+
54+
uploadManager = new UploadManager(config);
55+
}
56+
57+
@Test
58+
public void testSome() {
59+
// uploadManager.xxxxx
60+
}
61+
}

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

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
import com.qiniu.http.Response;
88
import org.junit.Assert;
99
import org.junit.Test;
10-
import qiniu.happydns.DnsClient;
11-
import qiniu.happydns.IResolver;
12-
import qiniu.happydns.local.Hosts;
13-
import qiniu.happydns.local.Resolver;
14-
import qiniu.happydns.local.SystemDnsServer;
15-
16-
import java.io.IOException;
17-
import java.net.InetAddress;
1810

1911

2012
public class HttpTest {
@@ -42,32 +34,6 @@ public void testPost2() {
4234
}
4335
}
4436

45-
@Test
46-
public void testDns() {
47-
IResolver r1 = SystemDnsServer.defaultResolver();
48-
IResolver r2 = null;
49-
try {
50-
r2 = new Resolver(InetAddress.getByName("119.29.29.29"));
51-
} catch (IOException ex) {
52-
ex.printStackTrace();
53-
}
54-
Hosts h = new Hosts();
55-
h.put("upnonodns.qiniu.com", "115.231.183.168");
56-
DnsClient dns = new DnsClient(new IResolver[]{r1, r2}, h);
57-
Client c = new Client(dns, false, null,
58-
Constants.CONNECT_TIMEOUT, Constants.READ_TIMEOUT, Constants.WRITE_TIMEOUT,
59-
Constants.DISPATCHER_MAX_REQUESTS, Constants.DISPATCHER_MAX_REQUESTS_PER_HOST,
60-
Constants.CONNECTION_POOL_MAX_IDLE_COUNT, Constants.CONNECTION_POOL_MAX_IDLE_MINUTES);
61-
Response r = null;
62-
try {
63-
r = c.post("http://upnonodns.qiniu.com", "hello", null);
64-
Assert.fail();
65-
} catch (QiniuException e) {
66-
Assert.assertNotNull(e.response.reqId);
67-
Assert.assertEquals(e.response.statusCode, 400);
68-
}
69-
}
70-
7137
@Test
7238
public void testPost3() {
7339
Response r = null;

0 commit comments

Comments
 (0)