mirror of
https://github.com/internetarchive/heritrix3.git
synced 2026-09-21 05:05:44 +00:00
Restrict number of cookies per domain to 50
This commit is contained in:
@@ -26,6 +26,8 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.SortedMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.apache.commons.collections.collection.CompositeCollection;
|
||||
import org.apache.http.client.CookieStore;
|
||||
@@ -59,6 +61,10 @@ import com.sleepycat.je.DatabaseException;
|
||||
public class BdbCookieStore extends AbstractCookieStore implements
|
||||
FetchHTTPCookieStore, CookieStore {
|
||||
|
||||
public static final int MAX_COOKIES_FOR_DOMAIN = 50;
|
||||
|
||||
private static Logger logger = Logger.getLogger(BdbCookieStore.class.getName());
|
||||
|
||||
/**
|
||||
* A {@link List} implementation that wraps a {@link Collection}. Needed
|
||||
* because httpclient requires {@code List<Cookie>}.
|
||||
@@ -130,6 +136,17 @@ public class BdbCookieStore extends AbstractCookieStore implements
|
||||
}
|
||||
|
||||
public void addCookie(Cookie cookie) {
|
||||
|
||||
if (isCookieCountMaxedForDomain(cookie.getDomain())) {
|
||||
logger.log(
|
||||
Level.FINEST,
|
||||
"Maximum number of cookies reached for domain "
|
||||
+ cookie.getDomain() + ". Will not add new cookie "
|
||||
+ cookie.getName() + " with value "
|
||||
+ cookie.getValue());
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] key;
|
||||
try {
|
||||
key = sortableKey(cookie).getBytes("UTF-8");
|
||||
@@ -143,6 +160,12 @@ public class BdbCookieStore extends AbstractCookieStore implements
|
||||
cookies.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean isCookieCountMaxedForDomain(String domain) {
|
||||
Collection<Cookie> subset = hostSubset(normalizeHost(domain));
|
||||
|
||||
return (subset != null && subset.size() >= MAX_COOKIES_FOR_DOMAIN);
|
||||
}
|
||||
|
||||
protected Collection<Cookie> hostSubset(String host) {
|
||||
try {
|
||||
|
||||
@@ -163,6 +163,25 @@ public class CookieStoreTest extends TmpDirTestCase {
|
||||
basicCookieStore().addCookie(cookie);
|
||||
assertCookieStoresEquivalent(basicCookieStore(), bdbCookieStore());
|
||||
}
|
||||
|
||||
public void testMaxCookieDomain() throws IOException {
|
||||
bdbCookieStore().clear();
|
||||
|
||||
for (int i = 1; i <= BdbCookieStore.MAX_COOKIES_FOR_DOMAIN; ++i) {
|
||||
BasicClientCookie cookie = new BasicClientCookie("name" + i, "value" + i);
|
||||
bdbCookieStore().addCookie(cookie);
|
||||
|
||||
assertCookieStoreCountEquals(bdbCookieStore, i);
|
||||
}
|
||||
|
||||
BasicClientCookie cookie = new BasicClientCookie("nametoomany1", "valuetoomany1");
|
||||
bdbCookieStore().addCookie(cookie);
|
||||
assertCookieStoreCountEquals(bdbCookieStore, BdbCookieStore.MAX_COOKIES_FOR_DOMAIN);
|
||||
|
||||
cookie = new BasicClientCookie("nametoomany2", "valuetoomany2");
|
||||
bdbCookieStore().addCookie(cookie);
|
||||
assertCookieStoreCountEquals(bdbCookieStore, BdbCookieStore.MAX_COOKIES_FOR_DOMAIN);
|
||||
}
|
||||
|
||||
public void testPaths() throws IOException {
|
||||
bdbCookieStore().clear();
|
||||
@@ -318,6 +337,10 @@ public class CookieStoreTest extends TmpDirTestCase {
|
||||
assertCookieListsEquivalent(bdbCookieList, basicCookieStore().getCookies());
|
||||
}
|
||||
|
||||
protected void assertCookieStoreCountEquals(BdbCookieStore bdb, int count) {
|
||||
assertEquals(bdb.getCookies().size(), count);
|
||||
}
|
||||
|
||||
protected void assertCookieListsEquivalent(List<Cookie> list1,
|
||||
List<Cookie> list2) {
|
||||
Comparator<Cookie> comparator = new Comparator<Cookie>() {
|
||||
|
||||
Reference in New Issue
Block a user