野火视频 v2.3.5版本的 MD5 值为:102ebe58fcb84dd006410b5d2f5a93ba
以下内容为反编译后的 HttpConnection.java 源代码,内容仅作参考
package org.jsoup.helper;
import android.support.v4.media.e;
import androidx.appcompat.view.a;
import com.efs.sdk.base.Constants;
import com.google.android.exoplayer2.C;
import com.google.common.net.HttpHeaders;
import com.google.common.primitives.UnsignedBytes;
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.URLConnection;
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.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import javax.annotation.Nullable;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import org.jsoup.Connection;
import org.jsoup.HttpStatusException;
import org.jsoup.UncheckedIOException;
import org.jsoup.UnsupportedMimeTypeException;
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(C.UTF8_NAME);
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 e4) {
throw new IllegalStateException(e4);
}
}
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 i4;
int i5 = 3;
i5 = (bArr.length >= 3 && (bArr[0] & 255) == 239 && (bArr[1] & 255) == 187 && (bArr[2] & 255) == 191) ? 0 : 0;
int length = bArr.length;
while (i5 < length) {
byte b4 = bArr[i5];
if ((b4 & UnsignedBytes.MAX_POWER_OF_TWO) != 0) {
if ((b4 & 224) == 192) {
i4 = i5 + 1;
} else if ((b4 & 240) == 224) {
i4 = i5 + 2;
} else if ((b4 & 248) != 240) {
return false;
} else {
i4 = i5 + 3;
}
if (i4 >= bArr.length) {
return false;
}
while (i5 < i4) {
i5++;
if ((bArr[i5] & 192) != 128) {
return false;
}
}
continue;
}
i5++;
}
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);
for (String str3 : headers(str)) {
if (str2.equalsIgnoreCase(str3)) {
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(HttpHeaders.ACCEPT_ENCODING, Constants.CP_GZIP);
addHeader("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)) {
this.postDataCharset = str;
return this;
}
throw new IllegalCharsetNameException(str);
}
@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 z3) {
this.followRedirects = z3;
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 z3) {
this.ignoreContentType = z3;
return this;
}
@Override
public Connection.Request ignoreHttpErrors(boolean z3) {
this.ignoreHttpErrors = z3;
return this;
}
@Override
public Connection.Request maxBodySize(int i4) {
Validate.isTrue(i4 >= 0, "maxSize must be 0 (unlimited) or larger");
this.maxBodySizeBytes = i4;
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 i4) {
Validate.isTrue(i4 >= 0, "Timeout milliseconds must be 0 (infinite) or greater");
this.timeoutMilliseconds = i4;
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 i4) {
this.proxy = new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(str, i4));
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) throws IOException {
URLConnection openConnection;
Proxy proxy = request.proxy();
if (proxy == null) {
openConnection = request.url().openConnection();
} else {
openConnection = request.url().openConnection(proxy);
}
HttpURLConnection httpURLConnection = (HttpURLConnection) openConnection;
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()) {
for (String str : (List) entry.getValue()) {
httpURLConnection.addRequestProperty((String) entry.getKey(), str);
}
}
return httpURLConnection;
}
private static LinkedHashMap<String, List<String>> createHeaderMap(HttpURLConnection httpURLConnection) {
LinkedHashMap<String, List<String>> linkedHashMap = new LinkedHashMap<>();
int i4 = 0;
while (true) {
String headerFieldKey = httpURLConnection.getHeaderFieldKey(i4);
String headerField = httpURLConnection.getHeaderField(i4);
if (headerFieldKey == null && headerField == null) {
return linkedHashMap;
}
i4++;
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) throws IOException {
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 e4) {
throw new UncheckedIOException(e4);
}
} 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) throws IOException {
boolean z3;
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());
z3 = false;
} else {
z3 = true;
}
for (Connection.KeyVal keyVal : request.data()) {
Validate.isFalse(keyVal.hasInputStream(), "InputStream data not supported in URL query string.");
if (z3) {
z3 = 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("Content-Type");
if (header == null) {
if (HttpConnection.needsMultipart(request)) {
String mimeBoundary = DataUtil.mimeBoundary();
request.header("Content-Type", "multipart/form-data; boundary=" + mimeBoundary);
return mimeBoundary;
}
StringBuilder a4 = e.a("application/x-www-form-urlencoded; charset=");
a4.append(request.postDataCharset());
request.header("Content-Type", a4.toString());
} else if (header.contains(HttpConnection.MULTIPART_FORM_DATA) && !header.contains("boundary")) {
String mimeBoundary2 = DataUtil.mimeBoundary();
request.header("Content-Type", "multipart/form-data; boundary=" + mimeBoundary2);
return mimeBoundary2;
}
return null;
}
private static void writePost(Connection.Request request, OutputStream outputStream, @Nullable String str) throws IOException {
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 z3 = true;
for (Connection.KeyVal keyVal2 : data) {
if (z3) {
z3 = 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() throws IOException {
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(HttpHeaders.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);
}
}
}
}
for (String str2 : value) {
addHeader(key, str2);
}
}
}
}
@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 Response execute(Request request, @Nullable Response response) throws IOException {
String outputContentType;
HttpURLConnection createConnection;
int responseCode;
Response response2;
synchronized (request) {
Validate.isFalse(request.executing, "Multiple threads were detected trying to execute the same request concurrently. Make sure to use Connection#newRequest() and do not share an executing request between threads.");
request.executing = true;
}
Validate.notNull(request, "Request must not be null");
URL url = request.url();
Validate.notNull(url, "URL must be specified to connect");
String protocol = url.getProtocol();
if (!protocol.equals("http") && !protocol.equals("https")) {
throw new MalformedURLException("Only http & https protocols supported");
}
boolean hasBody = request.method().hasBody();
boolean z3 = request.requestBody() != null;
if (!hasBody) {
StringBuilder a4 = e.a("Cannot set a request body for HTTP method ");
a4.append(request.method());
Validate.isFalse(z3, a4.toString());
}
Response response3 = null;
try {
try {
if (request.data().size() > 0 && (!hasBody || z3)) {
serialiseRequestUrl(request);
} else if (hasBody) {
outputContentType = setOutputContentType(request);
long nanoTime = System.nanoTime();
createConnection = createConnection(request);
createConnection.connect();
if (createConnection.getDoOutput()) {
OutputStream outputStream = createConnection.getOutputStream();
try {
try {
writePost(request, outputStream, outputContentType);
} finally {
outputStream.close();
}
} catch (IOException e4) {
createConnection.disconnect();
throw e4;
}
}
responseCode = createConnection.getResponseCode();
response2 = new Response(createConnection, request, response);
if (!response2.hasHeader("Location") && request.followRedirects()) {
if (responseCode != 307) {
request.method(Connection.Method.GET);
request.data().clear();
request.requestBody(null);
request.removeHeader("Content-Type");
}
String header = response2.header("Location");
Validate.notNull(header);
if (header.startsWith("http:/") && header.charAt(6) != '/') {
header = header.substring(6);
}
request.url(HttpConnection.encodeUrl(StringUtil.resolve(request.url(), header)));
request.executing = false;
return execute(request, response2);
} else if ((responseCode < 200 && responseCode < 400) || request.ignoreHttpErrors()) {
String contentType = response2.contentType();
if (contentType != null && !request.ignoreContentType() && !contentType.startsWith("text/") && !xmlContentTypeRxp.matcher(contentType).matches()) {
throw new UnsupportedMimeTypeException("Unhandled content type. Must be text/*, application/xml, or application/*+xml", contentType, request.url().toString());
}
response2.charset = DataUtil.getCharsetFromContentType(response2.contentType);
if (createConnection.getContentLength() != 0 && request.method() != Connection.Method.HEAD) {
InputStream errorStream = createConnection.getErrorStream() != null ? createConnection.getErrorStream() : createConnection.getInputStream();
response2.bodyStream = errorStream;
Validate.notNull(errorStream);
if (response2.hasHeaderWithValue("Content-Encoding", Constants.CP_GZIP)) {
response2.bodyStream = new GZIPInputStream(response2.bodyStream);
} else if (response2.hasHeaderWithValue("Content-Encoding", "deflate")) {
response2.bodyStream = new InflaterInputStream(response2.bodyStream, new Inflater(true));
}
response2.bodyStream = ConstrainableInputStream.wrap(response2.bodyStream, 32768, request.maxBodySize()).timeout(nanoTime, request.timeout());
} else {
response2.byteData = DataUtil.emptyByteBuffer();
}
request.executing = false;
response2.executed = true;
return response2;
} else {
throw new HttpStatusException("HTTP error fetching URL", responseCode, request.url().toString());
}
}
if (!response2.hasHeader("Location")) {
}
if (responseCode < 200) {
}
throw new HttpStatusException("HTTP error fetching URL", responseCode, request.url().toString());
} catch (IOException e5) {
e = e5;
response3 = response2;
if (response3 != null) {
response3.safeClose();
}
throw e;
}
createConnection.connect();
if (createConnection.getDoOutput()) {
}
responseCode = createConnection.getResponseCode();
response2 = new Response(createConnection, request, response);
} finally {
request.executing = false;
}
outputContentType = null;
long nanoTime2 = System.nanoTime();
createConnection = createConnection(request);
}
@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) throws IOException {
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 i4 = response.numRedirects + 1;
this.numRedirects = i4;
if (i4 >= 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) {
for (Connection.KeyVal keyVal : request.data()) {
if (keyVal.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 e4) {
throw new IllegalArgumentException(e4);
}
}
@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() throws IOException {
Response execute = Response.execute(this.req);
this.res = execute;
return execute;
}
@Override
public Connection followRedirects(boolean z3) {
this.req.followRedirects(z3);
return this;
}
@Override
public Document get() throws IOException {
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 z3) {
this.req.ignoreContentType(z3);
return this;
}
@Override
public Connection ignoreHttpErrors(boolean z3) {
this.req.ignoreHttpErrors(z3);
return this;
}
@Override
public Connection maxBodySize(int i4) {
this.req.maxBodySize(i4);
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() throws IOException {
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(HttpHeaders.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 i4) {
this.req.timeout(i4);
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 i4) {
this.req.proxy(str, i4);
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 e4) {
throw new IllegalArgumentException(a.a("Malformed URL: ", str), e4);
}
}
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 i4 = 0; i4 < strArr.length; i4 += 2) {
String str = strArr[i4];
String str2 = strArr[i4 + 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");
for (Connection.KeyVal keyVal : collection) {
this.req.data(keyVal);
}
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;
}
}