jimu-decompiled/sources/okhttp3/internal/platform/JdkWithJettyBootPlatform.java
2025-05-13 19:24:51 +02:00

121 lines
5.0 KiB
Java

package okhttp3.internal.platform;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import javax.net.ssl.SSLSocket;
import okhttp3.Protocol;
import okhttp3.internal.Util;
/* loaded from: classes2.dex */
class JdkWithJettyBootPlatform extends Platform {
private final Class<?> clientProviderClass;
private final Method getMethod;
private final Method putMethod;
private final Method removeMethod;
private final Class<?> serverProviderClass;
private static class JettyNegoProvider implements InvocationHandler {
private final List<String> protocols;
String selected;
boolean unsupported;
JettyNegoProvider(List<String> list) {
this.protocols = list;
}
@Override // java.lang.reflect.InvocationHandler
public Object invoke(Object obj, Method method, Object[] objArr) throws Throwable {
String name = method.getName();
Class<?> returnType = method.getReturnType();
if (objArr == null) {
objArr = Util.EMPTY_STRING_ARRAY;
}
if (name.equals("supports") && Boolean.TYPE == returnType) {
return true;
}
if (name.equals("unsupported") && Void.TYPE == returnType) {
this.unsupported = true;
return null;
}
if (name.equals("protocols") && objArr.length == 0) {
return this.protocols;
}
if ((!name.equals("selectProtocol") && !name.equals("select")) || String.class != returnType || objArr.length != 1 || !(objArr[0] instanceof List)) {
if ((!name.equals("protocolSelected") && !name.equals("selected")) || objArr.length != 1) {
return method.invoke(this, objArr);
}
this.selected = (String) objArr[0];
return null;
}
List list = (List) objArr[0];
int size = list.size();
for (int i = 0; i < size; i++) {
if (this.protocols.contains(list.get(i))) {
String str = (String) list.get(i);
this.selected = str;
return str;
}
}
String str2 = this.protocols.get(0);
this.selected = str2;
return str2;
}
}
JdkWithJettyBootPlatform(Method method, Method method2, Method method3, Class<?> cls, Class<?> cls2) {
this.putMethod = method;
this.getMethod = method2;
this.removeMethod = method3;
this.clientProviderClass = cls;
this.serverProviderClass = cls2;
}
public static Platform buildIfSupported() {
try {
Class<?> cls = Class.forName("org.eclipse.jetty.alpn.ALPN");
Class<?> cls2 = Class.forName("org.eclipse.jetty.alpn.ALPN$Provider");
return new JdkWithJettyBootPlatform(cls.getMethod("put", SSLSocket.class, cls2), cls.getMethod("get", SSLSocket.class), cls.getMethod("remove", SSLSocket.class), Class.forName("org.eclipse.jetty.alpn.ALPN$ClientProvider"), Class.forName("org.eclipse.jetty.alpn.ALPN$ServerProvider"));
} catch (ClassNotFoundException | NoSuchMethodException unused) {
return null;
}
}
@Override // okhttp3.internal.platform.Platform
public void afterHandshake(SSLSocket sSLSocket) {
try {
this.removeMethod.invoke(null, sSLSocket);
} catch (IllegalAccessException | InvocationTargetException e) {
throw Util.assertionError("unable to remove alpn", e);
}
}
@Override // okhttp3.internal.platform.Platform
public void configureTlsExtensions(SSLSocket sSLSocket, String str, List<Protocol> list) {
try {
this.putMethod.invoke(null, sSLSocket, Proxy.newProxyInstance(Platform.class.getClassLoader(), new Class[]{this.clientProviderClass, this.serverProviderClass}, new JettyNegoProvider(Platform.alpnProtocolNames(list))));
} catch (IllegalAccessException | InvocationTargetException e) {
throw Util.assertionError("unable to set alpn", e);
}
}
@Override // okhttp3.internal.platform.Platform
public String getSelectedProtocol(SSLSocket sSLSocket) {
try {
JettyNegoProvider jettyNegoProvider = (JettyNegoProvider) Proxy.getInvocationHandler(this.getMethod.invoke(null, sSLSocket));
if (!jettyNegoProvider.unsupported && jettyNegoProvider.selected == null) {
Platform.get().log(4, "ALPN callback dropped: HTTP/2 is disabled. Is alpn-boot on the boot class path?", null);
return null;
}
if (jettyNegoProvider.unsupported) {
return null;
}
return jettyNegoProvider.selected;
} catch (IllegalAccessException | InvocationTargetException e) {
throw Util.assertionError("unable to get selected protocol", e);
}
}
}