Compare cookies by paths

Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
diff --git a/core-common/src/main/java/org/glassfish/jersey/message/internal/CookiesParser.java b/core-common/src/main/java/org/glassfish/jersey/message/internal/CookiesParser.java
index a0257ca..7927e23 100644
--- a/core-common/src/main/java/org/glassfish/jersey/message/internal/CookiesParser.java
+++ b/core-common/src/main/java/org/glassfish/jersey/message/internal/CookiesParser.java
@@ -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
@@ -86,20 +86,29 @@
     }
 
     /**
-     * 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());
         }
     }
 
diff --git a/tests/e2e/src/test/java/org/glassfish/jersey/tests/api/CookieImplTest.java b/tests/e2e/src/test/java/org/glassfish/jersey/tests/api/CookieImplTest.java
index 234d855..cbeb277 100644
--- a/tests/e2e/src/test/java/org/glassfish/jersey/tests/api/CookieImplTest.java
+++ b/tests/e2e/src/test/java/org/glassfish/jersey/tests/api/CookieImplTest.java
@@ -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
@@ -151,27 +151,46 @@
     @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