Skip to content
Merged
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2025 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -86,20 +86,29 @@ public static Map<String, Cookie> parseCookies(String header) {
}

/**
* Check if a cookie with identical name had been parsed.
* If yes, the one with the longest string will be kept
* Check if a cookie with similar names had been parsed.
* If yes, the one with the longest path will be kept
* For similar paths the newest is stored
* @param cookies : Map of cookies
* @param cookie : Cookie to be checked
*/
private static void checkSimilarCookieName(Map<String, Cookie> cookies, MutableCookie cookie) {
if (cookie != null) {
if (cookies.containsKey(cookie.name)){
if (cookie.value.length() > cookies.get(cookie.name).getValue().length()){
cookies.put(cookie.name, cookie.getImmutableCookie());
}
} else {
cookies.put(cookie.name, cookie.getImmutableCookie());
}
if (cookie == null) {
return;
}

boolean alreadyPresent = cookies.containsKey(cookie.name);
boolean recordCookie = !alreadyPresent;

if (alreadyPresent) {
final String newPath = cookie.path == null ? "" : cookie.path;
final String existingPath = cookies.get(cookie.name).getPath() == null ? ""
: cookies.get(cookie.name).getPath();
recordCookie = (newPath.length() >= existingPath.length());
}

if (recordCookie) {
cookies.put(cookie.name, cookie.getImmutableCookie());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2025 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -151,27 +151,46 @@ public void testCreateCookies() {
@Test
public void testMultipleCookiesWithSameName(){

String cookieHeader = "kobe=longeststring; kobe=shortstring";
String cookieHeader = "kobe=oldeststring; kobe=neweststring";
Map<String, Cookie> cookies = HttpHeaderReader.readCookies(cookieHeader);
assertEquals(cookies.size(), 1);
Cookie c = cookies.get("kobe");
assertEquals(c.getVersion(), 0);
assertEquals("kobe", c.getName());
assertEquals("longeststring", c.getValue());
assertEquals("neweststring", c.getValue());

cookieHeader = "bryant=longeststring; bryant=shortstring; fred=shortstring ;fred=longeststring;$Path=/path";
cookieHeader = "bryant=longeststring; bryant=neweststring; fred=oldeststring ;fred=neweststring;$Path=/path";
cookies = HttpHeaderReader.readCookies(cookieHeader);
assertEquals(cookies.size(), 2);
c = cookies.get("bryant");
assertEquals(c.getVersion(), 0);
assertEquals("bryant", c.getName());
assertEquals("longeststring", c.getValue());
assertEquals("neweststring", c.getValue());
c = cookies.get("fred");
assertEquals(c.getVersion(), 0);
assertEquals("fred", c.getName());
assertEquals("longeststring", c.getValue());
assertEquals("neweststring", c.getValue());
assertEquals("/path", c.getPath());

cookieHeader = "cookiewithpath=longeststring;$Path=/path; cookiewithpath=string1;$Path=/path;"
+ " cookiewithpath=string2;$Path=/path ;cookiewithpath=string3;$Path=/path";
cookies = HttpHeaderReader.readCookies(cookieHeader);
assertEquals(cookies.size(), 1);
c = cookies.get("cookiewithpath");
assertEquals(c.getVersion(), 0);
assertEquals("cookiewithpath", c.getName());
assertEquals("string3", c.getValue());

cookieHeader = "cookiewithpath=longeststring;$Path=/path/added/path; cookiewithpath=string1;$Path=/path;"
+ " cookiewithpath=string2;$Path=/path ;cookiewithpath=string3;$Path=/path";
cookies = HttpHeaderReader.readCookies(cookieHeader);
assertEquals(cookies.size(), 1);
c = cookies.get("cookiewithpath");
assertEquals(c.getVersion(), 0);
assertEquals("cookiewithpath", c.getName());
assertEquals("longeststring", c.getValue());
assertEquals("/path/added/path", c.getPath());

}

@Test
Expand Down
Loading