Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remote ip #499

Merged
merged 6 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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