正在查看: Kreate v1.5.0 应用的 HttpConnection.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: Kreate v1.5.0 应用的 HttpConnection.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package org.jsoup.helper;
import j$.net.URLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
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.net.ssl.SSLSocketFactory;
import org.apache.commons.math3.optimization.direct.CMAESOptimizer;
import org.jsoup.Connection;
import org.jsoup.Progress;
import org.jsoup.helper.DataUtil;
import org.jsoup.internal.ControllableInputStream;
import org.jsoup.internal.Normalizer;
import org.jsoup.internal.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.jsoup.parser.StreamParser;
import org.mozilla.javascript.ES6Iterator;
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";
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;
private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
public static final String MULTIPART_FORM_DATA = "multipart/form-data";
private static final String USER_AGENT = "User-Agent";
Object client;
RequestAuthenticator lastAuth;
private Request req;
private Connection.Response res;
@Override
public Connection newRequest(String str) {
Connection url;
url = newRequest().url(str);
return url;
}
@Override
public Connection newRequest(URL url) {
Connection url2;
url2 = newRequest().url(url);
return url2;
}
public static Connection connect(String str) {
HttpConnection httpConnection = new HttpConnection();
httpConnection.url(str);
return httpConnection;
}
public static Connection connect(URL url) {
HttpConnection httpConnection = new HttpConnection();
httpConnection.url(url);
return httpConnection;
}
public HttpConnection() {
Request request = new Request();
this.req = request;
request.connection = this;
}
HttpConnection(Request request) {
this.req = new Request(request);
}
static String encodeMimeName(String str) {
return str.replace("\"", "%22");
}
@Override
public Connection newRequest() {
return new HttpConnection(this.req);
}
private HttpConnection(Request request, Response response) {
this.req = request;
this.res = response;
}
@Override
public Connection url(URL url) {
this.req.url(url);
return this;
}
@Override
public Connection url(String str) {
Validate.notEmptyParam(str, "url");
try {
this.req.url(new URL(str));
return this;
} catch (MalformedURLException e) {
throw new IllegalArgumentException(String.format("The supplied URL, '%s', is malformed. Make sure it is an absolute URL, and starts with 'http://' or 'https://'. See https://jsoup.org/cookbook/extracting-data/working-with-urls", str), e);
}
}
@Override
public Connection proxy(Proxy proxy) {
this.req.proxy(proxy);
return this;
}
@Override
public Connection proxy(String str, int i) {
this.req.proxy(str, i);
return this;
}
@Override
public Connection userAgent(String str) {
Validate.notNullParam(str, "userAgent");
this.req.header(USER_AGENT, str);
return this;
}
@Override
public Connection timeout(int i) {
this.req.timeout(i);
return this;
}
@Override
public Connection maxBodySize(int i) {
this.req.maxBodySize(i);
return this;
}
@Override
public Connection followRedirects(boolean z) {
this.req.followRedirects(z);
return this;
}
@Override
public Connection referrer(String str) {
Validate.notNullParam(str, "referrer");
this.req.header("Referer", str);
return this;
}
@Override
public Connection method(Connection.Method method) {
this.req.method(method);
return this;
}
@Override
public Connection ignoreHttpErrors(boolean z) {
this.req.ignoreHttpErrors(z);
return this;
}
@Override
public Connection ignoreContentType(boolean z) {
this.req.ignoreContentType(z);
return this;
}
@Override
public Connection data(String str, String str2) {
this.req.data((Connection.KeyVal) KeyVal.create(str, str2));
return this;
}
@Override
public Connection sslSocketFactory(SSLSocketFactory sSLSocketFactory) {
this.req.sslSocketFactory(sSLSocketFactory);
return this;
}
@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 data(String str, String str2, InputStream inputStream, String str3) {
this.req.data(KeyVal.create(str, str2, inputStream).contentType(str3));
return this;
}
@Override
public Connection data(Map<String, String> map) {
Validate.notNullParam(map, "data");
for (Map.Entry<String, String> entry : map.entrySet()) {
this.req.data((Connection.KeyVal) KeyVal.create(entry.getKey(), entry.getValue()));
}
return this;
}
@Override
public Connection data(String... strArr) {
Validate.notNullParam(strArr, "keyvals");
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.notNullParam(collection, "data");
Iterator<Connection.KeyVal> it = collection.iterator();
while (it.hasNext()) {
this.req.data(it.next());
}
return this;
}
@Override
public Connection.KeyVal data(String str) {
Validate.notEmptyParam(str, "key");
for (Connection.KeyVal keyVal : request().data()) {
if (keyVal.key().equals(str)) {
return keyVal;
}
}
return null;
}
@Override
public Connection requestBody(String str) {
this.req.requestBody(str);
return this;
}
@Override
public Connection header(String str, String str2) {
this.req.header(str, str2);
return this;
}
@Override
public Connection headers(Map<String, String> map) {
Validate.notNullParam(map, "headers");
for (Map.Entry<String, String> entry : map.entrySet()) {
this.req.header(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public Connection cookie(String str, String str2) {
this.req.cookie(str, str2);
return this;
}
@Override
public Connection cookies(Map<String, String> map) {
Validate.notNullParam(map, "cookies");
for (Map.Entry<String, String> entry : map.entrySet()) {
this.req.cookie(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public Connection cookieStore(CookieStore cookieStore) {
this.req.cookieManager = new CookieManager(cookieStore, null);
return this;
}
@Override
public CookieStore cookieStore() {
return this.req.cookieManager.getCookieStore();
}
@Override
public Connection parser(Parser parser) {
this.req.parser(parser);
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 Document post() throws IOException {
this.req.method(Connection.Method.POST);
execute();
Validate.notNull(this.res);
return this.res.parse();
}
@Override
public Connection.Response execute() throws IOException {
Response execute = Response.execute(this.req);
this.res = execute;
return execute;
}
@Override
public Connection.Request request() {
return this.req;
}
@Override
public Connection request(Connection.Request request) {
this.req = (Request) request;
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 response(Connection.Response response) {
this.res = response;
return this;
}
@Override
public Connection postDataCharset(String str) {
this.req.postDataCharset(str);
return this;
}
@Override
public Connection auth(RequestAuthenticator requestAuthenticator) {
this.req.auth(requestAuthenticator);
return this;
}
@Override
public Connection onResponseProgress(Progress<Connection.Response> progress) {
this.req.responseProgress = progress;
return this;
}
static abstract class Base<T extends Connection.Base<T>> implements Connection.Base<T> {
private static final URL UnsetUrl;
Map<String, String> cookies;
Map<String, List<String>> headers;
Connection.Method method;
URL url;
static {
try {
UnsetUrl = new URL("http://undefined/");
} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}
}
private Base() {
this.url = UnsetUrl;
this.method = Connection.Method.GET;
this.headers = new LinkedHashMap();
this.cookies = new LinkedHashMap();
}
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);
}
@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 url(URL url) {
Validate.notNullParam(url, "url");
this.url = new UrlBuilder(url).build();
return this;
}
@Override
public Connection.Method method() {
return this.method;
}
@Override
public T method(Connection.Method method) {
Validate.notNullParam(method, "method");
this.method = method;
return this;
}
@Override
public String header(String str) {
Validate.notNullParam(str, "name");
List<String> headersCaseInsensitive = getHeadersCaseInsensitive(str);
if (headersCaseInsensitive.isEmpty()) {
return null;
}
return StringUtil.join(headersCaseInsensitive, ", ");
}
@Override
public T addHeader(String str, String str2) {
Validate.notEmptyParam(str, "name");
if (str2 == null) {
str2 = "";
}
List<String> headers = headers(str);
if (headers.isEmpty()) {
headers = new ArrayList<>();
this.headers.put(str, headers);
}
headers.add(str2);
return this;
}
@Override
public List<String> headers(String str) {
Validate.notEmptyParam(str, "name");
return getHeadersCaseInsensitive(str);
}
@Override
public T header(String str, String str2) {
Validate.notEmptyParam(str, "name");
removeHeader(str);
addHeader(str, str2);
return this;
}
@Override
public boolean hasHeader(String str) {
Validate.notEmptyParam(str, "name");
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 T removeHeader(String str) {
Validate.notEmptyParam(str, "name");
Map.Entry<String, List<String>> scanHeaders = scanHeaders(str);
if (scanHeaders != null) {
this.headers.remove(scanHeaders.getKey());
}
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.isEmpty()) {
linkedHashMap.put(key, value.get(0));
}
}
return linkedHashMap;
}
@Override
public Map<String, List<String>> multiHeaders() {
return this.headers;
}
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.EMPTY_LIST;
}
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 String cookie(String str) {
Validate.notEmptyParam(str, "name");
return this.cookies.get(str);
}
@Override
public T cookie(String str, String str2) {
Validate.notEmptyParam(str, "name");
Validate.notNullParam(str2, ES6Iterator.VALUE_PROPERTY);
this.cookies.put(str, str2);
return this;
}
@Override
public boolean hasCookie(String str) {
Validate.notEmptyParam(str, "name");
return this.cookies.containsKey(str);
}
@Override
public T removeCookie(String str) {
Validate.notEmptyParam(str, "name");
this.cookies.remove(str);
return this;
}
@Override
public Map<String, String> cookies() {
return this.cookies;
}
}
public static class Request extends Base<Connection.Request> implements Connection.Request {
RequestAuthenticator authenticator;
private String body;
HttpConnection connection;
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;
String mimeBoundary;
private Parser parser;
private boolean parserDefined;
private String postDataCharset;
private Proxy proxy;
private Progress<Connection.Response> responseProgress;
private SSLSocketFactory sslSocketFactory;
private int timeoutMilliseconds;
@Override
public Connection.Request addHeader(String str, String str2) {
return super.addHeader(str, str2);
}
@Override
public String cookie(String str) {
return super.cookie(str);
}
@Override
public Connection.Request cookie(String str, String str2) {
return super.cookie(str, str2);
}
@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 Connection.Request header(String str, String str2) {
return super.header(str, str2);
}
@Override
public List headers(String str) {
return super.headers(str);
}
@Override
public Map headers() {
return super.headers();
}
@Override
public Connection.Request method(Connection.Method method) {
return super.method(method);
}
@Override
public Connection.Method method() {
return super.method();
}
@Override
public Map multiHeaders() {
return super.multiHeaders();
}
@Override
public Connection.Request removeCookie(String str) {
return super.removeCookie(str);
}
@Override
public Connection.Request removeHeader(String str) {
return super.removeHeader(str);
}
@Override
public URL url() {
return super.url();
}
@Override
public Connection.Request url(URL url) {
return super.url(url);
}
static {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
Request() {
super();
this.body = null;
this.ignoreHttpErrors = false;
this.ignoreContentType = false;
this.parserDefined = false;
this.postDataCharset = DataUtil.defaultCharsetName;
this.executing = false;
this.timeoutMilliseconds = CMAESOptimizer.DEFAULT_MAXITERATIONS;
this.maxBodySizeBytes = 2097152;
this.followRedirects = true;
this.data = new ArrayList();
this.method = Connection.Method.GET;
addHeader("Accept-Encoding", "gzip");
addHeader(HttpConnection.USER_AGENT, HttpConnection.DEFAULT_UA);
this.parser = Parser.htmlParser();
this.cookieManager = new CookieManager();
}
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.connection = request.connection;
this.proxy = request.proxy;
this.postDataCharset = request.postDataCharset;
this.timeoutMilliseconds = request.timeoutMilliseconds;
this.maxBodySizeBytes = request.maxBodySizeBytes;
this.followRedirects = request.followRedirects;
this.data = new ArrayList();
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.authenticator = request.authenticator;
this.responseProgress = request.responseProgress;
this.executing = false;
}
@Override
public Proxy proxy() {
return this.proxy;
}
@Override
public Request proxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
@Override
public Request proxy(String str, int i) {
this.proxy = new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(str, i));
return this;
}
@Override
public int timeout() {
return this.timeoutMilliseconds;
}
@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 int maxBodySize() {
return this.maxBodySizeBytes;
}
@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 boolean followRedirects() {
return this.followRedirects;
}
@Override
public Connection.Request followRedirects(boolean z) {
this.followRedirects = z;
return this;
}
@Override
public boolean ignoreHttpErrors() {
return this.ignoreHttpErrors;
}
@Override
public SSLSocketFactory sslSocketFactory() {
return this.sslSocketFactory;
}
@Override
public void sslSocketFactory(SSLSocketFactory sSLSocketFactory) {
this.sslSocketFactory = sSLSocketFactory;
}
@Override
public Connection.Request ignoreHttpErrors(boolean z) {
this.ignoreHttpErrors = z;
return this;
}
@Override
public boolean ignoreContentType() {
return this.ignoreContentType;
}
@Override
public Connection.Request ignoreContentType(boolean z) {
this.ignoreContentType = z;
return this;
}
@Override
public Request data(Connection.KeyVal keyVal) {
Validate.notNullParam(keyVal, "keyval");
this.data.add(keyVal);
return this;
}
@Override
public Collection<Connection.KeyVal> data() {
return this.data;
}
@Override
public Connection.Request requestBody(String str) {
this.body = str;
return this;
}
@Override
public String requestBody() {
return this.body;
}
@Override
public Request parser(Parser parser) {
this.parser = parser;
this.parserDefined = true;
return this;
}
@Override
public Parser parser() {
return this.parser;
}
@Override
public Connection.Request postDataCharset(String str) {
Validate.notNullParam(str, "charset");
if (!Charset.isSupported(str)) {
throw new IllegalCharsetNameException(str);
}
this.postDataCharset = str;
return this;
}
@Override
public String postDataCharset() {
return this.postDataCharset;
}
CookieManager cookieManager() {
return this.cookieManager;
}
@Override
public Connection.Request auth(RequestAuthenticator requestAuthenticator) {
this.authenticator = requestAuthenticator;
return this;
}
@Override
public RequestAuthenticator auth() {
return this.authenticator;
}
}
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("(\\w+)/\\w*\\+?xml.*");
private ControllableInputStream bodyStream;
private ByteBuffer byteData;
private String charset;
int contentLength;
String contentType;
private boolean executed;
RequestExecutor executor;
private boolean inputStreamRead;
private int numRedirects;
private final Request req;
int statusCode;
String statusMessage;
@Override
public Connection.Response addHeader(String str, String str2) {
return super.addHeader(str, str2);
}
@Override
public String cookie(String str) {
return super.cookie(str);
}
@Override
public Connection.Response cookie(String str, String str2) {
return super.cookie(str, str2);
}
@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 Connection.Response header(String str, String str2) {
return super.header(str, str2);
}
@Override
public List headers(String str) {
return super.headers(str);
}
@Override
public Map headers() {
return super.headers();
}
@Override
public Connection.Response method(Connection.Method method) {
return super.method(method);
}
@Override
public Connection.Method method() {
return super.method();
}
@Override
public Map multiHeaders() {
return super.multiHeaders();
}
@Override
public Connection.Response removeCookie(String str) {
return super.removeCookie(str);
}
@Override
public Connection.Response removeHeader(String str) {
return super.removeHeader(str);
}
@Override
public URL url() {
return super.url();
}
@Override
public Connection.Response url(URL url) {
return super.url(url);
}
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;
}
static Response execute(Request request) throws IOException {
return execute(request, null);
}
static org.jsoup.helper.HttpConnection.Response execute(org.jsoup.helper.HttpConnection.Request r8, org.jsoup.helper.HttpConnection.Response r9) throws java.io.IOException {
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 int statusCode() {
return this.statusCode;
}
@Override
public String statusMessage() {
return this.statusMessage;
}
@Override
public String charset() {
return this.charset;
}
@Override
public Response charset(String str) {
this.charset = str;
return this;
}
@Override
public String contentType() {
return this.contentType;
}
private ControllableInputStream prepareParse() {
Validate.isTrue(this.executed, "Request must be executed (with .execute(), .get(), or .post() before parsing response");
ControllableInputStream controllableInputStream = this.bodyStream;
if (this.byteData != null) {
controllableInputStream = ControllableInputStream.wrap(new ByteArrayInputStream(this.byteData.array(), 0, this.byteData.limit()), 0);
this.inputStreamRead = false;
}
Validate.isFalse(this.inputStreamRead, "Input stream already read and parsed, cannot re-read.");
Validate.notNull(controllableInputStream);
this.inputStreamRead = true;
return controllableInputStream;
}
@Override
public Document parse() throws IOException {
Document parseInputStream = DataUtil.parseInputStream(prepareParse(), this.charset, this.url.toExternalForm(), this.req.parser());
parseInputStream.connection(new HttpConnection(this.req, this));
this.charset = parseInputStream.outputSettings().charset().name();
safeClose();
return parseInputStream;
}
@Override
public StreamParser streamParser() throws IOException {
ControllableInputStream prepareParse = prepareParse();
String externalForm = this.url.toExternalForm();
DataUtil.CharsetDoc detectCharset = DataUtil.detectCharset(prepareParse, this.charset, externalForm, this.req.parser());
StreamParser streamParser = new StreamParser(this.req.parser());
streamParser.parse(new BufferedReader(new InputStreamReader(prepareParse, detectCharset.charset)), externalForm);
streamParser.document().connection(new HttpConnection(this.req, this));
this.charset = detectCharset.charset.name();
return streamParser;
}
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();
}
}
@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);
Validate.isTrue(this.byteData.hasArray());
byte[] array = this.byteData.array();
int arrayOffset = this.byteData.arrayOffset();
int limit = this.byteData.limit();
if (arrayOffset == 0 && limit == array.length) {
return array;
}
byte[] bArr = new byte[limit];
System.arraycopy(array, arrayOffset, bArr, 0, limit);
return bArr;
}
@Override
public Connection.Response bufferUp() {
prepareByteData();
return this;
}
@Override
public BufferedInputStream bodyStream() {
Validate.isTrue(this.executed, "Request must be executed (with .execute(), .get(), or .post() before getting response body");
if (this.byteData != null) {
return new BufferedInputStream(new ByteArrayInputStream(this.byteData.array(), 0, this.byteData.limit()), 8192);
}
Validate.isFalse(this.inputStreamRead, "Request has already been read");
Validate.notNull(this.bodyStream);
this.inputStreamRead = true;
return this.bodyStream.inputStream();
}
private void safeClose() {
ControllableInputStream controllableInputStream = this.bodyStream;
if (controllableInputStream != null) {
try {
controllableInputStream.close();
} catch (IOException unused) {
} catch (Throwable th) {
this.bodyStream = null;
throw th;
}
this.bodyStream = null;
}
RequestExecutor requestExecutor = this.executor;
if (requestExecutor != null) {
requestExecutor.safeClose();
}
}
Response(Request request) {
super();
this.executed = false;
this.inputStreamRead = false;
this.numRedirects = 0;
this.req = request;
}
void prepareResponse(Map<String, List<String>> map, Response response) throws IOException {
processResponseHeaders(map);
CookieUtil.storeCookies(this.req, this, this.url, map);
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()));
}
}
}
void processResponseHeaders(Map<String, List<String>> map) {
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
if (key != null) {
Iterator<String> it = entry.getValue().iterator();
while (it.hasNext()) {
addHeader(key, fixHeaderEncoding(it.next()));
}
}
}
}
private static String fixHeaderEncoding(String str) {
if (str == null) {
return str;
}
byte[] bytes = str.getBytes(HttpConnection.ISO_8859_1);
return looksLikeUtf8(bytes) ? new String(bytes, DataUtil.UTF_8) : str;
}
private static boolean looksLikeUtf8(byte[] bArr) {
int i;
int i2 = (bArr.length >= 3 && (bArr[0] & 255) == 239 && (bArr[1] & 255) == 187 && (bArr[2] & 255) == 191) ? 3 : 0;
int length = bArr.length;
boolean z = false;
while (i2 < length) {
byte b = bArr[i2];
if ((b & 128) != 0) {
if ((b & 224) == 192) {
i = i2 + 1;
} else if ((b & 240) == 224) {
i = i2 + 2;
} else {
if ((b & 248) != 240) {
return false;
}
i = i2 + 3;
}
if (i >= bArr.length) {
return false;
}
while (i2 < i) {
i2++;
if ((bArr[i2] & 192) != 128) {
return false;
}
}
z = true;
}
i2++;
}
return z;
}
private static void setOutputContentType(Request request) {
String mimeBoundary;
String header = request.header(HttpConnection.CONTENT_TYPE);
if (header == null) {
if (HttpConnection.needsMultipart(request)) {
mimeBoundary = DataUtil.mimeBoundary();
request.header(HttpConnection.CONTENT_TYPE, "multipart/form-data; boundary=" + mimeBoundary);
} else {
request.header(HttpConnection.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=" + request.postDataCharset());
mimeBoundary = null;
}
} else {
if (header.contains(HttpConnection.MULTIPART_FORM_DATA) && !header.contains("boundary")) {
mimeBoundary = DataUtil.mimeBoundary();
request.header(HttpConnection.CONTENT_TYPE, "multipart/form-data; boundary=" + mimeBoundary);
}
mimeBoundary = null;
}
request.mimeBoundary = mimeBoundary;
}
static void writePost(Request request, OutputStream outputStream) throws IOException {
Collection<Connection.KeyVal> data = request.data();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, Charset.forName(request.postDataCharset())));
String str = request.mimeBoundary;
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();
}
private static void serialiseRequestUrl(Connection.Request request) throws IOException {
UrlBuilder urlBuilder = new UrlBuilder(request.url());
for (Connection.KeyVal keyVal : request.data()) {
Validate.isFalse(keyVal.hasInputStream(), "InputStream data not supported in URL query string.");
urlBuilder.appendKeyVal(keyVal);
}
request.url(urlBuilder.build());
request.data().clear();
}
}
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 class KeyVal implements Connection.KeyVal {
private String contentType;
private String key;
private InputStream stream;
private String value;
public static KeyVal create(String str, String str2) {
return new KeyVal(str, str2);
}
public static KeyVal create(String str, String str2, InputStream inputStream) {
return new KeyVal(str, str2).inputStream(inputStream);
}
private KeyVal(String str, String str2) {
Validate.notEmptyParam(str, "key");
Validate.notNullParam(str2, ES6Iterator.VALUE_PROPERTY);
this.key = str;
this.value = str2;
}
@Override
public KeyVal key(String str) {
Validate.notEmptyParam(str, "key");
this.key = str;
return this;
}
@Override
public String key() {
return this.key;
}
@Override
public KeyVal value(String str) {
Validate.notNullParam(str, ES6Iterator.VALUE_PROPERTY);
this.value = str;
return this;
}
@Override
public String value() {
return this.value;
}
@Override
public KeyVal inputStream(InputStream inputStream) {
Validate.notNullParam(this.value, "inputStream");
this.stream = inputStream;
return this;
}
@Override
public InputStream inputStream() {
return this.stream;
}
@Override
public boolean hasInputStream() {
return this.stream != null;
}
@Override
public Connection.KeyVal contentType(String str) {
Validate.notEmpty(str);
this.contentType = str;
return this;
}
@Override
public String contentType() {
return this.contentType;
}
public String toString() {
return this.key + "=" + this.value;
}
}
}