diff --git a/okhttp-tests/src/test/java/okhttp3/DelegatingSocketFactory.java b/okhttp-tests/src/test/java/okhttp3/DelegatingSocketFactory.java index 33ac2f4e1ada..e94ec3fda86f 100644 --- a/okhttp-tests/src/test/java/okhttp3/DelegatingSocketFactory.java +++ b/okhttp-tests/src/test/java/okhttp3/DelegatingSocketFactory.java @@ -58,7 +58,7 @@ public DelegatingSocketFactory(SocketFactory delegate) { return configureSocket(socket); } - protected Socket configureSocket(Socket socket) throws IOException { + public Socket configureSocket(Socket socket) throws IOException { // No-op by default. return socket; } diff --git a/okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java b/okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java index 29558b123ffc..c15fb605d54e 100644 --- a/okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java +++ b/okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java @@ -2533,7 +2533,7 @@ protected ServerSocket configureServerSocket(ServerSocket serverSocket) }); urlFactory.setClient(urlFactory.client().newBuilder() .socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) { - @Override protected Socket configureSocket(Socket socket) throws IOException { + @Override public Socket configureSocket(Socket socket) throws IOException { socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE); socket.setSendBufferSize(SOCKET_BUFFER_SIZE); return socket; diff --git a/okhttp-tests/src/test/java/okhttp3/XxX.java b/okhttp-tests/src/test/java/okhttp3/XxX.java new file mode 100644 index 000000000000..160d04fd0f50 --- /dev/null +++ b/okhttp-tests/src/test/java/okhttp3/XxX.java @@ -0,0 +1,39 @@ +package okhttp3; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.Socket; +import java.util.concurrent.TimeUnit; +import javax.net.SocketFactory; + +public class XxX { + public static void main(String[] args) throws IOException { + SocketFactory socketFactory = new MyDelegatingSocketFactory(); + OkHttpClient.Builder builder = new OkHttpClient.Builder(); + builder.proxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 2001))); + builder.connectTimeout(10, TimeUnit.SECONDS); + builder.socketFactory(socketFactory); + OkHttpClient client = builder.build(); + + Request req = new Request.Builder().url("https://google.com/robots.txt").build(); + try (Response response = client.newCall(req).execute()) { + System.out.println(response.body.string().length()); + } + } + + public static class MyDelegatingSocketFactory extends DelegatingSocketFactory { + public MyDelegatingSocketFactory() { + super(SocketFactory.getDefault()); + } + + @Override public Socket configureSocket(Socket socket) throws IOException { + // set android + //TrafficStats.tagSocket(socket); + + System.out.println("configured"); + + return socket; + } + } +} diff --git a/okhttp-tests/src/test/java/okhttp3/internal/http/DisconnectTest.java b/okhttp-tests/src/test/java/okhttp3/internal/http/DisconnectTest.java index aad2bfc8ddff..408abbb9468d 100644 --- a/okhttp-tests/src/test/java/okhttp3/internal/http/DisconnectTest.java +++ b/okhttp-tests/src/test/java/okhttp3/internal/http/DisconnectTest.java @@ -59,7 +59,7 @@ public final class DisconnectTest { }); client = defaultClient().newBuilder() .socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) { - @Override protected Socket configureSocket(Socket socket) throws IOException { + @Override public Socket configureSocket(Socket socket) throws IOException { socket.setSendBufferSize(SOCKET_BUFFER_SIZE); socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE); return socket; diff --git a/okhttp-tests/src/test/java/okhttp3/internal/http/ThreadInterruptTest.java b/okhttp-tests/src/test/java/okhttp3/internal/http/ThreadInterruptTest.java index 9ac3b93eccce..c5bb4b3c8473 100644 --- a/okhttp-tests/src/test/java/okhttp3/internal/http/ThreadInterruptTest.java +++ b/okhttp-tests/src/test/java/okhttp3/internal/http/ThreadInterruptTest.java @@ -61,8 +61,7 @@ protected ServerSocket configureServerSocket(ServerSocket serverSocket) }); client = defaultClient().newBuilder() .socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) { - @Override - protected Socket configureSocket(Socket socket) throws IOException { + @Override public Socket configureSocket(Socket socket) throws IOException { socket.setSendBufferSize(SOCKET_BUFFER_SIZE); socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE); return socket; diff --git a/okhttp/src/main/java/okhttp3/internal/connection/RealConnection.java b/okhttp/src/main/java/okhttp3/internal/connection/RealConnection.java index 6afb0fd0171a..dd6fdb384fb7 100644 --- a/okhttp/src/main/java/okhttp3/internal/connection/RealConnection.java +++ b/okhttp/src/main/java/okhttp3/internal/connection/RealConnection.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.lang.ref.Reference; +import java.lang.reflect.InvocationTargetException; import java.net.ConnectException; import java.net.ProtocolException; import java.net.Proxy; @@ -30,6 +31,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; +import javax.net.SocketFactory; import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; @@ -58,6 +60,7 @@ import okhttp3.internal.http2.Http2Codec; import okhttp3.internal.http2.Http2Connection; import okhttp3.internal.http2.Http2Stream; +import okhttp3.internal.platform.OptionalMethod; import okhttp3.internal.platform.Platform; import okhttp3.internal.tls.OkHostnameVerifier; import okhttp3.internal.ws.RealWebSocket; @@ -230,7 +233,7 @@ private void connectSocket(int connectTimeout, int readTimeout, Call call, rawSocket = proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP ? address.socketFactory().createSocket() - : new Socket(proxy); + : configure(address.socketFactory(), new Socket(proxy)); eventListener.connectStart(call, route.socketAddress(), proxy); rawSocket.setSoTimeout(readTimeout); @@ -256,6 +259,19 @@ private void connectSocket(int connectTimeout, int readTimeout, Call call, } } + private Socket configure(SocketFactory socketFactory, Socket socket) { + OptionalMethod m = + new OptionalMethod(Socket.class, "configureSocket", Socket.class); + + try { + m.invoke(socketFactory, socket); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + return socket; + } + private void establishProtocol(ConnectionSpecSelector connectionSpecSelector, Call call, EventListener eventListener) throws IOException { if (route.address().sslSocketFactory() == null) { diff --git a/okhttp/src/main/java/okhttp3/internal/platform/OptionalMethod.java b/okhttp/src/main/java/okhttp3/internal/platform/OptionalMethod.java index c26132fedb77..5f13a6a6aa32 100644 --- a/okhttp/src/main/java/okhttp3/internal/platform/OptionalMethod.java +++ b/okhttp/src/main/java/okhttp3/internal/platform/OptionalMethod.java @@ -26,7 +26,7 @@ * * @param the type of the object the method might be on, typically an interface or base class */ -class OptionalMethod { +public class OptionalMethod { /** The return type of the method. null means "don't care". */ private final Class returnType; @@ -42,7 +42,7 @@ class OptionalMethod { * @param methodName the name of the method * @param methodParams the method parameter types */ - OptionalMethod(Class returnType, String methodName, Class... methodParams) { + public OptionalMethod(Class returnType, String methodName, Class... methodParams) { this.returnType = returnType; this.methodName = methodName; this.methodParams = methodParams;