From ecc295f3a086ff7f3a4e464c1735d6f6ad6bcba4 Mon Sep 17 00:00:00 2001 From: Sxci Date: Fri, 13 Nov 2020 14:30:34 +0800 Subject: [PATCH 1/6] add remote address even socket timeout --- src/main/java/com/qiniu/http/Client.java | 35 +++++++++--- src/test/java/test/com/qiniu/HttpTest.java | 55 +++++++++++++++++++ .../test/com/qiniu/storage/BucketTest2.java | 1 + 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/qiniu/http/Client.java b/src/main/java/com/qiniu/http/Client.java index ee47f3390..612f697f5 100755 --- a/src/main/java/com/qiniu/http/Client.java +++ b/src/main/java/com/qiniu/http/Client.java @@ -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; @@ -69,17 +71,34 @@ public Client(final Dns dns, final boolean hostFirst, final ProxyConfiguration p builder.dispatcher(dispatcher); builder.connectionPool(connectionPool); - builder.addNetworkInterceptor(new Interceptor() { + builder.eventListener(new EventListener() { + @Override + public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) { + Request req = call.request(); + if (req != null) { + IpTag tag = (IpTag) req.tag(); + tag.ip = inetSocketAddress + ""; + } + } + }); + builder.addInterceptor(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(); + okhttp3.Response response = null; + IOException ex = null; try { - tag.ip = chain.connection().socket().getRemoteSocketAddress().toString(); - } catch (Exception e) { - tag.ip = ""; + response = chain.proceed(request); + } catch (IOException e) { + if (e instanceof java.net.ConnectException) { + ex = e; + } else { + IpTag tag = (IpTag) request.tag(); + ex = new IOException(e + " on " + tag.ip, e); + } + } + if (ex != null) { + throw ex; } return response; } @@ -445,6 +464,6 @@ public void accept(String key, Object value) { } private static class IpTag { - public String ip = null; + public String ip = ""; } } diff --git a/src/test/java/test/com/qiniu/HttpTest.java b/src/test/java/test/com/qiniu/HttpTest.java index f2b9fd486..92fbd8a14 100644 --- a/src/test/java/test/com/qiniu/HttpTest.java +++ b/src/test/java/test/com/qiniu/HttpTest.java @@ -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(); @@ -93,4 +98,54 @@ public void testProxy() { Assert.assertEquals(e.response.statusCode, 400); } } + + @Test + public void testTimeout() throws NoSuchFieldException, IllegalAccessException { + try { + Response res = new Client().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("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); + } + } } diff --git a/src/test/java/test/com/qiniu/storage/BucketTest2.java b/src/test/java/test/com/qiniu/storage/BucketTest2.java index cf1e40078..aed82c885 100644 --- a/src/test/java/test/com/qiniu/storage/BucketTest2.java +++ b/src/test/java/test/com/qiniu/storage/BucketTest2.java @@ -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()); } From 002bf03cff64f1c9f22a94a6c4b77d90c1eb1620 Mon Sep 17 00:00:00 2001 From: Sxci Date: Fri, 13 Nov 2020 14:33:04 +0800 Subject: [PATCH 2/6] x --- src/main/java/com/qiniu/http/Client.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/qiniu/http/Client.java b/src/main/java/com/qiniu/http/Client.java index 612f697f5..c1094e093 100755 --- a/src/main/java/com/qiniu/http/Client.java +++ b/src/main/java/com/qiniu/http/Client.java @@ -90,12 +90,8 @@ public okhttp3.Response intercept(Chain chain) throws IOException { try { response = chain.proceed(request); } catch (IOException e) { - if (e instanceof java.net.ConnectException) { - ex = e; - } else { - IpTag tag = (IpTag) request.tag(); - ex = new IOException(e + " on " + tag.ip, e); - } + IpTag tag = (IpTag) request.tag(); + ex = new IOException(e + " on " + tag.ip, e); } if (ex != null) { throw ex; From af316fe91512204b7300660c2f1f6c7cd43cb9bb Mon Sep 17 00:00:00 2001 From: Sxci Date: Fri, 13 Nov 2020 14:47:40 +0800 Subject: [PATCH 3/6] iptag would not be null --- src/main/java/com/qiniu/http/Client.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/qiniu/http/Client.java b/src/main/java/com/qiniu/http/Client.java index c1094e093..e1672d56e 100755 --- a/src/main/java/com/qiniu/http/Client.java +++ b/src/main/java/com/qiniu/http/Client.java @@ -75,10 +75,8 @@ public Client(final Dns dns, final boolean hostFirst, final ProxyConfiguration p @Override public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) { Request req = call.request(); - if (req != null) { - IpTag tag = (IpTag) req.tag(); - tag.ip = inetSocketAddress + ""; - } + IpTag tag = (IpTag) req.tag(); + tag.ip = inetSocketAddress + ""; } }); builder.addInterceptor(new Interceptor() { From d0a5ab5109a4f355c85f9f461516de5a63dc394f Mon Sep 17 00:00:00 2001 From: Sxci Date: Fri, 13 Nov 2020 15:04:10 +0800 Subject: [PATCH 4/6] add remote address when reuse connection --- src/main/java/com/qiniu/http/Client.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/com/qiniu/http/Client.java b/src/main/java/com/qiniu/http/Client.java index e1672d56e..e029fc63d 100755 --- a/src/main/java/com/qiniu/http/Client.java +++ b/src/main/java/com/qiniu/http/Client.java @@ -79,6 +79,23 @@ public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy p 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(); + if (tag.ip != null && tag.ip.length() > 5) { + return response; + } + try { + tag.ip = chain.connection().socket().getRemoteSocketAddress().toString(); + } catch (Exception e) { + // ingore + } + return response; + } + }); builder.addInterceptor(new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { From 1dc3aa7ee0082a93c7856eff7871bd89e95f1816 Mon Sep 17 00:00:00 2001 From: Sxci Date: Fri, 13 Nov 2020 15:10:46 +0800 Subject: [PATCH 5/6] x --- src/main/java/com/qiniu/http/Client.java | 2 +- src/test/java/test/com/qiniu/HttpTest.java | 37 +++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/qiniu/http/Client.java b/src/main/java/com/qiniu/http/Client.java index e029fc63d..4e88f6362 100755 --- a/src/main/java/com/qiniu/http/Client.java +++ b/src/main/java/com/qiniu/http/Client.java @@ -89,7 +89,7 @@ public okhttp3.Response intercept(Chain chain) throws IOException { return response; } try { - tag.ip = chain.connection().socket().getRemoteSocketAddress().toString(); + tag.ip = chain.connection().socket().getRemoteSocketAddress() + ""; } catch (Exception e) { // ingore } diff --git a/src/test/java/test/com/qiniu/HttpTest.java b/src/test/java/test/com/qiniu/HttpTest.java index 92fbd8a14..33f21cfcf 100644 --- a/src/test/java/test/com/qiniu/HttpTest.java +++ b/src/test/java/test/com/qiniu/HttpTest.java @@ -101,8 +101,29 @@ public void testProxy() { @Test public void testTimeout() throws NoSuchFieldException, IllegalAccessException { + Client client0 = new Client(); try { - Response res = new Client().get("https://www.qiniu.com/?v=12345"); + 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); @@ -118,6 +139,20 @@ public void testTimeout() throws NoSuchFieldException, IllegalAccessException { 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"); From 7e5dfac2aafd44688127207e099c8d1109f73299 Mon Sep 17 00:00:00 2001 From: Sxci Date: Fri, 13 Nov 2020 23:33:17 +0800 Subject: [PATCH 6/6] update ip --- src/main/java/com/qiniu/http/Client.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/qiniu/http/Client.java b/src/main/java/com/qiniu/http/Client.java index 4e88f6362..51c78eddb 100755 --- a/src/main/java/com/qiniu/http/Client.java +++ b/src/main/java/com/qiniu/http/Client.java @@ -85,9 +85,6 @@ public okhttp3.Response intercept(Chain chain) throws IOException { Request request = chain.request(); okhttp3.Response response = chain.proceed(request); IpTag tag = (IpTag) request.tag(); - if (tag.ip != null && tag.ip.length() > 5) { - return response; - } try { tag.ip = chain.connection().socket().getRemoteSocketAddress() + ""; } catch (Exception e) {