Fix domain error in CookieStore
This commit is contained in:
1 parent
24a64c23dc
commit
4e651066fb
3 files changed
+75
-96
No files matched your search
@@ -35,20 +35,23 @@ public class CookieDBStore implements CookieJar {
|
||||
map = CookieDB.getAllCookies();
|
||||
}
|
||||
|
||||
private boolean hasExpired(Cookie cookie) {
|
||||
private static boolean hasExpired(Cookie cookie) {
|
||||
return cookie.expiresAt() < System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private static CookieWithID removeCookie(List<CookieWithID> list, Cookie cookie) {
|
||||
for (int i = 0, n = list.size(); i < n; i++) {
|
||||
CookieWithID cwi = list.get(i);
|
||||
if (cwi.cookie.name().equals(cookie.name())) {
|
||||
list.remove(i);
|
||||
return cwi;
|
||||
}
|
||||
public boolean domainMatch(Cookie cookie, String urlHost) {
|
||||
return cookie.hostOnly()
|
||||
? urlHost.equals(cookie.domain())
|
||||
: domainMatch(urlHost, cookie.domain());
|
||||
}
|
||||
|
||||
private static boolean domainMatch(String urlHost, String cookieDomain) {
|
||||
if (urlHost.equals(cookieDomain)) {
|
||||
return true; // As in 'example.com' matching 'example.com'.
|
||||
}
|
||||
|
||||
return null;
|
||||
return urlHost.endsWith(cookieDomain)
|
||||
&& urlHost.charAt(urlHost.length() - cookieDomain.length() - 1) == '.';
|
||||
}
|
||||
|
||||
private static CookieWithID removeCookie(List<CookieWithID> list, String name) {
|
||||
@@ -63,12 +66,22 @@ public class CookieDBStore implements CookieJar {
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean containInternal(String domain, String name) {
|
||||
List<CookieWithID> cookies = map.get(domain);
|
||||
if (cookies != null) {
|
||||
for (int i = 0, n = cookies.size(); i < n; i++) {
|
||||
CookieWithID cwi = cookies.get(i);
|
||||
if (cwi.cookie.name().equals(name)) {
|
||||
private boolean containInternal(String host, String name, List<CookieWithID> cookies) {
|
||||
for (int i = 0, n = cookies.size(); i < n; i++) {
|
||||
Cookie cookie = cookies.get(i).cookie;
|
||||
if (domainMatch(cookie, host) && cookie.name().equals(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contain(String host, String name) {
|
||||
for (String key : map.keySet()) {
|
||||
if (domainMatch(host, key)) {
|
||||
List<CookieWithID> cookies = map.get(key);
|
||||
// cookies can not be null
|
||||
if (containInternal(host, name, cookies)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -76,11 +89,12 @@ public class CookieDBStore implements CookieJar {
|
||||
return false;
|
||||
}
|
||||
|
||||
private synchronized void removeInternal(String domain, Cookie cookie) {
|
||||
private synchronized void removeInternal(Cookie cookie) {
|
||||
String domain = cookie.domain();
|
||||
List<CookieWithID> cookies = map.get(domain);
|
||||
if (cookies != null) {
|
||||
// Remove from list
|
||||
CookieWithID cwi = removeCookie(cookies, cookie);
|
||||
CookieWithID cwi = removeCookie(cookies, cookie.name());
|
||||
if (cwi != null) {
|
||||
// Remove from DB
|
||||
CookieDB.removeCookie(cwi.id);
|
||||
@@ -88,34 +102,37 @@ public class CookieDBStore implements CookieJar {
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void removeInternal(String domain, String name) {
|
||||
List<CookieWithID> cookies = map.get(domain);
|
||||
if (cookies != null) {
|
||||
// Remove from list
|
||||
CookieWithID cwi = removeCookie(cookies, name);
|
||||
if (cwi != null) {
|
||||
private void removeInternal(String host, List<CookieWithID> cookies) {
|
||||
for (Iterator<CookieWithID> i = cookies.iterator(); i.hasNext();) {
|
||||
CookieWithID cwi = i.next();
|
||||
Cookie cookie = cwi.cookie;
|
||||
// Check match host or has expired
|
||||
if (domainMatch(cookie, host) || hasExpired(cookie)) {
|
||||
// Remove from list
|
||||
i.remove();
|
||||
// Remove from DB
|
||||
CookieDB.removeCookie(cwi.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void removeInternal(String domain) {
|
||||
// Remove all cookies of the domain from map
|
||||
List<CookieWithID> cookies = map.remove(domain);
|
||||
if (cookies != null) {
|
||||
for (CookieWithID cwi : cookies) {
|
||||
// Remove from DB
|
||||
CookieDB.removeCookie(cwi.id);
|
||||
public void remove(String host) {
|
||||
for (String key : map.keySet()) {
|
||||
if (domainMatch(host, key)) {
|
||||
List<CookieWithID> cookies = map.get(key);
|
||||
// cookies can not be null
|
||||
removeInternal(host, cookies);
|
||||
}
|
||||
cookies.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void addInternal(String domain, Cookie cookie) {
|
||||
|
||||
public void add(Cookie cookie) {
|
||||
String domain = cookie.domain();
|
||||
|
||||
// Check expired
|
||||
if (hasExpired(cookie)) {
|
||||
removeInternal(domain, cookie);
|
||||
removeInternal(cookie);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -124,8 +141,9 @@ public class CookieDBStore implements CookieJar {
|
||||
cookies = new ArrayList<>();
|
||||
map.put(domain, cookies);
|
||||
} else {
|
||||
// Remove old cookie
|
||||
// Remove from list
|
||||
CookieWithID cwi = removeCookie(cookies, cookie);
|
||||
CookieWithID cwi = removeCookie(cookies, cookie.name());
|
||||
if (cwi != null) {
|
||||
// Remove from DB
|
||||
CookieDB.removeCookie(cwi.id);
|
||||
@@ -138,40 +156,31 @@ public class CookieDBStore implements CookieJar {
|
||||
cookies.add(new CookieWithID(id, cookie));
|
||||
}
|
||||
|
||||
public boolean contain(String url, String name) {
|
||||
return containInternal(CookieDB.cookiesDomain(url), name);
|
||||
}
|
||||
|
||||
public void remove(String url, String name) {
|
||||
removeInternal(CookieDB.cookiesDomain(url), name);
|
||||
}
|
||||
|
||||
public void remove(String url) {
|
||||
removeInternal(CookieDB.cookiesDomain(url));
|
||||
}
|
||||
|
||||
public void add(Cookie cookie) {
|
||||
addInternal(CookieDB.cookiesDomain(cookie.domain()), cookie);
|
||||
private void getInternal(HttpUrl url, List<CookieWithID> cookies, List<Cookie> result) {
|
||||
for (Iterator<CookieWithID> i = cookies.iterator(); i.hasNext();) {
|
||||
CookieWithID cwi = i.next();
|
||||
Cookie cookie = cwi.cookie;
|
||||
// Check expired
|
||||
if (hasExpired(cookie)) {
|
||||
// Remove from list
|
||||
i.remove();
|
||||
// Remove from DB
|
||||
CookieDB.removeCookie(cwi.id);
|
||||
} else if (cookie.matches(url)) {
|
||||
result.add(cookie);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<Cookie> get(HttpUrl url) {
|
||||
List<Cookie> result = new ArrayList<>();
|
||||
|
||||
String domain = CookieDB.cookiesDomain(url.url());
|
||||
List<CookieWithID> cookies = map.get(domain);
|
||||
if (cookies != null) {
|
||||
for (Iterator<CookieWithID> i = cookies.iterator(); i.hasNext();) {
|
||||
CookieWithID cwi = i.next();
|
||||
Cookie cookie = cwi.cookie;
|
||||
// Check expired
|
||||
if (hasExpired(cookie)) {
|
||||
// Remove from list
|
||||
i.remove();
|
||||
// Remove from DB
|
||||
CookieDB.removeCookie(cwi.id);
|
||||
} else if (cookie.matches(url)) {
|
||||
result.add(cookie);
|
||||
}
|
||||
String host = url.host();
|
||||
for (String key : map.keySet()) {
|
||||
if (domainMatch(host, key)) {
|
||||
List<CookieWithID> cookies = map.get(key);
|
||||
// cookies can not be null
|
||||
getInternal(url, cookies, result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user