Skip to content

Commit ae07260

Browse files
committed
Compare cookies by paths
Signed-off-by: Maxim Nesen <[email protected]>
1 parent f9dd572 commit ae07260

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

core-common/src/main/java/org/glassfish/jersey/message/internal/CookiesParser.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -86,20 +86,29 @@ public static Map<String, Cookie> parseCookies(String header) {
8686
}
8787

8888
/**
89-
* Check if a cookie with identical name had been parsed.
90-
* If yes, the one with the longest string will be kept
89+
* Check if a cookie with similar names had been parsed.
90+
* If yes, the one with the longest path will be kept
91+
* For similar paths the newest is stored
9192
* @param cookies : Map of cookies
9293
* @param cookie : Cookie to be checked
9394
*/
9495
private static void checkSimilarCookieName(Map<String, Cookie> cookies, MutableCookie cookie) {
95-
if (cookie != null) {
96-
if (cookies.containsKey(cookie.name)){
97-
if (cookie.value.length() > cookies.get(cookie.name).getValue().length()){
98-
cookies.put(cookie.name, cookie.getImmutableCookie());
99-
}
100-
} else {
101-
cookies.put(cookie.name, cookie.getImmutableCookie());
102-
}
96+
if (cookie == null) {
97+
return;
98+
}
99+
100+
boolean alreadyPresent = cookies.containsKey(cookie.name);
101+
boolean recordCookie = !alreadyPresent;
102+
103+
if (alreadyPresent) {
104+
final String newPath = cookie.path == null ? "" : cookie.path;
105+
final String existingPath = cookies.get(cookie.name).getPath() == null ? ""
106+
: cookies.get(cookie.name).getPath();
107+
recordCookie = (newPath.length() >= existingPath.length());
108+
}
109+
110+
if (recordCookie) {
111+
cookies.put(cookie.name, cookie.getImmutableCookie());
103112
}
104113
}
105114

tests/e2e/src/test/java/org/glassfish/jersey/tests/api/CookieImplTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -151,27 +151,46 @@ public void testCreateCookies() {
151151
@Test
152152
public void testMultipleCookiesWithSameName(){
153153

154-
String cookieHeader = "kobe=longeststring; kobe=shortstring";
154+
String cookieHeader = "kobe=oldeststring; kobe=neweststring";
155155
Map<String, Cookie> cookies = HttpHeaderReader.readCookies(cookieHeader);
156156
assertEquals(cookies.size(), 1);
157157
Cookie c = cookies.get("kobe");
158158
assertEquals(c.getVersion(), 0);
159159
assertEquals("kobe", c.getName());
160-
assertEquals("longeststring", c.getValue());
160+
assertEquals("neweststring", c.getValue());
161161

162-
cookieHeader = "bryant=longeststring; bryant=shortstring; fred=shortstring ;fred=longeststring;$Path=/path";
162+
cookieHeader = "bryant=longeststring; bryant=neweststring; fred=oldeststring ;fred=neweststring;$Path=/path";
163163
cookies = HttpHeaderReader.readCookies(cookieHeader);
164164
assertEquals(cookies.size(), 2);
165165
c = cookies.get("bryant");
166166
assertEquals(c.getVersion(), 0);
167167
assertEquals("bryant", c.getName());
168-
assertEquals("longeststring", c.getValue());
168+
assertEquals("neweststring", c.getValue());
169169
c = cookies.get("fred");
170170
assertEquals(c.getVersion(), 0);
171171
assertEquals("fred", c.getName());
172-
assertEquals("longeststring", c.getValue());
172+
assertEquals("neweststring", c.getValue());
173173
assertEquals("/path", c.getPath());
174174

175+
cookieHeader = "cookiewithpath=longeststring;$Path=/path; cookiewithpath=string1;$Path=/path;"
176+
+ " cookiewithpath=string2;$Path=/path ;cookiewithpath=string3;$Path=/path";
177+
cookies = HttpHeaderReader.readCookies(cookieHeader);
178+
assertEquals(cookies.size(), 1);
179+
c = cookies.get("cookiewithpath");
180+
assertEquals(c.getVersion(), 0);
181+
assertEquals("cookiewithpath", c.getName());
182+
assertEquals("string3", c.getValue());
183+
184+
cookieHeader = "cookiewithpath=longeststring;$Path=/path/added/path; cookiewithpath=string1;$Path=/path;"
185+
+ " cookiewithpath=string2;$Path=/path ;cookiewithpath=string3;$Path=/path";
186+
cookies = HttpHeaderReader.readCookies(cookieHeader);
187+
assertEquals(cookies.size(), 1);
188+
c = cookies.get("cookiewithpath");
189+
assertEquals(c.getVersion(), 0);
190+
assertEquals("cookiewithpath", c.getName());
191+
assertEquals("longeststring", c.getValue());
192+
assertEquals("/path/added/path", c.getPath());
193+
175194
}
176195

177196
@Test

0 commit comments

Comments
 (0)