package okhttp3.repackaged.internal.huc; import com.google.common.net.HttpHeaders; import io.grpc.internal.GrpcUtil; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpRetryException; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.Proxy; import java.net.SocketPermission; import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; import java.security.Permission; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import o.giO; import o.giX; import okhttp3.repackaged.Connection; import okhttp3.repackaged.Handshake; import okhttp3.repackaged.Headers; import okhttp3.repackaged.HttpUrl; import okhttp3.repackaged.MediaType; import okhttp3.repackaged.OkHttpClient; import okhttp3.repackaged.Protocol; import okhttp3.repackaged.Request; import okhttp3.repackaged.RequestBody; import okhttp3.repackaged.Response; import okhttp3.repackaged.Route; import okhttp3.repackaged.internal.Internal; import okhttp3.repackaged.internal.JavaNetHeaders; import okhttp3.repackaged.internal.Platform; import okhttp3.repackaged.internal.URLFilter; import okhttp3.repackaged.internal.Util; import okhttp3.repackaged.internal.Version; import okhttp3.repackaged.internal.http.HttpDate; import okhttp3.repackaged.internal.http.HttpEngine; import okhttp3.repackaged.internal.http.HttpMethod; import okhttp3.repackaged.internal.http.OkHeaders; import okhttp3.repackaged.internal.http.RequestException; import okhttp3.repackaged.internal.http.RetryableSink; import okhttp3.repackaged.internal.http.RouteException; import okhttp3.repackaged.internal.http.StatusLine; import okhttp3.repackaged.internal.http.StreamAllocation; /* loaded from: classes6.dex */ public class HttpURLConnectionImpl extends HttpURLConnection { private static final Set amJ = new LinkedHashSet(Arrays.asList("OPTIONS", "GET", "HEAD", GrpcUtil.HTTP_METHOD, "PUT", "DELETE", "TRACE", "PATCH")); private static final RequestBody amK = RequestBody.create((MediaType) null, new byte[0]); private Headers agY; Handshake agZ; private URLFilter aip; private Route amD; private Headers.Builder amL; private long amM; private int amN; OkHttpClient client; protected HttpEngine httpEngine; protected IOException httpEngineFailure; public HttpURLConnectionImpl(URL url, OkHttpClient okHttpClient) { super(url); this.amL = new Headers.Builder(); this.amM = -1L; this.client = okHttpClient; } public HttpURLConnectionImpl(URL url, OkHttpClient okHttpClient, URLFilter uRLFilter) { this(url, okHttpClient); this.aip = uRLFilter; } @Override // java.net.URLConnection public final void connect() throws IOException { vk(); do { } while (!C(false)); } @Override // java.net.HttpURLConnection public final void disconnect() { HttpEngine httpEngine = this.httpEngine; if (httpEngine == null) { return; } httpEngine.cancel(); } @Override // java.net.HttpURLConnection public final InputStream getErrorStream() { try { HttpEngine vm = vm(); if (!HttpEngine.hasBody(vm.getResponse()) || vm.getResponse().code() < 400) { return null; } return vm.getResponse().body().byteStream(); } catch (IOException unused) { return null; } } private Headers vj() throws IOException { if (this.agY == null) { Response response = vm().getResponse(); this.agY = response.headers().newBuilder().add(OkHeaders.SELECTED_PROTOCOL, response.protocol().toString()).add(OkHeaders.RESPONSE_SOURCE, p(response)).build(); } return this.agY; } private static String p(Response response) { if (response.networkResponse() == null) { if (response.cacheResponse() == null) { return "NONE"; } StringBuilder sb = new StringBuilder("CACHE "); sb.append(response.code()); return sb.toString(); } if (response.cacheResponse() == null) { StringBuilder sb2 = new StringBuilder("NETWORK "); sb2.append(response.code()); return sb2.toString(); } StringBuilder sb3 = new StringBuilder("CONDITIONAL_CACHE "); sb3.append(response.networkResponse().code()); return sb3.toString(); } @Override // java.net.HttpURLConnection, java.net.URLConnection public final String getHeaderField(int i) { try { Headers vj = vj(); if (i >= 0 && i < vj.size()) { return vj.value(i); } return null; } catch (IOException unused) { return null; } } @Override // java.net.URLConnection public final String getHeaderField(String str) { String str2; try { if (str == null) { str2 = StatusLine.get(vm().getResponse()).toString(); } else { str2 = vj().get(str); } return str2; } catch (IOException unused) { return null; } } @Override // java.net.HttpURLConnection, java.net.URLConnection public final String getHeaderFieldKey(int i) { try { Headers vj = vj(); if (i >= 0 && i < vj.size()) { return vj.name(i); } return null; } catch (IOException unused) { return null; } } @Override // java.net.URLConnection public final Map> getHeaderFields() { try { return JavaNetHeaders.toMultimap(vj(), StatusLine.get(vm().getResponse()).toString()); } catch (IOException unused) { return Collections.emptyMap(); } } @Override // java.net.URLConnection public final Map> getRequestProperties() { if (((URLConnection) this).connected) { throw new IllegalStateException("Cannot access request header fields after connection is set"); } return JavaNetHeaders.toMultimap(this.amL.build(), null); } @Override // java.net.URLConnection public final InputStream getInputStream() throws IOException { if (!((URLConnection) this).doInput) { throw new ProtocolException("This protocol does not support input"); } HttpEngine vm = vm(); if (getResponseCode() >= 400) { throw new FileNotFoundException(((URLConnection) this).url.toString()); } return vm.getResponse().body().byteStream(); } @Override // java.net.URLConnection public final OutputStream getOutputStream() throws IOException { connect(); giO bufferedRequestBody = this.httpEngine.getBufferedRequestBody(); if (bufferedRequestBody == null) { StringBuilder sb = new StringBuilder("method does not support a request body: "); sb.append(((HttpURLConnection) this).method); throw new ProtocolException(sb.toString()); } if (this.httpEngine.hasResponse()) { throw new ProtocolException("cannot write request body after response has been read"); } return bufferedRequestBody.h(); } @Override // java.net.HttpURLConnection, java.net.URLConnection public final Permission getPermission() throws IOException { int defaultPort; URL url = getURL(); String host = url.getHost(); if (url.getPort() != -1) { defaultPort = url.getPort(); } else { defaultPort = HttpUrl.defaultPort(url.getProtocol()); } if (usingProxy()) { InetSocketAddress inetSocketAddress = (InetSocketAddress) this.client.proxy().address(); host = inetSocketAddress.getHostName(); defaultPort = inetSocketAddress.getPort(); } StringBuilder sb = new StringBuilder(); sb.append(host); sb.append(":"); sb.append(defaultPort); return new SocketPermission(sb.toString(), "connect, resolve"); } @Override // java.net.URLConnection public final String getRequestProperty(String str) { if (str == null) { return null; } return this.amL.get(str); } @Override // java.net.URLConnection public void setConnectTimeout(int i) { this.client = this.client.newBuilder().connectTimeout(i, TimeUnit.MILLISECONDS).build(); } @Override // java.net.HttpURLConnection public void setInstanceFollowRedirects(boolean z) { this.client = this.client.newBuilder().followRedirects(z).build(); } @Override // java.net.HttpURLConnection public boolean getInstanceFollowRedirects() { return this.client.followRedirects(); } @Override // java.net.URLConnection public int getConnectTimeout() { return this.client.connectTimeoutMillis(); } @Override // java.net.URLConnection public void setReadTimeout(int i) { this.client = this.client.newBuilder().readTimeout(i, TimeUnit.MILLISECONDS).build(); } @Override // java.net.URLConnection public int getReadTimeout() { return this.client.readTimeoutMillis(); } private void vk() throws IOException { IOException iOException = this.httpEngineFailure; if (iOException != null) { throw iOException; } if (this.httpEngine != null) { return; } ((URLConnection) this).connected = true; try { if (((URLConnection) this).doOutput) { if (((HttpURLConnection) this).method.equals("GET")) { ((HttpURLConnection) this).method = GrpcUtil.HTTP_METHOD; } else if (!HttpMethod.permitsRequestBody(((HttpURLConnection) this).method)) { StringBuilder sb = new StringBuilder(); sb.append(((HttpURLConnection) this).method); sb.append(" does not support writing"); throw new ProtocolException(sb.toString()); } } this.httpEngine = a(((HttpURLConnection) this).method, null, null, null); } catch (IOException e) { this.httpEngineFailure = e; throw e; } } private HttpEngine a(String str, StreamAllocation streamAllocation, RetryableSink retryableSink, Response response) throws MalformedURLException, UnknownHostException { Request.Builder method = new Request.Builder().url(Internal.instance.getHttpUrlChecked(getURL().toString())).method(str, HttpMethod.requiresRequestBody(str) ? amK : null); Headers build = this.amL.build(); int size = build.size(); boolean z = false; for (int i = 0; i < size; i++) { method.addHeader(build.name(i), build.value(i)); } if (HttpMethod.permitsRequestBody(str)) { long j = this.amM; if (j != -1) { method.header(HttpHeaders.CONTENT_LENGTH, Long.toString(j)); } else if (((HttpURLConnection) this).chunkLength > 0) { method.header(HttpHeaders.TRANSFER_ENCODING, "chunked"); } else { z = true; } if (build.get(HttpHeaders.CONTENT_TYPE) == null) { method.header(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); } } boolean z2 = z; if (build.get("User-Agent") == null) { method.header("User-Agent", vl()); } Request build2 = method.build(); OkHttpClient okHttpClient = this.client; if (Internal.instance.internalCache(okHttpClient) != null && !getUseCaches()) { okHttpClient = this.client.newBuilder().cache(null).build(); } return new HttpEngine(okHttpClient, build2, z2, true, false, streamAllocation, retryableSink, response); } private String vl() { String property = System.getProperty("http.agent"); return property != null ? Util.toHumanReadableAscii(property) : Version.userAgent(); } private HttpEngine vm() throws IOException { vk(); if (this.httpEngine.hasResponse()) { return this.httpEngine; } while (true) { if (C(true)) { Response response = this.httpEngine.getResponse(); Request followUpRequest = this.httpEngine.followUpRequest(); if (followUpRequest == null) { this.httpEngine.releaseStreamAllocation(); return this.httpEngine; } int i = this.amN + 1; this.amN = i; if (i > 20) { StringBuilder sb = new StringBuilder("Too many follow-up requests: "); sb.append(this.amN); throw new ProtocolException(sb.toString()); } ((URLConnection) this).url = followUpRequest.url().url(); this.amL = followUpRequest.headers().newBuilder(); giX requestBody = this.httpEngine.getRequestBody(); StreamAllocation streamAllocation = null; if (!followUpRequest.method().equals(((HttpURLConnection) this).method)) { requestBody = null; } if (requestBody != null && !(requestBody instanceof RetryableSink)) { throw new HttpRetryException("Cannot retry streamed HTTP body", ((HttpURLConnection) this).responseCode); } StreamAllocation close = this.httpEngine.close(); if (this.httpEngine.sameConnection(followUpRequest.url())) { streamAllocation = close; } else { close.release(); } this.httpEngine = a(followUpRequest.method(), streamAllocation, (RetryableSink) requestBody, response); } } } private boolean C(boolean z) throws IOException { Handshake handshake; URLFilter uRLFilter = this.aip; if (uRLFilter != null) { uRLFilter.checkURLPermitted(this.httpEngine.getRequest().url().url()); } boolean z2 = true; try { try { try { try { this.httpEngine.sendRequest(); Connection connection = this.httpEngine.getConnection(); if (connection != null) { this.amD = connection.route(); handshake = connection.handshake(); } else { handshake = null; this.amD = null; } this.agZ = handshake; if (z) { this.httpEngine.readResponse(); } return true; } catch (RouteException e) { HttpEngine recover = this.httpEngine.recover(e.getLastConnectException()); if (recover != null) { this.httpEngine = recover; return false; } IOException lastConnectException = e.getLastConnectException(); this.httpEngineFailure = lastConnectException; throw lastConnectException; } } catch (IOException e2) { HttpEngine recover2 = this.httpEngine.recover(e2); if (recover2 != null) { this.httpEngine = recover2; return false; } this.httpEngineFailure = e2; throw e2; } catch (RequestException e3) { IOException cause = e3.getCause(); this.httpEngineFailure = cause; throw cause; } } catch (Throwable th) { th = th; z2 = false; if (z2) { this.httpEngine.close().release(); } throw th; } } catch (Throwable th2) { th = th2; } } @Override // java.net.HttpURLConnection public final boolean usingProxy() { Proxy proxy; Route route = this.amD; if (route != null) { proxy = route.proxy(); } else { proxy = this.client.proxy(); } return (proxy == null || proxy.type() == Proxy.Type.DIRECT) ? false : true; } @Override // java.net.HttpURLConnection public String getResponseMessage() throws IOException { return vm().getResponse().message(); } @Override // java.net.HttpURLConnection public final int getResponseCode() throws IOException { return vm().getResponse().code(); } @Override // java.net.URLConnection public final void setRequestProperty(String str, String str2) { if (((URLConnection) this).connected) { throw new IllegalStateException("Cannot set request property after connection is made"); } if (str == null) { throw new NullPointerException("field == null"); } if (str2 == null) { Platform platform = Platform.get(); StringBuilder sb = new StringBuilder("Ignoring header "); sb.append(str); sb.append(" because its value was null."); platform.logW(sb.toString()); return; } if ("X-Android-Transports".equals(str) || "X-Android-Protocols".equals(str)) { f(str2, false); } else { this.amL.set(str, str2); } } @Override // java.net.URLConnection public void setIfModifiedSince(long j) { super.setIfModifiedSince(j); if (((URLConnection) this).ifModifiedSince != 0) { this.amL.set(HttpHeaders.IF_MODIFIED_SINCE, HttpDate.format(new Date(((URLConnection) this).ifModifiedSince))); } else { this.amL.removeAll(HttpHeaders.IF_MODIFIED_SINCE); } } @Override // java.net.URLConnection public final void addRequestProperty(String str, String str2) { if (((URLConnection) this).connected) { throw new IllegalStateException("Cannot add request property after connection is made"); } if (str == null) { throw new NullPointerException("field == null"); } if (str2 == null) { Platform platform = Platform.get(); StringBuilder sb = new StringBuilder("Ignoring header "); sb.append(str); sb.append(" because its value was null."); platform.logW(sb.toString()); return; } if ("X-Android-Transports".equals(str) || "X-Android-Protocols".equals(str)) { f(str2, true); } else { this.amL.add(str, str2); } } private void f(String str, boolean z) { ArrayList arrayList = new ArrayList(); if (z) { arrayList.addAll(this.client.protocols()); } for (String str2 : str.split(",", -1)) { try { arrayList.add(Protocol.get(str2)); } catch (IOException e) { throw new IllegalStateException(e); } } this.client = this.client.newBuilder().protocols(arrayList).build(); } @Override // java.net.HttpURLConnection public void setRequestMethod(String str) throws ProtocolException { Set set = amJ; if (!set.contains(str)) { StringBuilder sb = new StringBuilder("Expected one of "); sb.append(set); sb.append(" but was "); sb.append(str); throw new ProtocolException(sb.toString()); } ((HttpURLConnection) this).method = str; } @Override // java.net.HttpURLConnection public void setFixedLengthStreamingMode(int i) { setFixedLengthStreamingMode(i); } @Override // java.net.HttpURLConnection public void setFixedLengthStreamingMode(long j) { if (((URLConnection) this).connected) { throw new IllegalStateException("Already connected"); } if (((HttpURLConnection) this).chunkLength > 0) { throw new IllegalStateException("Already in chunked mode"); } if (j < 0) { throw new IllegalArgumentException("contentLength < 0"); } this.amM = j; ((HttpURLConnection) this).fixedContentLength = (int) Math.min(j, 2147483647L); } }