Retain cookies for domain.

This commit is contained in:
vlofgren 2022-08-18 20:44:44 +02:00
parent a1eb8375a2
commit ede62f2515

View File

@ -9,21 +9,22 @@ import java.util.List;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class Cookies { public class Cookies {
final ThreadLocal<ConcurrentHashMap<HttpUrl, List<Cookie>>> cookieJar = ThreadLocal.withInitial(ConcurrentHashMap::new); final ThreadLocal<ConcurrentHashMap<String, List<Cookie>>> cookieJar = ThreadLocal.withInitial(ConcurrentHashMap::new);
public CookieJar getJar() { public CookieJar getJar() {
return new CookieJar() { return new CookieJar() {
@Override @Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
if (!cookies.isEmpty()) { if (!cookies.isEmpty()) {
cookieJar.get().put(url, cookies); cookieJar.get().put(url.host(), cookies);
} }
} }
@Override @Override
public List<Cookie> loadForRequest(HttpUrl url) { public List<Cookie> loadForRequest(HttpUrl url) {
return cookieJar.get().getOrDefault(url, Collections.emptyList()); return cookieJar.get().getOrDefault(url.host(), Collections.emptyList());
} }
}; };
} }