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

RFC: example configure hook #3600

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions okhttp-tests/src/test/java/okhttp3/XxX.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -256,6 +259,19 @@ private void connectSocket(int connectTimeout, int readTimeout, Call call,
}
}

private Socket configure(SocketFactory socketFactory, Socket socket) {
OptionalMethod<SocketFactory> m =
new OptionalMethod<SocketFactory>(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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @param <T> the type of the object the method might be on, typically an interface or base class
*/
class OptionalMethod<T> {
public class OptionalMethod<T> {

/** The return type of the method. null means "don't care". */
private final Class<?> returnType;
Expand All @@ -42,7 +42,7 @@ class OptionalMethod<T> {
* @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;
Expand Down