Skip to content

Commit

Permalink
Merge pull request #499 from qiniu/add_remote_ip
Browse files Browse the repository at this point in the history
Add remote ip
  • Loading branch information
sxci authored Nov 18, 2020
2 parents eb7fbb6 + 7e5dfac commit de4b6e0
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/main/java/com/qiniu/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.UnknownHostException;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -69,17 +71,42 @@ public Client(final Dns dns, final boolean hostFirst, final ProxyConfiguration p

builder.dispatcher(dispatcher);
builder.connectionPool(connectionPool);
builder.eventListener(new EventListener() {
@Override
public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) {
Request req = call.request();
IpTag tag = (IpTag) req.tag();
tag.ip = inetSocketAddress + "";
}
});
builder.addNetworkInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();

okhttp3.Response response = chain.proceed(request);
IpTag tag = (IpTag) request.tag();
try {
tag.ip = chain.connection().socket().getRemoteSocketAddress().toString();
tag.ip = chain.connection().socket().getRemoteSocketAddress() + "";
} catch (Exception e) {
tag.ip = "";
// ingore
}
return response;
}
});
builder.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
okhttp3.Response response = null;
IOException ex = null;
try {
response = chain.proceed(request);
} catch (IOException e) {
IpTag tag = (IpTag) request.tag();
ex = new IOException(e + " on " + tag.ip, e);
}
if (ex != null) {
throw ex;
}
return response;
}
Expand Down Expand Up @@ -445,6 +472,6 @@ public void accept(String key, Object value) {
}

private static class IpTag {
public String ip = null;
public String ip = "";
}
}
90 changes: 90 additions & 0 deletions src/test/java/test/com/qiniu/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import com.qiniu.http.Client;
import com.qiniu.http.ProxyConfiguration;
import com.qiniu.http.Response;
import okhttp3.OkHttpClient;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;

public class HttpTest {
private Client httpManager = new Client();
Expand Down Expand Up @@ -93,4 +98,89 @@ public void testProxy() {
Assert.assertEquals(e.response.statusCode, 400);
}
}

@Test
public void testTimeout() throws NoSuchFieldException, IllegalAccessException {
Client client0 = new Client();
try {
Response res = client0.get("https://www.qiniu.com/?v=12345");
String r = res.toString();
System.out.println(r);
Assert.assertTrue("https, must have port 443", r.indexOf(":443") > 0);
} catch (QiniuException e) {
e.printStackTrace();
Assert.fail("should be ok");
}

try {
Response res = client0.get("https://www.qiniu.com/?v=12345");
String r = res.toString();
System.out.println(r);
Assert.assertTrue("https, must have port 443", r.indexOf(":443") > 0);
} catch (QiniuException e) {
e.printStackTrace();
Assert.fail("should be ok");
}

try {
Response res = client0.get("https://www.qiniu.com/?v=12345");
String r = res.toString();
System.out.println(r);
Assert.assertTrue("https, must have port 443", r.indexOf(":443") > 0);
} catch (QiniuException e) {
e.printStackTrace();
Assert.fail("should be ok");
}

Client client = new Client();
Field field = client.getClass().getDeclaredField("httpClient");
field.setAccessible(true);
OkHttpClient okHttpClient = (OkHttpClient) field.get(client);
okHttpClient = okHttpClient.newBuilder().connectTimeout(3, TimeUnit.MILLISECONDS).build();
field.set(client, okHttpClient);

try {
client.get("http://rs.qbox.me/?v=12");
Assert.fail("should be timeout");
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue("http, must have port 80", e.getMessage().indexOf(":80") > 10);
}
try {
client.get("http://rs.qbox.me/?v=12");
Assert.fail("should be timeout");
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue("http, must have port 80", e.getMessage().indexOf(":80") > 10);
}
try {
client.get("http://rs.qbox.me/?v=12");
Assert.fail("should be timeout");
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue("http, must have port 80", e.getMessage().indexOf(":80") > 10);
}
try {
client.get("https://rs.qbox.me/?v=we");
Assert.fail("should be timeout");
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue("https, must have port 443", e.getMessage().indexOf(":443") > 10);
}

try {
client.get("http://www.qiniu.com/?v=543");
Assert.fail("should be timeout");
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue("http, must have port 80", e.getMessage().indexOf(":80") > 10);
}
try {
client.get("https://www.qiniu.com/?v=kgd");
Assert.fail("should be timeout");
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue("https, must have port 443", e.getMessage().indexOf(":443") > 10);
}
}
}
1 change: 1 addition & 0 deletions src/test/java/test/com/qiniu/storage/BucketTest2.java
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ public void testBatch() {
bucketManager.copy(bucket, key, bucket, moveFromKey);
bucketManager.copy(bucket, key, bucket, moveFromKey2);
} catch (QiniuException e) {
e.printStackTrace();
Assert.fail(e.response.toString());
}

Expand Down

0 comments on commit de4b6e0

Please sign in to comment.