mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-11-15 02:17:18 +01:00
268 lines
11 KiB
Java
268 lines
11 KiB
Java
package com.amazonaws.http;
|
|
|
|
import androidx.collection.SieveCacheKt;
|
|
import com.amazon.rtcsc.service.deviceconfiguration.util.DeviceConfigConstants;
|
|
import com.amazonaws.ClientConfiguration;
|
|
import com.amazonaws.http.HttpResponse;
|
|
import com.amazonaws.logging.Log;
|
|
import com.amazonaws.logging.LogFactory;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.ProtocolException;
|
|
import java.net.URL;
|
|
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 okio.Buffer$$ExternalSyntheticBUOutline0;
|
|
import org.apache.http.client.methods.HttpHead;
|
|
import rx.Single$$ExternalSyntheticBUOutline0;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
/* loaded from: classes5.dex */
|
|
public class UrlHttpClient implements HttpClient {
|
|
private static final int BUFFER_SIZE_MULTIPLIER = 8;
|
|
private static final int DEFAULT_BUFFER_SIZE = 1024;
|
|
private static final String TAG = "amazonaws";
|
|
private static final Log log = LogFactory.getLog((Class<?>) UrlHttpClient.class);
|
|
private final ClientConfiguration config;
|
|
private SSLContext sc = null;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
public final class CurlBuilder {
|
|
private final URL url;
|
|
private String method = null;
|
|
private final HashMap<String, String> headers = new HashMap<>();
|
|
private String content = null;
|
|
private boolean contentOverflow = false;
|
|
|
|
public CurlBuilder(URL url) {
|
|
if (url != null) {
|
|
this.url = url;
|
|
} else {
|
|
Single$$ExternalSyntheticBUOutline0.m$3("Must have a valid url");
|
|
throw null;
|
|
}
|
|
}
|
|
|
|
public String build() {
|
|
if (!isValid()) {
|
|
Single$$ExternalSyntheticBUOutline0.m$2("Invalid state, cannot create curl command");
|
|
return null;
|
|
}
|
|
StringBuilder sb = new StringBuilder("curl");
|
|
if (this.method != null) {
|
|
sb.append(" -X ");
|
|
sb.append(this.method);
|
|
}
|
|
for (Map.Entry<String, String> entry : this.headers.entrySet()) {
|
|
sb.append(" -H \"");
|
|
sb.append(entry.getKey());
|
|
sb.append(":");
|
|
sb.append(entry.getValue());
|
|
sb.append(DeviceConfigConstants.DOUBLE_QUOTE);
|
|
}
|
|
if (this.content != null) {
|
|
sb.append(" -d '");
|
|
sb.append(this.content);
|
|
sb.append("'");
|
|
}
|
|
sb.append(" ");
|
|
sb.append(this.url.toString());
|
|
return sb.toString();
|
|
}
|
|
|
|
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 setHeaders(Map<String, String> map) {
|
|
this.headers.clear();
|
|
this.headers.putAll(map);
|
|
return this;
|
|
}
|
|
|
|
public CurlBuilder setMethod(String str) {
|
|
this.method = str;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
public UrlHttpClient(ClientConfiguration clientConfiguration) {
|
|
this.config = clientConfiguration;
|
|
}
|
|
|
|
private void enableCustomTrustManager(HttpsURLConnection httpsURLConnection) {
|
|
if (this.sc == null) {
|
|
TrustManager[] trustManagerArr = {this.config.getTrustManager()};
|
|
try {
|
|
SSLContext sSLContext = SSLContext.getInstance("TLS");
|
|
this.sc = sSLContext;
|
|
sSLContext.init(null, trustManagerArr, null);
|
|
} catch (GeneralSecurityException e) {
|
|
Buffer$$ExternalSyntheticBUOutline0.m((Throwable) e);
|
|
return;
|
|
}
|
|
}
|
|
httpsURLConnection.setSSLSocketFactory(this.sc.getSocketFactory());
|
|
}
|
|
|
|
private void write(InputStream inputStream, OutputStream outputStream, CurlBuilder curlBuilder, ByteBuffer byteBuffer) throws IOException {
|
|
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 HttpURLConnection applyHeadersAndMethod(HttpRequest httpRequest, HttpURLConnection httpURLConnection, CurlBuilder curlBuilder) throws ProtocolException {
|
|
if (httpRequest.getHeaders() != null && !httpRequest.getHeaders().isEmpty()) {
|
|
if (curlBuilder != null) {
|
|
curlBuilder.setHeaders(httpRequest.getHeaders());
|
|
}
|
|
for (Map.Entry<String, String> entry : httpRequest.getHeaders().entrySet()) {
|
|
String key = entry.getKey();
|
|
if (!key.equals("Content-Length") && !key.equals("Host")) {
|
|
key.equals("Expect");
|
|
httpURLConnection.setRequestProperty(key, entry.getValue());
|
|
}
|
|
}
|
|
}
|
|
String method = httpRequest.getMethod();
|
|
httpURLConnection.setRequestMethod(method);
|
|
if (curlBuilder != null) {
|
|
curlBuilder.setMethod(method);
|
|
}
|
|
return httpURLConnection;
|
|
}
|
|
|
|
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 HttpResponse createHttpResponse(HttpRequest httpRequest, HttpURLConnection httpURLConnection) throws IOException {
|
|
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<String, List<String>> entry : httpURLConnection.getHeaderFields().entrySet()) {
|
|
if (entry.getKey() != null) {
|
|
content.header(entry.getKey(), entry.getValue().get(0));
|
|
}
|
|
}
|
|
return content.build();
|
|
}
|
|
|
|
@Override // com.amazonaws.http.HttpClient
|
|
public HttpResponse execute(HttpRequest httpRequest) throws IOException {
|
|
HttpURLConnection httpURLConnection = (HttpURLConnection) 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 HttpURLConnection getUrlConnection(URL url) throws IOException {
|
|
return (HttpURLConnection) url.openConnection();
|
|
}
|
|
|
|
public void printToLog(String str) {
|
|
log.debug(str);
|
|
}
|
|
|
|
public void writeContentToConnection(HttpRequest httpRequest, HttpURLConnection httpURLConnection, CurlBuilder curlBuilder) throws IOException {
|
|
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() < SieveCacheKt.NodeLinkMask) {
|
|
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();
|
|
}
|
|
|
|
@Override // com.amazonaws.http.HttpClient
|
|
public void shutdown() {
|
|
}
|
|
|
|
public HttpURLConnection applyHeadersAndMethod(HttpRequest httpRequest, HttpURLConnection httpURLConnection) throws ProtocolException {
|
|
return applyHeadersAndMethod(httpRequest, httpURLConnection, null);
|
|
}
|
|
|
|
public void writeContentToConnection(HttpRequest httpRequest, HttpURLConnection httpURLConnection) throws IOException {
|
|
writeContentToConnection(httpRequest, httpURLConnection, null);
|
|
}
|
|
}
|