Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,43 @@ public void setup() {
public List<Cookie> get() {
return store.get(requestUri);
}

/**
* A request whose host has no cookie: either nothing was ever stored, the common case for an API
* client on the default store, or the jar holds only another host's cookies. Plain {@code http},
* so a lookup that resolves the secure context parses the host as an address.
*/
@State(Scope.Thread)
public static class NoMatch {
@Param({"api.eu.service.example.org", "192.0.2.10"})
public String host;

private final ThreadSafeCookieStore emptyStore = new ThreadSafeCookieStore();
private final ThreadSafeCookieStore populatedStore = new ThreadSafeCookieStore();
private Uri requestUri;

@Setup(Level.Trial)
public void setup() {
Uri other = Uri.create("http://www.example.com/");
for (int i = 0; i < 5; i++) {
populatedStore.add(other, new DefaultCookie("cookie" + i, "value" + i));
}
DefaultCookie parent = new DefaultCookie("parent", "v");
parent.setDomain("example.com");
populatedStore.add(other, parent);
populatedStore.add(Uri.create("http://198.51.100.7/"), new DefaultCookie("ip", "v"));

requestUri = Uri.create("http://" + host + "/v1/items");
}
}

@Benchmark
public List<Cookie> emptyJar(NoMatch state) {
return state.emptyStore.get(state.requestUri);
}

@Benchmark
public List<Cookie> unrelatedHost(NoMatch state) {
return state.populatedStore.get(state.requestUri);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ public void add(Uri uri, Cookie cookie) {

@Override
public List<Cookie> get(Uri uri) {
String domain = requestDomain(uri);
return get(domain, requestPath(uri), uri.isSecured(), isSecureContext(uri, domain));
// Most clients never receive a cookie; an empty jar cannot match, so skip the host work.
if (cookieJar.isEmpty()) {
return Collections.emptyList();
}
return get(uri, requestDomain(uri), requestPath(uri));
}

@Override
Expand Down Expand Up @@ -404,7 +407,12 @@ && pathsMatch(storedKey.path, newKey.path)
return false;
}

private List<Cookie> get(String domain, String path, boolean tls, boolean secure) {
private List<Cookie> get(Uri uri, String domain, String path) {
boolean tls = uri.isSecured();
// Resolved at the first bucket found: on plaintext it parses the host as an address, and a host
// with no bucket in its domain chain never needs it.
boolean secure = false;
boolean secureResolved = false;
boolean exactDomainMatch = true;
String subDomain = domain;
List<Map.Entry<CookieKey, StoredCookie>> results = null;
Expand All @@ -413,17 +421,21 @@ private List<Cookie> get(String domain, String path, boolean tls, boolean secure
boolean walkParents = !isIpAddressLiteral(domain);

while (MiscUtils.isNonEmpty(subDomain)) {
// Lazily allocate a single result list and append matches straight into it; an imperative
// scan avoids the per-sub-domain-level Stream pipeline (filter/map stages, two capturing
// lambdas, a spliterator and the Collectors.toList intermediate list) that this ran on every
// cookie-enabled request.
if (results == null) {
results = new ArrayList<>(4);
}
int from = results.size();
collectStoredCookies(subDomain, path, tls, secure, exactDomainMatch, results);
if (results.size() - from > 1) {
results.subList(from, results.size()).sort(LONGER_PATH_FIRST);
Map<CookieKey, StoredCookie> bucket = cookieJar.get(subDomain);
if (bucket != null) {
if (!secureResolved) {
secure = isSecureContext(uri, domain);
secureResolved = true;
}
// Lazily allocate a single result list and append matches straight into it; an imperative
// scan avoids the per-sub-domain-level Stream pipeline (filter/map stages, two capturing
// lambdas, a spliterator and the Collectors.toList intermediate list) that this ran on every
// cookie-enabled request.
int from = results == null ? 0 : results.size();
results = collectStoredCookies(bucket, path, tls, secure, exactDomainMatch, results);
if (results != null && results.size() - from > 1) {
results.subList(from, results.size()).sort(LONGER_PATH_FIRST);
}
}
if (!walkParents) {
break;
Expand All @@ -432,7 +444,7 @@ private List<Cookie> get(String domain, String path, boolean tls, boolean secure
exactDomainMatch = false;
}

if (results == null || results.isEmpty()) {
if (results == null) {
return Collections.emptyList();
}
if (results.size() == 1) {
Expand Down Expand Up @@ -464,23 +476,23 @@ private List<Cookie> get(String domain, String path, boolean tls, boolean secure
.reversed()
.thenComparingLong(entry -> entry.getValue().seq);

private void collectStoredCookies(String domain, String path, boolean tls, boolean secure, boolean isExactMatch,
List<Map.Entry<CookieKey, StoredCookie>> out) {
final Map<CookieKey, StoredCookie> innerMap = cookieJar.get(domain);
if (innerMap == null) {
return;
}

private @Nullable List<Map.Entry<CookieKey, StoredCookie>> collectStoredCookies(
Map<CookieKey, StoredCookie> innerMap, String path, boolean tls, boolean secure, boolean isExactMatch,
@Nullable List<Map.Entry<CookieKey, StoredCookie>> out) {
for (Map.Entry<CookieKey, StoredCookie> entry : innerMap.entrySet()) {
CookieKey key = entry.getKey();
StoredCookie storedCookie = entry.getValue();
if (!hasCookieExpired(storedCookie.cookie, storedCookie.createdAt)
&& (isExactMatch || !storedCookie.hostOnly)
&& pathsMatch(key.path, path)
&& (tls || !storedCookie.cookie.isSecure() || (secure && !storedCookie.overTls))) {
if (out == null) {
out = new ArrayList<>(4);
}
out.add(entry);
}
}
return out;
}

private void removeExpired() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,58 @@ public void returnsEmptyForUnknownDomain() {
List<Cookie> result = store.get(Uri.create("http://www.bar.com/"));
assertTrue(result.isEmpty(), "no cookies should match an unrelated domain");
}

private static final String[] UNRELATED_HOSTS = {
"http://www.bar.com/", "https://www.bar.com/",
"http://api.eu.service.bar.org/x", "https://api.eu.service.bar.org/x",
"http://192.0.2.10/", "https://192.0.2.10/",
"http://localhost/", "http://127.0.0.1/",
};

@Test
public void anEmptyJarReturnsNothingForAnyHost() {
ThreadSafeCookieStore store = new ThreadSafeCookieStore();
for (String url : UNRELATED_HOSTS) {
assertTrue(store.get(Uri.create(url)).isEmpty(), url);
}
}

@Test
public void aClearedJarReturnsNothing() {
ThreadSafeCookieStore store = new ThreadSafeCookieStore();
Uri uri = Uri.create("http://www.foo.com/");
store.add(uri, ClientCookieDecoder.LAX.decode("ALPHA=VALUE1; Path=/"));
store.clear();

assertTrue(store.get(uri).isEmpty());
}

@Test
public void aPopulatedJarReturnsNothingForAnUnrelatedHost() {
ThreadSafeCookieStore store = new ThreadSafeCookieStore();
store.add(Uri.create("https://www.foo.com/"), ClientCookieDecoder.LAX.decode("TLS=V; Secure; Path=/"));
store.add(Uri.create("http://www.foo.com/"), ClientCookieDecoder.LAX.decode("HOST=V; Path=/"));
store.add(Uri.create("http://www.foo.com/"), ClientCookieDecoder.LAX.decode("PARENT=V; Domain=foo.com; Path=/"));
store.add(Uri.create("http://198.51.100.7/"), ClientCookieDecoder.LAX.decode("IP=V; Path=/"));

for (String url : UNRELATED_HOSTS) {
assertTrue(store.get(Uri.create(url)).isEmpty(), url);
}
assertEquals(setOf("TLS=V", "HOST=V", "PARENT=V"), namesValues(store.get(Uri.create("https://www.foo.com/"))));
assertEquals(setOf("HOST=V", "PARENT=V"), namesValues(store.get(Uri.create("http://www.foo.com/"))));
assertEquals(setOf("PARENT=V"), namesValues(store.get(Uri.create("http://api.eu.foo.com/"))));
assertEquals(setOf("IP=V"), namesValues(store.get(Uri.create("http://198.51.100.7/"))));
}

@Test
public void aBucketWithNoMatchReturnsNothing() {
ThreadSafeCookieStore store = new ThreadSafeCookieStore();
store.add(Uri.create("https://www.foo.com/"), ClientCookieDecoder.LAX.decode("TLS=V; Secure; Path=/"));
store.add(Uri.create("http://www.foo.com/"), ClientCookieDecoder.LAX.decode("DEEP=V; Path=/deep"));
store.add(Uri.create("http://www.foo.com/"), ClientCookieDecoder.LAX.decode("HOST=V; Path=/"));

assertEquals(setOf("DEEP=V", "HOST=V"), namesValues(store.get(Uri.create("http://www.foo.com/deep"))));
assertEquals(setOf("HOST=V"), namesValues(store.get(Uri.create("http://www.foo.com/x"))));
assertTrue(store.get(Uri.create("http://sub.www.foo.com/deep")).isEmpty(), "host-only cookies only");
}
}
Loading