what-the-bank/sources/okhttp3/repackaged/JavaNetCookieJar.java

96 lines
3.8 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.repackaged;
import com.google.common.net.HttpHeaders;
import java.io.IOException;
import java.net.CookieHandler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import okhttp3.repackaged.Cookie;
import okhttp3.repackaged.internal.Internal;
import okhttp3.repackaged.internal.Util;
/* loaded from: classes6.dex */
public final class JavaNetCookieJar implements CookieJar {
private final CookieHandler ahT;
public JavaNetCookieJar(CookieHandler cookieHandler) {
this.ahT = cookieHandler;
}
@Override // okhttp3.repackaged.CookieJar
public final void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
if (this.ahT != null) {
ArrayList arrayList = new ArrayList();
Iterator<Cookie> it = list.iterator();
while (it.hasNext()) {
arrayList.add(it.next().toString());
}
try {
this.ahT.put(httpUrl.uri(), Collections.singletonMap(HttpHeaders.SET_COOKIE, arrayList));
} catch (IOException e) {
Logger logger = Internal.logger;
Level level = Level.WARNING;
StringBuilder sb = new StringBuilder("Saving cookies failed for ");
sb.append(httpUrl.resolve("/..."));
logger.log(level, sb.toString(), (Throwable) e);
}
}
}
@Override // okhttp3.repackaged.CookieJar
public final List<Cookie> loadForRequest(HttpUrl httpUrl) {
try {
ArrayList arrayList = null;
for (Map.Entry<String, List<String>> entry : this.ahT.get(httpUrl.uri(), Collections.emptyMap()).entrySet()) {
String key = entry.getKey();
if (HttpHeaders.COOKIE.equalsIgnoreCase(key) || "Cookie2".equalsIgnoreCase(key)) {
if (!entry.getValue().isEmpty()) {
for (String str : entry.getValue()) {
if (arrayList == null) {
arrayList = new ArrayList();
}
arrayList.addAll(d(httpUrl, str));
}
}
}
}
if (arrayList != null) {
return Collections.unmodifiableList(arrayList);
}
return Collections.emptyList();
} catch (IOException e) {
Logger logger = Internal.logger;
Level level = Level.WARNING;
StringBuilder sb = new StringBuilder("Loading cookies failed for ");
sb.append(httpUrl.resolve("/..."));
logger.log(level, sb.toString(), (Throwable) e);
return Collections.emptyList();
}
}
private List<Cookie> d(HttpUrl httpUrl, String str) {
ArrayList arrayList = new ArrayList();
int length = str.length();
int i = 0;
while (i < length) {
int delimiterOffset = Util.delimiterOffset(str, i, length, ";,");
int delimiterOffset2 = Util.delimiterOffset(str, i, delimiterOffset, '=');
String trimSubstring = Util.trimSubstring(str, i, delimiterOffset2);
if (!trimSubstring.startsWith("$")) {
String trimSubstring2 = delimiterOffset2 < delimiterOffset ? Util.trimSubstring(str, delimiterOffset2 + 1, delimiterOffset) : "";
if (trimSubstring2.startsWith("\"") && trimSubstring2.endsWith("\"")) {
trimSubstring2 = trimSubstring2.substring(1, trimSubstring2.length() - 1);
}
arrayList.add(new Cookie.Builder().name(trimSubstring).value(trimSubstring2).domain(httpUrl.host()).build());
}
i = delimiterOffset + 1;
}
return arrayList;
}
}