package com.amazonaws.http; import com.amazonaws.ClientConfiguration; import com.amazonaws.http.HttpResponse; import com.amazonaws.logging.Log; import com.amazonaws.logging.LogFactory; import com.facebook.internal.security.CertificateUtil; import com.google.firebase.perf.network.FirebasePerfUrlConnection; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.nio.BufferOverflowException; import java.nio.ByteBuffer; import java.security.GeneralSecurityException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import org.apache.http.client.methods.HttpHead; import org.apache.http.protocol.HTTP; /* loaded from: classes.dex */ public class UrlHttpClient implements HttpClient { public static final Log log = LogFactory.getLog(UrlHttpClient.class); public final ClientConfiguration config; public SSLContext customTrustSSLContext = null; public TLS12SocketFactory customTrustTls12SocketFactory; public final TLS12SocketFactory tls12SocketFactory; @Override // com.amazonaws.http.HttpClient public void shutdown() { } public UrlHttpClient(ClientConfiguration clientConfiguration) { this.config = clientConfiguration; TLS12SocketFactory.createTLS12SocketFactory(); } @Override // com.amazonaws.http.HttpClient public HttpResponse execute(HttpRequest httpRequest) { HttpURLConnection httpURLConnection = (HttpURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(httpRequest.getUri().toURL().openConnection())); CurlBuilder curlBuilder = this.config.isCurlLogging() ? new CurlBuilder(httpRequest.getUri().toURL()) : null; configureConnection(httpRequest, httpURLConnection); applyHeadersAndMethod(httpRequest, httpURLConnection, curlBuilder); writeContentToConnection(httpRequest, httpURLConnection, curlBuilder); if (curlBuilder != null) { if (curlBuilder.isValid()) { printToLog(curlBuilder.build()); } else { printToLog("Failed to create curl, content too long"); } } return createHttpResponse(httpRequest, httpURLConnection); } public HttpResponse createHttpResponse(HttpRequest httpRequest, HttpURLConnection httpURLConnection) { String responseMessage = httpURLConnection.getResponseMessage(); int responseCode = httpURLConnection.getResponseCode(); InputStream errorStream = httpURLConnection.getErrorStream(); if (errorStream == null && !HttpHead.METHOD_NAME.equals(httpRequest.getMethod())) { try { errorStream = httpURLConnection.getInputStream(); } catch (IOException unused) { } } HttpResponse.Builder content = HttpResponse.builder().statusCode(responseCode).statusText(responseMessage).content(errorStream); for (Map.Entry> entry : httpURLConnection.getHeaderFields().entrySet()) { if (entry.getKey() != null) { content.header(entry.getKey(), entry.getValue().get(0)); } } return content.build(); } public void writeContentToConnection(HttpRequest httpRequest, HttpURLConnection httpURLConnection, CurlBuilder curlBuilder) { ByteBuffer byteBuffer; if (httpRequest.getContent() == null || httpRequest.getContentLength() < 0) { return; } httpURLConnection.setDoOutput(true); if (!httpRequest.isStreaming()) { httpURLConnection.setFixedLengthStreamingMode((int) httpRequest.getContentLength()); } OutputStream outputStream = httpURLConnection.getOutputStream(); if (curlBuilder != null) { if (httpRequest.getContentLength() < 2147483647L) { byteBuffer = ByteBuffer.allocate((int) httpRequest.getContentLength()); write(httpRequest.getContent(), outputStream, curlBuilder, byteBuffer); if (curlBuilder != null && byteBuffer != null && byteBuffer.position() != 0) { curlBuilder.setContent(new String(byteBuffer.array(), "UTF-8")); } outputStream.flush(); outputStream.close(); } curlBuilder.setContentOverflow(true); } byteBuffer = null; write(httpRequest.getContent(), outputStream, curlBuilder, byteBuffer); if (curlBuilder != null) { curlBuilder.setContent(new String(byteBuffer.array(), "UTF-8")); } outputStream.flush(); outputStream.close(); } public HttpURLConnection applyHeadersAndMethod(HttpRequest httpRequest, HttpURLConnection httpURLConnection, CurlBuilder curlBuilder) { if (httpRequest.getHeaders() != null && !httpRequest.getHeaders().isEmpty()) { if (curlBuilder != null) { curlBuilder.setHeaders(httpRequest.getHeaders()); } for (Map.Entry entry : httpRequest.getHeaders().entrySet()) { String str = (String) entry.getKey(); if (!str.equals(HTTP.CONTENT_LEN) && !str.equals(HTTP.TARGET_HOST)) { httpURLConnection.setRequestProperty(str, (String) entry.getValue()); } } } String method = httpRequest.getMethod(); httpURLConnection.setRequestMethod(method); if (curlBuilder != null) { curlBuilder.setMethod(method); } return httpURLConnection; } public void printToLog(String str) { log.debug(str); } public final void write(InputStream inputStream, OutputStream outputStream, CurlBuilder curlBuilder, ByteBuffer byteBuffer) { byte[] bArr = new byte[8192]; while (true) { int read = inputStream.read(bArr); if (read == -1) { return; } if (byteBuffer != null) { try { byteBuffer.put(bArr, 0, read); } catch (BufferOverflowException unused) { curlBuilder.setContentOverflow(true); } } outputStream.write(bArr, 0, read); } } public void configureConnection(HttpRequest httpRequest, HttpURLConnection httpURLConnection) { httpURLConnection.setConnectTimeout(this.config.getConnectionTimeout()); httpURLConnection.setReadTimeout(this.config.getSocketTimeout()); httpURLConnection.setInstanceFollowRedirects(false); httpURLConnection.setUseCaches(false); if (httpRequest.isStreaming()) { httpURLConnection.setChunkedStreamingMode(0); } if (httpURLConnection instanceof HttpsURLConnection) { HttpsURLConnection httpsURLConnection = (HttpsURLConnection) httpURLConnection; if (this.config.getTrustManager() != null) { enableCustomTrustManager(httpsURLConnection); } } } public final void enableCustomTrustManager(HttpsURLConnection httpsURLConnection) { if (this.customTrustSSLContext == null) { TrustManager[] trustManagerArr = {this.config.getTrustManager()}; try { SSLContext sSLContext = SSLContext.getInstance("TLS"); this.customTrustSSLContext = sSLContext; sSLContext.init(null, trustManagerArr, null); TLS12SocketFactory.createTLS12SocketFactory(this.customTrustSSLContext); } catch (GeneralSecurityException e) { throw new RuntimeException(e); } } httpsURLConnection.setSSLSocketFactory(this.customTrustSSLContext.getSocketFactory()); } public final class CurlBuilder { public final URL url; public String method = null; public final HashMap headers = new HashMap(); public String content = null; public boolean contentOverflow = false; public boolean isValid() { return !this.contentOverflow; } public CurlBuilder setContent(String str) { this.content = str; return this; } public CurlBuilder setContentOverflow(boolean z) { this.contentOverflow = z; return this; } public CurlBuilder setMethod(String str) { this.method = str; return this; } public CurlBuilder(URL url) { if (url == null) { throw new IllegalArgumentException("Must have a valid url"); } this.url = url; } public CurlBuilder setHeaders(Map map) { this.headers.clear(); this.headers.putAll(map); return this; } public String build() { if (!isValid()) { throw new IllegalStateException("Invalid state, cannot create curl command"); } StringBuilder sb = new StringBuilder("curl"); if (this.method != null) { sb.append(" -X "); sb.append(this.method); } for (Map.Entry entry : this.headers.entrySet()) { sb.append(" -H \""); sb.append((String) entry.getKey()); sb.append(CertificateUtil.DELIMITER); sb.append((String) entry.getValue()); sb.append("\""); } if (this.content != null) { sb.append(" -d '"); sb.append(this.content); sb.append("'"); } sb.append(" "); sb.append(this.url.toString()); return sb.toString(); } } }