正在查看: Top浏览器 v4.6.16 应用的 HttpConnection.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: Top浏览器 v4.6.16 应用的 HttpConnection.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package org.jsoup.helper;
import androidx.exifinterface.media.ExifInterface;
import com.efs.sdk.base.Constants;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpURLConnection;
import java.net.IDN;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import org.jsoup.Connection;
import org.jsoup.UncheckedIOException;
import org.jsoup.internal.ConstrainableInputStream;
import org.jsoup.internal.Normalizer;
import org.jsoup.internal.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.jsoup.parser.TokenQueue;
public class HttpConnection implements Connection {
public static final String CONTENT_ENCODING = "Content-Encoding";
public static final String CONTENT_TYPE = "Content-Type";
public static final String DEFAULT_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36";
private static final String DefaultUploadType = "application/octet-stream";
public static final String FORM_URL_ENCODED = "application/x-www-form-urlencoded";
private static final int HTTP_TEMP_REDIR = 307;
public static final String MULTIPART_FORM_DATA = "multipart/form-data";
private static final String USER_AGENT = "User-Agent";
private Request req;
@Nullable
private Connection.Response res;
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
public static abstract class Base<T extends Connection.Base<T>> implements Connection.Base<T> {
private static final URL UnsetUrl;
public Map<String, String> cookies;
public Map<String, List<String>> headers;
public Connection.Method method;
public URL url;
static {
try {
UnsetUrl = new URL("http://undefined/");
} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}
}
private static String fixHeaderEncoding(String str) {
byte[] bytes = str.getBytes(HttpConnection.ISO_8859_1);
return !looksLikeUtf8(bytes) ? str : new String(bytes, HttpConnection.UTF_8);
}
private List<String> getHeadersCaseInsensitive(String str) {
Validate.notNull(str);
for (Map.Entry<String, List<String>> entry : this.headers.entrySet()) {
if (str.equalsIgnoreCase(entry.getKey())) {
return entry.getValue();
}
}
return Collections.emptyList();
}
private static boolean looksLikeUtf8(byte[] bArr) {
int i;
int i2 = (bArr.length >= 3 && (bArr[0] & ExifInterface.MARKER) == 239 && (bArr[1] & ExifInterface.MARKER) == 187 && (bArr[2] & ExifInterface.MARKER) == 191) ? 3 : 0;
int length = bArr.length;
while (i2 < length) {
byte b2 = bArr[i2];
if ((b2 & 128) != 0) {
if ((b2 & 224) == 192) {
i = i2 + 1;
} else if ((b2 & 240) == 224) {
i = i2 + 2;
} else {
if ((b2 & 248) != 240) {
return false;
}
i = i2 + 3;
}
if (i >= bArr.length) {
return false;
}
while (i2 < i) {
i2++;
if ((bArr[i2] & 192) != 128) {
return false;
}
}
}
i2++;
}
return true;
}
@Nullable
private Map.Entry<String, List<String>> scanHeaders(String str) {
String lowerCase = Normalizer.lowerCase(str);
for (Map.Entry<String, List<String>> entry : this.headers.entrySet()) {
if (Normalizer.lowerCase(entry.getKey()).equals(lowerCase)) {
return entry;
}
}
return null;
}
@Override
public T addHeader(String str, String str2) {
Validate.notEmpty(str);
if (str2 == null) {
str2 = "";
}
List<String> headers = headers(str);
if (headers.isEmpty()) {
headers = new ArrayList<>();
this.headers.put(str, headers);
}
headers.add(fixHeaderEncoding(str2));
return this;
}
@Override
public String cookie(String str) {
Validate.notEmpty(str, "Cookie name must not be empty");
return this.cookies.get(str);
}
@Override
public Map<String, String> cookies() {
return this.cookies;
}
@Override
public boolean hasCookie(String str) {
Validate.notEmpty(str, "Cookie name must not be empty");
return this.cookies.containsKey(str);
}
@Override
public boolean hasHeader(String str) {
Validate.notEmpty(str, "Header name must not be empty");
return !getHeadersCaseInsensitive(str).isEmpty();
}
@Override
public boolean hasHeaderWithValue(String str, String str2) {
Validate.notEmpty(str);
Validate.notEmpty(str2);
Iterator<String> it = headers(str).iterator();
while (it.hasNext()) {
if (str2.equalsIgnoreCase(it.next())) {
return true;
}
}
return false;
}
@Override
public String header(String str) {
Validate.notNull(str, "Header name must not be null");
List<String> headersCaseInsensitive = getHeadersCaseInsensitive(str);
if (headersCaseInsensitive.size() > 0) {
return StringUtil.join(headersCaseInsensitive, ", ");
}
return null;
}
@Override
public List<String> headers(String str) {
Validate.notEmpty(str);
return getHeadersCaseInsensitive(str);
}
@Override
public Connection.Method method() {
return this.method;
}
@Override
public Map<String, List<String>> multiHeaders() {
return this.headers;
}
@Override
public T removeCookie(String str) {
Validate.notEmpty(str, "Cookie name must not be empty");
this.cookies.remove(str);
return this;
}
@Override
public T removeHeader(String str) {
Validate.notEmpty(str, "Header name must not be empty");
Map.Entry<String, List<String>> scanHeaders = scanHeaders(str);
if (scanHeaders != null) {
this.headers.remove(scanHeaders.getKey());
}
return this;
}
@Override
public URL url() {
URL url = this.url;
if (url != UnsetUrl) {
return url;
}
throw new IllegalArgumentException("URL not set. Make sure to call #url(...) before executing the request.");
}
@Override
public T method(Connection.Method method) {
Validate.notNull(method, "Method must not be null");
this.method = method;
return this;
}
private Base() {
this.url = UnsetUrl;
this.method = Connection.Method.GET;
this.headers = new LinkedHashMap();
this.cookies = new LinkedHashMap();
}
@Override
public T cookie(String str, String str2) {
Validate.notEmpty(str, "Cookie name must not be empty");
Validate.notNull(str2, "Cookie value must not be null");
this.cookies.put(str, str2);
return this;
}
@Override
public Map<String, String> headers() {
LinkedHashMap linkedHashMap = new LinkedHashMap(this.headers.size());
for (Map.Entry<String, List<String>> entry : this.headers.entrySet()) {
String key = entry.getKey();
List<String> value = entry.getValue();
if (value.size() > 0) {
linkedHashMap.put(key, value.get(0));
}
}
return linkedHashMap;
}
@Override
public T url(URL url) {
Validate.notNull(url, "URL must not be null");
this.url = HttpConnection.punyUrl(url);
return this;
}
@Override
public T header(String str, String str2) {
Validate.notEmpty(str, "Header name must not be empty");
removeHeader(str);
addHeader(str, str2);
return this;
}
private Base(Base<T> base) {
this.url = UnsetUrl;
this.method = Connection.Method.GET;
this.url = base.url;
this.method = base.method;
this.headers = new LinkedHashMap();
for (Map.Entry<String, List<String>> entry : base.headers.entrySet()) {
this.headers.put(entry.getKey(), new ArrayList(entry.getValue()));
}
LinkedHashMap linkedHashMap = new LinkedHashMap();
this.cookies = linkedHashMap;
linkedHashMap.putAll(base.cookies);
}
}
public static class KeyVal implements Connection.KeyVal {
@Nullable
private String contentType;
private String key;
@Nullable
private InputStream stream;
private String value;
private KeyVal(String str, String str2) {
Validate.notEmpty(str, "Data key must not be empty");
Validate.notNull(str2, "Data value must not be null");
this.key = str;
this.value = str2;
}
public static KeyVal create(String str, String str2) {
return new KeyVal(str, str2);
}
@Override
public Connection.KeyVal contentType(String str) {
Validate.notEmpty(str);
this.contentType = str;
return this;
}
@Override
public boolean hasInputStream() {
return this.stream != null;
}
public String toString() {
return this.key + "=" + this.value;
}
public static KeyVal create(String str, String str2, InputStream inputStream) {
return new KeyVal(str, str2).inputStream(inputStream);
}
@Override
public KeyVal inputStream(InputStream inputStream) {
Validate.notNull(this.value, "Data input stream must not be null");
this.stream = inputStream;
return this;
}
@Override
public KeyVal key(String str) {
Validate.notEmpty(str, "Data key must not be empty");
this.key = str;
return this;
}
@Override
public KeyVal value(String str) {
Validate.notNull(str, "Data value must not be null");
this.value = str;
return this;
}
@Override
public String contentType() {
return this.contentType;
}
@Override
public InputStream inputStream() {
return this.stream;
}
@Override
public String key() {
return this.key;
}
@Override
public String value() {
return this.value;
}
}
public static class Request extends Base<Connection.Request> implements Connection.Request {
@Nullable
private String body;
private CookieManager cookieManager;
private final Collection<Connection.KeyVal> data;
private volatile boolean executing;
private boolean followRedirects;
private boolean ignoreContentType;
private boolean ignoreHttpErrors;
private int maxBodySizeBytes;
private Parser parser;
private boolean parserDefined;
private String postDataCharset;
@Nullable
private Proxy proxy;
@Nullable
private SSLSocketFactory sslSocketFactory;
private int timeoutMilliseconds;
static {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
public Request() {
super();
this.body = null;
this.ignoreHttpErrors = false;
this.ignoreContentType = false;
this.parserDefined = false;
this.postDataCharset = DataUtil.defaultCharsetName;
this.executing = false;
this.timeoutMilliseconds = 30000;
this.maxBodySizeBytes = 2097152;
this.followRedirects = true;
this.data = new ArrayList();
this.method = Connection.Method.GET;
addHeader("Accept-Encoding", Constants.CP_GZIP);
addHeader(HttpConnection.USER_AGENT, HttpConnection.DEFAULT_UA);
this.parser = Parser.htmlParser();
this.cookieManager = new CookieManager();
}
@Override
public Connection.Request addHeader(String str, String str2) {
return super.addHeader(str, str2);
}
@Override
public String cookie(String str) {
return super.cookie(str);
}
public CookieManager cookieManager() {
return this.cookieManager;
}
@Override
public Map cookies() {
return super.cookies();
}
@Override
public boolean followRedirects() {
return this.followRedirects;
}
@Override
public boolean hasCookie(String str) {
return super.hasCookie(str);
}
@Override
public boolean hasHeader(String str) {
return super.hasHeader(str);
}
@Override
public boolean hasHeaderWithValue(String str, String str2) {
return super.hasHeaderWithValue(str, str2);
}
@Override
public String header(String str) {
return super.header(str);
}
@Override
public List headers(String str) {
return super.headers(str);
}
@Override
public boolean ignoreContentType() {
return this.ignoreContentType;
}
@Override
public boolean ignoreHttpErrors() {
return this.ignoreHttpErrors;
}
@Override
public int maxBodySize() {
return this.maxBodySizeBytes;
}
@Override
public Connection.Request method(Connection.Method method) {
return super.method(method);
}
@Override
public Map multiHeaders() {
return super.multiHeaders();
}
@Override
public Connection.Request postDataCharset(String str) {
Validate.notNull(str, "Charset must not be null");
if (!Charset.isSupported(str)) {
throw new IllegalCharsetNameException(str);
}
this.postDataCharset = str;
return this;
}
@Override
public Connection.Request removeCookie(String str) {
return super.removeCookie(str);
}
@Override
public Connection.Request removeHeader(String str) {
return super.removeHeader(str);
}
@Override
public Connection.Request requestBody(@Nullable String str) {
this.body = str;
return this;
}
@Override
public SSLSocketFactory sslSocketFactory() {
return this.sslSocketFactory;
}
@Override
public URL url() {
return super.url();
}
@Override
public Connection.Request cookie(String str, String str2) {
return super.cookie(str, str2);
}
@Override
public Request data(Connection.KeyVal keyVal) {
Validate.notNull(keyVal, "Key val must not be null");
this.data.add(keyVal);
return this;
}
@Override
public Connection.Request followRedirects(boolean z) {
this.followRedirects = z;
return this;
}
@Override
public Connection.Request header(String str, String str2) {
return super.header(str, str2);
}
@Override
public Map headers() {
return super.headers();
}
@Override
public Connection.Request ignoreContentType(boolean z) {
this.ignoreContentType = z;
return this;
}
@Override
public Connection.Request ignoreHttpErrors(boolean z) {
this.ignoreHttpErrors = z;
return this;
}
@Override
public Connection.Request maxBodySize(int i) {
Validate.isTrue(i >= 0, "maxSize must be 0 (unlimited) or larger");
this.maxBodySizeBytes = i;
return this;
}
@Override
public Connection.Method method() {
return super.method();
}
@Override
public Request parser(Parser parser) {
this.parser = parser;
this.parserDefined = true;
return this;
}
@Override
public String requestBody() {
return this.body;
}
@Override
public void sslSocketFactory(SSLSocketFactory sSLSocketFactory) {
this.sslSocketFactory = sSLSocketFactory;
}
@Override
public int timeout() {
return this.timeoutMilliseconds;
}
@Override
public Connection.Request url(URL url) {
return super.url(url);
}
@Override
public Proxy proxy() {
return this.proxy;
}
@Override
public Request timeout(int i) {
Validate.isTrue(i >= 0, "Timeout milliseconds must be 0 (infinite) or greater");
this.timeoutMilliseconds = i;
return this;
}
@Override
public Collection<Connection.KeyVal> data() {
return this.data;
}
@Override
public Parser parser() {
return this.parser;
}
@Override
public Request proxy(@Nullable Proxy proxy) {
this.proxy = proxy;
return this;
}
@Override
public String postDataCharset() {
return this.postDataCharset;
}
@Override
public Request proxy(String str, int i) {
this.proxy = new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(str, i));
return this;
}
public Request(Request request) {
super(request);
this.body = null;
this.ignoreHttpErrors = false;
this.ignoreContentType = false;
this.parserDefined = false;
this.postDataCharset = DataUtil.defaultCharsetName;
this.executing = false;
this.proxy = request.proxy;
this.postDataCharset = request.postDataCharset;
this.timeoutMilliseconds = request.timeoutMilliseconds;
this.maxBodySizeBytes = request.maxBodySizeBytes;
this.followRedirects = request.followRedirects;
ArrayList arrayList = new ArrayList();
this.data = arrayList;
arrayList.addAll(request.data());
this.body = request.body;
this.ignoreHttpErrors = request.ignoreHttpErrors;
this.ignoreContentType = request.ignoreContentType;
this.parser = request.parser.newInstance();
this.parserDefined = request.parserDefined;
this.sslSocketFactory = request.sslSocketFactory;
this.cookieManager = request.cookieManager;
this.executing = false;
}
}
public static class Response extends Base<Connection.Response> implements Connection.Response {
private static final String LOCATION = "Location";
private static final int MAX_REDIRECTS = 20;
private static final Pattern xmlContentTypeRxp = Pattern.compile("(application|text)/\\w*\\+?xml.*");
@Nullable
private InputStream bodyStream;
@Nullable
private ByteBuffer byteData;
@Nullable
private String charset;
@Nullable
private HttpURLConnection conn;
@Nullable
private final String contentType;
private boolean executed;
private boolean inputStreamRead;
private int numRedirects;
private final Request req;
private final int statusCode;
private final String statusMessage;
public Response() {
super();
this.executed = false;
this.inputStreamRead = false;
this.numRedirects = 0;
this.statusCode = 400;
this.statusMessage = "Request not made";
this.req = new Request();
this.contentType = null;
}
private static HttpURLConnection createConnection(Request request) {
Proxy proxy = request.proxy();
HttpURLConnection httpURLConnection = (HttpURLConnection) (proxy == null ? request.url().openConnection() : request.url().openConnection(proxy));
httpURLConnection.setRequestMethod(request.method().name());
httpURLConnection.setInstanceFollowRedirects(false);
httpURLConnection.setConnectTimeout(request.timeout());
httpURLConnection.setReadTimeout(request.timeout() / 2);
if (request.sslSocketFactory() != null && (httpURLConnection instanceof HttpsURLConnection)) {
((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(request.sslSocketFactory());
}
if (request.method().hasBody()) {
httpURLConnection.setDoOutput(true);
}
CookieUtil.applyCookiesToRequest(request, httpURLConnection);
for (Map.Entry entry : request.multiHeaders().entrySet()) {
Iterator it = ((List) entry.getValue()).iterator();
while (it.hasNext()) {
httpURLConnection.addRequestProperty((String) entry.getKey(), (String) it.next());
}
}
return httpURLConnection;
}
private static LinkedHashMap<String, List<String>> createHeaderMap(HttpURLConnection httpURLConnection) {
LinkedHashMap<String, List<String>> linkedHashMap = new LinkedHashMap<>();
int i = 0;
while (true) {
String headerFieldKey = httpURLConnection.getHeaderFieldKey(i);
String headerField = httpURLConnection.getHeaderField(i);
if (headerFieldKey == null && headerField == null) {
return linkedHashMap;
}
i++;
if (headerFieldKey != null && headerField != null) {
if (linkedHashMap.containsKey(headerFieldKey)) {
linkedHashMap.get(headerFieldKey).add(headerField);
} else {
ArrayList arrayList = new ArrayList();
arrayList.add(headerField);
linkedHashMap.put(headerFieldKey, arrayList);
}
}
}
}
public static Response execute(Request request) {
return execute(request, null);
}
private void prepareByteData() {
Validate.isTrue(this.executed, "Request must be executed (with .execute(), .get(), or .post() before getting response body");
if (this.bodyStream == null || this.byteData != null) {
return;
}
Validate.isFalse(this.inputStreamRead, "Request has already been read (with .parse())");
try {
try {
this.byteData = DataUtil.readToByteBuffer(this.bodyStream, this.req.maxBodySize());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} finally {
this.inputStreamRead = true;
safeClose();
}
}
private void safeClose() {
InputStream inputStream = this.bodyStream;
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException unused) {
} catch (Throwable th) {
this.bodyStream = null;
throw th;
}
this.bodyStream = null;
}
HttpURLConnection httpURLConnection = this.conn;
if (httpURLConnection != null) {
httpURLConnection.disconnect();
this.conn = null;
}
}
private static void serialiseRequestUrl(Connection.Request request) {
boolean z;
URL url = request.url();
StringBuilder borrowBuilder = StringUtil.borrowBuilder();
borrowBuilder.append(url.getProtocol());
borrowBuilder.append("://");
borrowBuilder.append(url.getAuthority());
borrowBuilder.append(url.getPath());
borrowBuilder.append("?");
if (url.getQuery() != null) {
borrowBuilder.append(url.getQuery());
z = false;
} else {
z = true;
}
for (Connection.KeyVal keyVal : request.data()) {
Validate.isFalse(keyVal.hasInputStream(), "InputStream data not supported in URL query string.");
if (z) {
z = false;
} else {
borrowBuilder.append('&');
}
String key = keyVal.key();
String str = DataUtil.defaultCharsetName;
borrowBuilder.append(URLEncoder.encode(key, str));
borrowBuilder.append('=');
borrowBuilder.append(URLEncoder.encode(keyVal.value(), str));
}
request.url(new URL(StringUtil.releaseBuilder(borrowBuilder)));
request.data().clear();
}
@Nullable
private static String setOutputContentType(Connection.Request request) {
String header = request.header(HttpConnection.CONTENT_TYPE);
if (header != null) {
if (header.contains(HttpConnection.MULTIPART_FORM_DATA) && !header.contains("boundary")) {
String mimeBoundary = DataUtil.mimeBoundary();
request.header(HttpConnection.CONTENT_TYPE, "multipart/form-data; boundary=" + mimeBoundary);
return mimeBoundary;
}
} else {
if (HttpConnection.needsMultipart(request)) {
String mimeBoundary2 = DataUtil.mimeBoundary();
request.header(HttpConnection.CONTENT_TYPE, "multipart/form-data; boundary=" + mimeBoundary2);
return mimeBoundary2;
}
request.header(HttpConnection.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=" + request.postDataCharset());
}
return null;
}
private static void writePost(Connection.Request request, OutputStream outputStream, @Nullable String str) {
Collection<Connection.KeyVal> data = request.data();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, request.postDataCharset()));
if (str != null) {
for (Connection.KeyVal keyVal : data) {
bufferedWriter.write("--");
bufferedWriter.write(str);
bufferedWriter.write("\r\n");
bufferedWriter.write("Content-Disposition: form-data; name=\"");
bufferedWriter.write(HttpConnection.encodeMimeName(keyVal.key()));
bufferedWriter.write("\"");
InputStream inputStream = keyVal.inputStream();
if (inputStream != null) {
bufferedWriter.write("; filename=\"");
bufferedWriter.write(HttpConnection.encodeMimeName(keyVal.value()));
bufferedWriter.write("\"\r\nContent-Type: ");
String contentType = keyVal.contentType();
if (contentType == null) {
contentType = HttpConnection.DefaultUploadType;
}
bufferedWriter.write(contentType);
bufferedWriter.write("\r\n\r\n");
bufferedWriter.flush();
DataUtil.crossStreams(inputStream, outputStream);
outputStream.flush();
} else {
bufferedWriter.write("\r\n\r\n");
bufferedWriter.write(keyVal.value());
}
bufferedWriter.write("\r\n");
}
bufferedWriter.write("--");
bufferedWriter.write(str);
bufferedWriter.write("--");
} else {
String requestBody = request.requestBody();
if (requestBody != null) {
bufferedWriter.write(requestBody);
} else {
boolean z = true;
for (Connection.KeyVal keyVal2 : data) {
if (z) {
z = false;
} else {
bufferedWriter.append('&');
}
bufferedWriter.write(URLEncoder.encode(keyVal2.key(), request.postDataCharset()));
bufferedWriter.write(61);
bufferedWriter.write(URLEncoder.encode(keyVal2.value(), request.postDataCharset()));
}
}
}
bufferedWriter.close();
}
@Override
public Connection.Response addHeader(String str, String str2) {
return super.addHeader(str, str2);
}
@Override
public String body() {
prepareByteData();
Validate.notNull(this.byteData);
String str = this.charset;
String charBuffer = (str == null ? DataUtil.UTF_8 : Charset.forName(str)).decode(this.byteData).toString();
this.byteData.rewind();
return charBuffer;
}
@Override
public byte[] bodyAsBytes() {
prepareByteData();
Validate.notNull(this.byteData);
return this.byteData.array();
}
@Override
public BufferedInputStream bodyStream() {
Validate.isTrue(this.executed, "Request must be executed (with .execute(), .get(), or .post() before getting response body");
Validate.isFalse(this.inputStreamRead, "Request has already been read");
this.inputStreamRead = true;
return ConstrainableInputStream.wrap(this.bodyStream, 32768, this.req.maxBodySize());
}
@Override
public Connection.Response bufferUp() {
prepareByteData();
return this;
}
@Override
public String contentType() {
return this.contentType;
}
@Override
public String cookie(String str) {
return super.cookie(str);
}
@Override
public Map cookies() {
return super.cookies();
}
@Override
public boolean hasCookie(String str) {
return super.hasCookie(str);
}
@Override
public boolean hasHeader(String str) {
return super.hasHeader(str);
}
@Override
public boolean hasHeaderWithValue(String str, String str2) {
return super.hasHeaderWithValue(str, str2);
}
@Override
public String header(String str) {
return super.header(str);
}
@Override
public List headers(String str) {
return super.headers(str);
}
@Override
public Connection.Response method(Connection.Method method) {
return super.method(method);
}
@Override
public Map multiHeaders() {
return super.multiHeaders();
}
@Override
public Document parse() {
Validate.isTrue(this.executed, "Request must be executed (with .execute(), .get(), or .post() before parsing response");
if (this.byteData != null) {
this.bodyStream = new ByteArrayInputStream(this.byteData.array());
this.inputStreamRead = false;
}
Validate.isFalse(this.inputStreamRead, "Input stream already read and parsed, cannot re-read.");
Document parseInputStream = DataUtil.parseInputStream(this.bodyStream, this.charset, this.url.toExternalForm(), this.req.parser());
parseInputStream.connection(new HttpConnection(this.req, this));
this.charset = parseInputStream.outputSettings().charset().name();
this.inputStreamRead = true;
safeClose();
return parseInputStream;
}
public void processResponseHeaders(Map<String, List<String>> map) {
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
if (key != null) {
List<String> value = entry.getValue();
if (key.equalsIgnoreCase("Set-Cookie")) {
for (String str : value) {
if (str != null) {
TokenQueue tokenQueue = new TokenQueue(str);
String trim = tokenQueue.chompTo("=").trim();
String trim2 = tokenQueue.consumeTo(";").trim();
if (trim.length() > 0 && !this.cookies.containsKey(trim)) {
cookie(trim, trim2);
}
}
}
}
Iterator<String> it = value.iterator();
while (it.hasNext()) {
addHeader(key, it.next());
}
}
}
}
@Override
public Connection.Response removeCookie(String str) {
return super.removeCookie(str);
}
@Override
public Connection.Response removeHeader(String str) {
return super.removeHeader(str);
}
@Override
public int statusCode() {
return this.statusCode;
}
@Override
public String statusMessage() {
return this.statusMessage;
}
@Override
public URL url() {
return super.url();
}
public static org.jsoup.helper.HttpConnection.Response execute(org.jsoup.helper.HttpConnection.Request r8, @javax.annotation.Nullable org.jsoup.helper.HttpConnection.Response r9) {
throw new UnsupportedOperationException("Method not decompiled: org.jsoup.helper.HttpConnection.Response.execute(org.jsoup.helper.HttpConnection$Request, org.jsoup.helper.HttpConnection$Response):org.jsoup.helper.HttpConnection$Response");
}
@Override
public String charset() {
return this.charset;
}
@Override
public Connection.Response cookie(String str, String str2) {
return super.cookie(str, str2);
}
@Override
public Connection.Response header(String str, String str2) {
return super.header(str, str2);
}
@Override
public Map headers() {
return super.headers();
}
@Override
public Connection.Method method() {
return super.method();
}
@Override
public Connection.Response url(URL url) {
return super.url(url);
}
@Override
public Response charset(String str) {
this.charset = str;
return this;
}
private Response(HttpURLConnection httpURLConnection, Request request, @Nullable Response response) {
super();
this.executed = false;
this.inputStreamRead = false;
this.numRedirects = 0;
this.conn = httpURLConnection;
this.req = request;
this.method = Connection.Method.valueOf(httpURLConnection.getRequestMethod());
this.url = httpURLConnection.getURL();
this.statusCode = httpURLConnection.getResponseCode();
this.statusMessage = httpURLConnection.getResponseMessage();
this.contentType = httpURLConnection.getContentType();
LinkedHashMap<String, List<String>> createHeaderMap = createHeaderMap(httpURLConnection);
processResponseHeaders(createHeaderMap);
CookieUtil.storeCookies(request, this.url, createHeaderMap);
if (response != null) {
for (Map.Entry entry : response.cookies().entrySet()) {
if (!hasCookie((String) entry.getKey())) {
cookie((String) entry.getKey(), (String) entry.getValue());
}
}
response.safeClose();
int i = response.numRedirects + 1;
this.numRedirects = i;
if (i >= 20) {
throw new IOException(String.format("Too many redirects occurred trying to load URL %s", response.url()));
}
}
}
}
public static Connection connect(String str) {
HttpConnection httpConnection = new HttpConnection();
httpConnection.url(str);
return httpConnection;
}
public static String encodeMimeName(String str) {
return str.replace("\"", "%22");
}
private static String encodeUrl(String str) {
try {
return encodeUrl(new URL(str)).toExternalForm();
} catch (Exception unused) {
return str;
}
}
public static boolean needsMultipart(Connection.Request request) {
Iterator<Connection.KeyVal> it = request.data().iterator();
while (it.hasNext()) {
if (it.next().hasInputStream()) {
return true;
}
}
return false;
}
public static URL punyUrl(URL url) {
if (StringUtil.isAscii(url.getHost())) {
return url;
}
try {
return new URL(url.getProtocol(), IDN.toASCII(url.getHost()), url.getPort(), url.getFile());
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public Connection cookie(String str, String str2) {
this.req.cookie(str, str2);
return this;
}
@Override
public Connection cookieStore(CookieStore cookieStore) {
this.req.cookieManager = new CookieManager(cookieStore, null);
return this;
}
@Override
public Connection cookies(Map<String, String> map) {
Validate.notNull(map, "Cookie map must not be null");
for (Map.Entry<String, String> entry : map.entrySet()) {
this.req.cookie(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public Connection data(String str, String str2) {
this.req.data((Connection.KeyVal) KeyVal.create(str, str2));
return this;
}
@Override
public Connection.Response execute() {
Response execute = Response.execute(this.req);
this.res = execute;
return execute;
}
@Override
public Connection followRedirects(boolean z) {
this.req.followRedirects(z);
return this;
}
@Override
public Document get() {
this.req.method(Connection.Method.GET);
execute();
Validate.notNull(this.res);
return this.res.parse();
}
@Override
public Connection header(String str, String str2) {
this.req.header(str, str2);
return this;
}
@Override
public Connection headers(Map<String, String> map) {
Validate.notNull(map, "Header map must not be null");
for (Map.Entry<String, String> entry : map.entrySet()) {
this.req.header(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public Connection ignoreContentType(boolean z) {
this.req.ignoreContentType(z);
return this;
}
@Override
public Connection ignoreHttpErrors(boolean z) {
this.req.ignoreHttpErrors(z);
return this;
}
@Override
public Connection maxBodySize(int i) {
this.req.maxBodySize(i);
return this;
}
@Override
public Connection method(Connection.Method method) {
this.req.method(method);
return this;
}
@Override
public Connection newRequest() {
return new HttpConnection(this.req);
}
@Override
public Connection parser(Parser parser) {
this.req.parser(parser);
return this;
}
@Override
public Document post() {
this.req.method(Connection.Method.POST);
execute();
Validate.notNull(this.res);
return this.res.parse();
}
@Override
public Connection postDataCharset(String str) {
this.req.postDataCharset(str);
return this;
}
@Override
public Connection proxy(@Nullable Proxy proxy) {
this.req.proxy(proxy);
return this;
}
@Override
public Connection referrer(String str) {
Validate.notNull(str, "Referrer must not be null");
this.req.header("Referer", str);
return this;
}
@Override
public Connection.Request request() {
return this.req;
}
@Override
public Connection requestBody(String str) {
this.req.requestBody(str);
return this;
}
@Override
public Connection.Response response() {
Connection.Response response = this.res;
if (response != null) {
return response;
}
throw new IllegalArgumentException("You must execute the request before getting a response.");
}
@Override
public Connection sslSocketFactory(SSLSocketFactory sSLSocketFactory) {
this.req.sslSocketFactory(sSLSocketFactory);
return this;
}
@Override
public Connection timeout(int i) {
this.req.timeout(i);
return this;
}
@Override
public Connection url(URL url) {
this.req.url(url);
return this;
}
@Override
public Connection userAgent(String str) {
Validate.notNull(str, "User agent must not be null");
this.req.header(USER_AGENT, str);
return this;
}
public HttpConnection() {
this.req = new Request();
}
@Override
public CookieStore cookieStore() {
return this.req.cookieManager.getCookieStore();
}
@Override
public Connection data(String str, String str2, InputStream inputStream) {
this.req.data((Connection.KeyVal) KeyVal.create(str, str2, inputStream));
return this;
}
@Override
public Connection proxy(String str, int i) {
this.req.proxy(str, i);
return this;
}
@Override
public Connection request(Connection.Request request) {
this.req = (Request) request;
return this;
}
@Override
public Connection url(String str) {
Validate.notEmpty(str, "Must supply a valid URL");
try {
this.req.url(new URL(encodeUrl(str)));
return this;
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Malformed URL: " + str, e);
}
}
public static Connection connect(URL url) {
HttpConnection httpConnection = new HttpConnection();
httpConnection.url(url);
return httpConnection;
}
public static URL encodeUrl(URL url) {
URL punyUrl = punyUrl(url);
try {
return new URL(new URI(punyUrl.toExternalForm().replace(" ", "%20")).toASCIIString());
} catch (MalformedURLException | URISyntaxException unused) {
return punyUrl;
}
}
@Override
public Connection data(String str, String str2, InputStream inputStream, String str3) {
this.req.data(KeyVal.create(str, str2, inputStream).contentType(str3));
return this;
}
@Override
public Connection response(Connection.Response response) {
this.res = response;
return this;
}
public HttpConnection(Request request) {
this.req = new Request(request);
}
@Override
public Connection data(Map<String, String> map) {
Validate.notNull(map, "Data map must not be null");
for (Map.Entry<String, String> entry : map.entrySet()) {
this.req.data((Connection.KeyVal) KeyVal.create(entry.getKey(), entry.getValue()));
}
return this;
}
private HttpConnection(Request request, Response response) {
this.req = request;
this.res = response;
}
@Override
public Connection data(String... strArr) {
Validate.notNull(strArr, "Data key value pairs must not be null");
Validate.isTrue(strArr.length % 2 == 0, "Must supply an even number of key value pairs");
for (int i = 0; i < strArr.length; i += 2) {
String str = strArr[i];
String str2 = strArr[i + 1];
Validate.notEmpty(str, "Data key must not be empty");
Validate.notNull(str2, "Data value must not be null");
this.req.data((Connection.KeyVal) KeyVal.create(str, str2));
}
return this;
}
@Override
public Connection data(Collection<Connection.KeyVal> collection) {
Validate.notNull(collection, "Data collection must not be null");
Iterator<Connection.KeyVal> it = collection.iterator();
while (it.hasNext()) {
this.req.data(it.next());
}
return this;
}
@Override
public Connection.KeyVal data(String str) {
Validate.notEmpty(str, "Data key must not be empty");
for (Connection.KeyVal keyVal : request().data()) {
if (keyVal.key().equals(str)) {
return keyVal;
}
}
return null;
}
}