QQ浏览器 v12.2.3.7053版本的 MD5 值为:8285ab3059e5c8b521a264dfbc5c3685
以下内容为反编译后的 QBOKHttpUrlConnection.java 源代码,内容仅作参考
package com.tencent.mtt.network.http;
import android.os.Bundle;
import android.webkit.ValueCallback;
import com.tencent.common.http.HttpTimeRecord;
import com.tencent.common.http.MttRequestBase;
import com.tencent.common.threadpool.BrowserExecutorSupplier;
import com.tencent.mtt.apkplugin.impl.IAPInjectService;
import com.tencent.mtt.external.novel.facade.INovelService;
import com.tencent.mtt.network.oksupport.QBOKConnection;
import com.tencent.mtt.network.queen.KingOkHttpEventListener;
import com.tencent.mtt.network.queen.inerceptor.KingCardNetworkInterceptor;
import com.tencent.mtt.network.queen.inerceptor.KingCardRequestInterceptor;
import com.tencent.mtt.network.queen.inerceptor.KingFlowInterceptor;
import com.tencent.mtt.network.tbsnet.TbsNetLiveLogger;
import com.tencent.tbs.common.internal.service.StatServerHolder;
import com.xiaomi.mipush.sdk.Constants;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.SocketPermission;
import java.net.URL;
import java.security.Permission;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import okhttp3.Call;
import okhttp3.Dispatcher;
import okhttp3.EventListener;
import okhttp3.Handshake;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.JavaNetHeaders;
import okhttp3.internal.OKHttpConnectInfo;
import okhttp3.internal.URLFilter;
import okhttp3.internal.Util;
import okhttp3.internal.http.HttpDate;
import okhttp3.internal.http.HttpHeaders;
import okhttp3.internal.http.HttpMethod;
import okhttp3.internal.http.StatusLine;
import okhttp3.internal.huc.BufferedRequestBody;
import okhttp3.internal.huc.OutputStreamRequestBody;
import okhttp3.internal.huc.StreamedRequestBody;
import okhttp3.internal.platform.Platform;
public class QBOKHttpUrlConnection extends QBOKConnection {
URLFilter f68426a;
Proxy f68427b;
Handshake f68428c;
public Call call;
private OkHttpClient f;
private Headers.Builder g;
private Random h;
private Headers i;
private long j;
private Response k;
private OutputStreamRequestBody l;
private Map<String, List<String>> m;
private final OkHttpClient.Builder n;
public static final String SELECTED_PROTOCOL = Platform.get().getPrefix() + "-Selected-Protocol";
public static final String RESPONSE_SOURCE = Platform.get().getPrefix() + "-Response-Source";
private static final Set<String> f68425d = new LinkedHashSet(Arrays.asList(MttRequestBase.METHOD_NAME_OPTIONS, "GET", MttRequestBase.METHOD_NAME_HEAD, "POST", MttRequestBase.METHOD_NAME_PUT, MttRequestBase.METHOD_NAME_DELETE, MttRequestBase.METHOD_NAME_TRACE, "PATCH"));
private static X509TrustManager e = Util.platformTrustManager();
public final class NetworkInterceptor implements Interceptor {
NetworkInterceptor() {
}
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
if (QBOKHttpUrlConnection.this.l != null) {
request = QBOKHttpUrlConnection.this.l.prepareToSendRequest(request);
}
return chain.proceed(request);
}
}
public QBOKHttpUrlConnection(OKHttpConnectInfo oKHttpConnectInfo) {
super(oKHttpConnectInfo.getUrl());
this.g = new Headers.Builder();
this.h = new Random();
this.j = -1L;
this.f = oKHttpConnectInfo.getClient();
this.n = this.f.newBuilder();
}
private <T> T a(Class<T> cls, List list) {
if (list == null) {
return null;
}
Iterator it = list.iterator();
while (it.hasNext()) {
T t = (T) it.next();
if (cls.isInstance(t)) {
return t;
}
}
return null;
}
private static String a(Response response) {
StringBuilder sb;
String str;
if (response.networkResponse() == null) {
if (response.cacheResponse() == null) {
return INovelService.FROM_WHERE_NONE;
}
sb = new StringBuilder();
str = "CACHE ";
} else {
if (response.cacheResponse() != null) {
sb = new StringBuilder();
sb.append("CONDITIONAL_CACHE ");
response = response.networkResponse();
sb.append(response.code());
return sb.toString();
}
sb = new StringBuilder();
str = "NETWORK ";
}
sb.append(str);
sb.append(response.code());
return sb.toString();
}
private static EventListener.Factory a(OkHttpClient okHttpClient, final OkHttpEventListenerDispatcher okHttpEventListenerDispatcher) {
final EventListener.Factory eventListenerFactory = okHttpClient.eventListenerFactory();
return new EventListener.Factory() {
@Override
public EventListener create(Call call) {
EventListener.Factory factory = EventListener.Factory.this;
if (factory != null) {
okHttpEventListenerDispatcher.registerEventListener(factory.create(call));
}
return okHttpEventListenerDispatcher;
}
};
}
public void a() {
HashMap hashMap = new HashMap();
hashMap.put("url", getURL().toString());
hashMap.put("is_successful", "false");
hashMap.put("status_code", "-200");
hashMap.put("errMsg", "executeAsync in QBOKHttpUrlConnection");
hashMap.put("connectionType", "QBOKHttpUrlConnection");
StatServerHolder.statWithBeacon("MTT_NETWORK_QUALITY_STAT_BEACON", hashMap);
}
private void b() {
if (!this.k.isSuccessful() || HttpTimeRecord.isDebugMode || (getBindObject() != null && (getBindObject() instanceof MttRequestBase) && ((MttRequestBase) getBindObject()).getIsWupRequest())) {
StringBuilder sb = new StringBuilder();
sb.append("isSuccessful=");
sb.append(this.k.isSuccessful());
sb.append(" url=");
sb.append(getURL().toString());
sb.append(" uniqueId=");
sb.append(getUniqueIdFromBindObject());
sb.append(" code=");
sb.append(this.k.code());
sb.append(" requestHeader=");
sb.append(this.call.request().headers().toString().replaceAll("\n", " "));
sb.append(" responseHeader=");
sb.append(this.k.headers() != null ? this.k.headers().toString().replaceAll("\n", " ") : IAPInjectService.EP_NULL);
TbsNetLiveLogger.error("QBOKHttpUrlConnection", sb.toString());
}
}
private Headers c() throws IOException {
if (this.i == null) {
Response f = f();
this.i = f.headers().newBuilder().add(SELECTED_PROTOCOL, f.protocol().toString()).add(RESPONSE_SOURCE, a(f)).build();
}
return this.i;
}
private OkHttpClient.Builder d() {
KingCardRequestInterceptor kingCardRequestInterceptor = (KingCardRequestInterceptor) a(KingCardRequestInterceptor.class, this.f.interceptors());
KingCardNetworkInterceptor kingCardNetworkInterceptor = (KingCardNetworkInterceptor) a(KingCardNetworkInterceptor.class, this.f.networkInterceptors());
KingFlowInterceptor kingFlowInterceptor = (KingFlowInterceptor) a(KingFlowInterceptor.class, this.f.networkInterceptors());
this.n.interceptors().clear();
this.n.networkInterceptors().clear();
this.n.networkInterceptors().add(new NetworkInterceptor());
this.n.dispatcher(new Dispatcher(this.f.dispatcher().executorService()));
if (kingCardNetworkInterceptor != null) {
this.n.addNetworkInterceptor(kingCardNetworkInterceptor);
}
if (kingCardRequestInterceptor != null) {
this.n.addInterceptor(kingCardRequestInterceptor);
}
if (kingFlowInterceptor != null) {
this.n.addInterceptor(kingFlowInterceptor);
}
if (!getUseCaches()) {
this.n.cache(null);
}
if (this.f.proxy() != null && kingCardRequestInterceptor != null && kingCardRequestInterceptor.a() != null) {
kingCardRequestInterceptor.a().b(this.f.proxy());
this.n.proxy(null);
}
if (kingCardRequestInterceptor != null) {
OkHttpEventListenerDispatcher okHttpEventListenerDispatcher = new OkHttpEventListenerDispatcher();
okHttpEventListenerDispatcher.registerEventListener(new KingOkHttpEventListener(kingCardRequestInterceptor.a()));
this.n.eventListenerFactory(a(this.f, okHttpEventListenerDispatcher));
}
return this.n;
}
private String e() {
return "qbhttp/1.0.0";
}
private Response f() throws IOException {
return this.k;
}
@Override
public void addRequestProperty(String str, String str2) {
if (str == null) {
throw new NullPointerException("field == null");
}
if (str2 != null) {
this.g.add(str, str2);
return;
}
Platform.get().log(5, "Ignoring header " + str + " because its value was null.", null);
}
public Call buildCall() throws IOException {
Call call = this.call;
if (call != null) {
return call;
}
checkInBuildCall();
this.l = null;
if (HttpMethod.permitsRequestBody(this.method)) {
if (this.g.get("Content-Type") == null) {
this.g.add("Content-Type", "application/x-www-form-urlencoded");
}
long j = -1;
boolean z = this.j != -1 || this.chunkLength > 0;
String str = this.g.get("Content-Length");
long j2 = this.j;
if (j2 != -1) {
j = j2;
} else if (str != null) {
j = Long.parseLong(str);
}
this.l = z ? new StreamedRequestBody(j) : new BufferedRequestBody(j);
this.l.timeout().timeout(this.f.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
}
Request build = new Request.Builder().url(getURL().toString()).headers(this.g.build()).method(this.method, this.l).build();
URLFilter uRLFilter = this.f68426a;
if (uRLFilter != null) {
uRLFilter.checkURLPermitted(build.url().url());
}
OkHttpClient.Builder d2 = d();
if (!getUseCaches()) {
d2.cache(null);
}
this.f = d2.build();
Call newCall = this.f.newCall(build);
this.call = newCall;
return newCall;
}
@Override
public void cancel() {
Call call = this.call;
if (call == null) {
return;
}
call.cancel();
}
public void checkInBuildCall() throws IOException {
if (this.doOutput) {
if (this.method.equals("GET")) {
this.method = "POST";
} else if (!HttpMethod.permitsRequestBody(this.method)) {
throw new ProtocolException(this.method + " does not support writing");
}
}
if (this.g.get("User-Agent") == null) {
this.g.add("User-Agent", e());
}
}
@Override
public void execute() throws IOException {
MttRequestBase mttRequestBase;
if (getBindObject() == null || !(getBindObject() instanceof MttRequestBase)) {
mttRequestBase = null;
} else {
mttRequestBase = (MttRequestBase) getBindObject();
((MttRequestBase) getBindObject()).setIsTbsNetRequest(false);
}
if (mttRequestBase != null && mttRequestBase.getSpanProxy() != null) {
mttRequestBase.getSpanProxy().addEvent("OK3_START");
}
Call buildCall = buildCall();
final long currentTimeMillis = System.currentTimeMillis();
this.k = buildCall.execute();
final long currentTimeMillis2 = System.currentTimeMillis();
if (mttRequestBase != null && mttRequestBase.getSpanProxy() != null) {
mttRequestBase.getSpanProxy().addEvent("OK3_EXECUTE_END", "executeTime", "" + currentTimeMillis + Constants.ACCEPT_TIME_SEPARATOR_SERVER + currentTimeMillis2);
}
this.f68428c = this.k.handshake();
this.url = this.k.request().url().url();
try {
b();
if (this.h.nextInt(100) >= 5) {
return;
}
BrowserExecutorSupplier.postForIoTasks(new BrowserExecutorSupplier.BackgroundRunable() {
@Override
public void doRun() {
QBOKHttpUrlConnection qBOKHttpUrlConnection = QBOKHttpUrlConnection.this;
qBOKHttpUrlConnection.uploadNetworkQualityToBeacon(qBOKHttpUrlConnection.k, currentTimeMillis2, currentTimeMillis);
}
});
} catch (Exception unused) {
}
}
@Override
public void executeAsync(ValueCallback<Bundle> valueCallback) throws IOException {
BrowserExecutorSupplier.postForIoTasks(new BrowserExecutorSupplier.BackgroundRunable() {
@Override
public void doRun() {
QBOKHttpUrlConnection.this.a();
}
});
throw new IOException("error executeAsync in QBOKHttpUrlConnection");
}
@Override
public int getConnectTimeout() {
this.f = this.n.build();
return this.f.connectTimeoutMillis();
}
@Override
public InputStream getErrorStream() {
try {
Response f = f();
if (!HttpHeaders.hasBody(f) || f.code() < 400) {
return null;
}
return f.body().byteStream();
} catch (IOException unused) {
return null;
}
}
@Override
public String getHeaderField(int i) {
try {
Headers c2 = c();
if (i >= 0 && i < c2.size()) {
return c2.value(i);
}
} catch (IOException unused) {
}
return null;
}
@Override
public String getHeaderField(String str) {
try {
return str == null ? StatusLine.get(f()).toString() : c().get(str);
} catch (IOException unused) {
return null;
}
}
@Override
public String getHeaderFieldKey(int i) {
try {
Headers c2 = c();
if (i >= 0 && i < c2.size()) {
return c2.name(i);
}
} catch (IOException unused) {
}
return null;
}
@Override
public Map<String, List<String>> getHeaderFields() {
try {
if (this.m == null) {
this.m = JavaNetHeaders.toMultimap(c(), StatusLine.get(f()).toString());
}
return this.m;
} catch (IOException unused) {
return Collections.emptyMap();
}
}
@Override
public HostnameVerifier getHostnameVerifier() {
this.f = this.n.build();
return this.f.hostnameVerifier();
}
@Override
public InputStream getInputStream() throws IOException {
if (!this.doInput) {
throw new ProtocolException("This protocol does not support input");
}
Response f = f();
if (f.code() < 400) {
return f.body().byteStream();
}
throw new FileNotFoundException(this.url.toString());
}
@Override
public boolean getInstanceFollowRedirects() {
this.f = this.n.build();
return this.f.followRedirects();
}
@Override
public OutputStream getOutputStream() throws IOException {
OutputStreamRequestBody outputStreamRequestBody = (OutputStreamRequestBody) buildCall().request().body();
if (outputStreamRequestBody != null) {
if (outputStreamRequestBody.isClosed()) {
throw new ProtocolException("cannot write request body after response has been read");
}
return outputStreamRequestBody.outputStream();
}
throw new ProtocolException("method does not support a request body: " + this.method);
}
@Override
public Permission getPermission() throws IOException {
URL url = getURL();
String host = url.getHost();
int port = url.getPort() != -1 ? url.getPort() : HttpUrl.defaultPort(url.getProtocol());
if (usingProxy()) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) this.f.proxy().address();
host = inetSocketAddress.getHostName();
port = inetSocketAddress.getPort();
}
return new SocketPermission(host + Constants.COLON_SEPARATOR + port, "connect, resolve");
}
@Override
public int getReadTimeout() {
this.f = this.n.build();
return this.f.readTimeoutMillis();
}
@Override
public Map<String, List<String>> getRequestProperties() {
return JavaNetHeaders.toMultimap(this.g.build(), null);
}
@Override
public String getRequestProperty(String str) {
if (str == null) {
return null;
}
return this.g.get(str);
}
@Override
public int getResponseCode() throws IOException {
return f().code();
}
@Override
public String getResponseMessage() throws IOException {
return f().message();
}
@Override
public SSLSocketFactory getSSLSocketFactory() {
return this.f.sslSocketFactory();
}
public String getUniqueIdFromBindObject() {
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append((getBindObject() == null || !(getBindObject() instanceof MttRequestBase)) ? "-1" : Integer.valueOf(((MttRequestBase) getBindObject()).getUniqueId()));
return sb.toString();
}
@Override
public void setConnectTimeout(int i) {
this.n.connectTimeout(i, TimeUnit.MILLISECONDS);
}
@Override
public void setFixedLengthStreamingMode(int i) {
setFixedLengthStreamingMode(i);
}
@Override
public void setFixedLengthStreamingMode(long j) {
if (this.chunkLength > 0) {
throw new IllegalStateException("Already in chunked mode");
}
if (j < 0) {
throw new IllegalArgumentException("contentLength < 0");
}
this.j = j;
this.fixedContentLength = (int) Math.min(j, 2147483647L);
}
@Override
public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
this.n.hostnameVerifier(hostnameVerifier);
}
@Override
public void setIfModifiedSince(long j) {
super.setIfModifiedSince(j);
long j2 = this.ifModifiedSince;
Headers.Builder builder = this.g;
if (j2 != 0) {
builder.set("If-Modified-Since", HttpDate.format(new Date(this.ifModifiedSince)));
} else {
builder.removeAll("If-Modified-Since");
}
}
@Override
public void setInstanceFollowRedirects(boolean z) {
this.n.followRedirects(z);
}
@Override
public void setReadTimeout(int i) {
this.n.readTimeout(i, TimeUnit.MILLISECONDS);
}
@Override
public void setRequestMethod(String str) throws ProtocolException {
if (f68425d.contains(str)) {
this.method = str;
return;
}
throw new ProtocolException("Expected one of " + f68425d + " but was " + str);
}
@Override
public void setRequestProperty(String str, String str2) {
if (str == null) {
throw new NullPointerException("field == null");
}
if (str2 != null) {
this.g.set(str, str2);
return;
}
Platform.get().log(5, "Ignoring header " + str + " because its value was null.", null);
}
@Override
public void setSSLSocketFactory(SSLSocketFactory sSLSocketFactory) {
this.n.sslSocketFactory(sSLSocketFactory, e);
}
public void uploadNetworkQualityToBeacon(Response response, long j, long j2) {
try {
if (response.request().url().toString().startsWith("https://otheve.beacon.qq.com")) {
return;
}
HashMap hashMap = new HashMap();
hashMap.put("url", response.request().url().toString());
hashMap.put("is_successful", "" + response.isSuccessful());
hashMap.put("status_code", "" + response.code());
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(response.body() != null ? Long.valueOf(response.body().contentLength()) : "");
hashMap.put("content_length", sb.toString());
hashMap.put("receive_milis", "" + response.receivedResponseAtMillis());
hashMap.put("send_millis", "" + response.sentRequestAtMillis());
hashMap.put("duration", "" + (j - j2));
hashMap.put("connectionType", "QBOKHttpUrlConnection");
StatServerHolder.statWithBeacon("MTT_NETWORK_QUALITY_STAT_BEACON", hashMap);
} catch (Exception unused) {
}
}
@Override
public boolean usingProxy() {
if (this.f68427b != null) {
return true;
}
Proxy proxy = this.f.proxy();
return (proxy == null || proxy.type() == Proxy.Type.DIRECT) ? false : true;
}
}