Repository: jhy/jsoup
Pull request #2222 (2024-11-04 23:25:26)

Bump org.apache.maven.plugins:maven-javadoc-plugin from 3.10.0 to 3.11.1
Bumps [org.apache.maven.plugins:maven-javadoc-plugin](https://github.com/apache/maven-javadoc-plugin) from 3.10.0 to 3.11.1.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/apache/maven-javadoc-plugin/releases">org.apache.maven.plugins:maven-javadoc-plugin's releases</a>.</em></p>
<blockquote>
<h2>maven-javadoc-plugin-3.10.1</h2>
<h2>What's Changed</h2>
<ul>
<li>[MJAVADOC-810] [REGRESSION] MJAVADOC-791 causes forked Maven execution fail if any toolchains or settings file isn't present by <a href="https://github.com/cstamas"><code>@​cstamas</code></a> in <a href="https://redirect.github.com/apache/maven-javadoc-plugin/pull/313">apache/maven-javadoc-plugin#313</a></li>
<li>Bump org.apache.commons:commons-lang3 from 3.16.0 to 3.17.0 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://redirect.github.com/apache/maven-javadoc-plugin/pull/314">apache/maven-javadoc-plugin#314</a></li>
<li>Bump org.codehaus.plexus:plexus-io from 3.5.0 to 3.5.1 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://redirect.github.com/apache/maven-javadoc-plugin/pull/315">apache/maven-javadoc-plugin#315</a></li>
<li>Bump commons-io:commons-io from 2.16.1 to 2.17.0 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://redirect.github.com/apache/maven-javadoc-plugin/pull/319">apache/maven-javadoc-plugin#319</a></li>
<li>[MJAVADOC-812] [REGRESSION] maven-javadoc-plugin 3.10.0 creates empty… by <a href="https://github.com/michael-o"><code>@​michael-o</code></a> in <a href="https://redirect.github.com/apache/maven-javadoc-plugin/pull/320">apache/maven-javadoc-plugin#320</a></li>
</ul>
<p><strong>Full Changelog</strong>: <a href="https://github.com/apache/maven-javadoc-plugin/compare/maven-javadoc-plugin-3.10.0...maven-javadoc-plugin-3.10.1">https://github.com/apache/maven-javadoc-plugin/compare/maven-javadoc-plugin-3.10.0...maven-javadoc-plugin-3.10.1</a></p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/apache/maven-javadoc-plugin/commit/619650c667070ef114d571c0f075ce456ace1cf8"><code>619650c</code></a> [maven-release-plugin] prepare release maven-javadoc-plugin-3.11.1</li>
<li><a href="https://github.com/apache/maven-javadoc-plugin/commit/e314da0226d100eae39d96d7a0cbdad799879dca"><code>e314da0</code></a> [MJAVADOC-821] Align toolchain discovery code with Maven Compiler Plugin</li>
<li><a href="https://github.com/apache/maven-javadoc-plugin/commit/62a686180915de1e3c0a2f71081776eacfd1d4c3"><code>62a6861</code></a> [MJAVADOC-820] [REGRESSION] MJAVADOC-787 was merged incompletely</li>
<li><a href="https://github.com/apache/maven-javadoc-plugin/commit/d1090c50ce66e10ab0adcadb996b0fe790c900b6"><code>d1090c5</code></a> [maven-release-plugin] prepare for next development iteration</li>
<li><a href="https://github.com/apache/maven-javadoc-plugin/commit/ee030f719f4bc7df1671f6dfe5993f778b57a504"><code>ee030f7</code></a> [maven-release-plugin] prepare release maven-javadoc-plugin-3.11.0</li>
<li><a href="https://github.com/apache/maven-javadoc-plugin/commit/6c5fdc06d76fdd429c4d3ae01e70be8917c64bea"><code>6c5fdc0</code></a> [MJAVADOC-819] Align archive generation code with Maven Source Plugin</li>
<li><a href="https://github.com/apache/maven-javadoc-plugin/commit/3a90de597adff7c8f8c4a420400f24c7386e9880"><code>3a90de5</code></a> [MJAVADOC-787] Automatic detection of release option for JDK &lt; 9</li>
<li><a href="https://github.com/apache/maven-javadoc-plugin/commit/373172d8782ad011cc14e20ac78a84d032ad1d4b"><code>373172d</code></a> [MJAVADOC-817] Upgrade to Doxia 2.0.0 GA Stack</li>
<li><a href="https://github.com/apache/maven-javadoc-plugin/commit/ba266c0fe11a822bf3fb19c365a174246eae29f9"><code>ba266c0</code></a> Fix SCM tag</li>
<li><a href="https://github.com/apache/maven-javadoc-plugin/commit/5775ce13455d565bd10b15cbeb94310074dee8a3"><code>5775ce1</code></a> Fix typo</li>
<li>Additional commits viewable in <a href="https://github.com/apache/maven-javadoc-plugin/compare/maven-javadoc-plugin-3.10.0...maven-javadoc-plugin-3.11.1">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=org.apache.maven.plugins:maven-javadoc-plugin&package-manager=maven&previous-version=3.10.0&new-version=3.11.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>


Patch:
diff --git a/.gitignore b/.gitignore
index b22249a2..a7ff73a6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,3 +9,4 @@ target/
 *Thrash*
 bin/
 .vscode/
+.java-version
diff --git a/CHANGES.md b/CHANGES.md
index 39b789d9..73d01e17 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -29,8 +29,10 @@
   children. [2187](https://github.com/jhy/jsoup/issues/2187)
 * A selector query that included multiple `:has()` components in a nested `:has()` might incorrectly
   execute. [2131](https://github.com/jhy/jsoup/issues/2131)
-* Updated the simple view of cookies available via `Connection.Response#cookies()` to reflect the contents of the 
-  current cookie jar for the current URL. [1831](https://github.com/jhy/jsoup/issues/1831)
+* When cookie names in a response are duplicated, the simple view of cookies available via
+  `Connection.Response#cookies()` will provide the last one set. Generally it is better to use
+  the [Jsoup.newSession](https://jsoup.org/cookbook/web/request-session) method to maintain a cookie jar, as that
+  applies appropriate path selection on cookies when making requests. [1831](https://github.com/jhy/jsoup/issues/1831)
 
 ## 1.18.1 (2024-Jul-10)
 
diff --git a/pom.xml b/pom.xml
index 201f441b..eccb9388 100644
--- a/pom.xml
+++ b/pom.xml
@@ -118,7 +118,7 @@
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-javadoc-plugin</artifactId>
-        <version>3.10.0</version>
+        <version>3.11.1</version>
         <configuration>
           <doclint>none</doclint>
           <source>8</source>
diff --git a/src/main/java/org/jsoup/Connection.java b/src/main/java/org/jsoup/Connection.java
index 55a5dbac..8ced43f0 100644
--- a/src/main/java/org/jsoup/Connection.java
+++ b/src/main/java/org/jsoup/Connection.java
@@ -632,9 +632,12 @@ public interface Connection {
         T removeCookie(String name);
 
         /**
-         * Retrieve all of the request/response cookies as a map
-         * @return cookies
-         * @see #cookieStore()
+         Retrieve the request/response cookies as a map. For response cookies, if duplicate cookie names were sent, the
+         last one set will be the one included. For session management, rather than using these response cookies, prefer
+         to use {@link Jsoup#newSession()} and related methods.
+
+         @return simple cookie map
+         @see #cookieStore()
          */
         Map<String, String> cookies();
     }
diff --git a/src/main/java/org/jsoup/helper/CookieUtil.java b/src/main/java/org/jsoup/helper/CookieUtil.java
index 218e935e..5ef9c468 100644
--- a/src/main/java/org/jsoup/helper/CookieUtil.java
+++ b/src/main/java/org/jsoup/helper/CookieUtil.java
@@ -2,6 +2,7 @@ package org.jsoup.helper;
 
 import org.jsoup.Connection;
 import org.jsoup.internal.StringUtil;
+import org.jsoup.parser.TokenQueue;
 
 import java.io.IOException;
 import java.net.CookieManager;
@@ -91,13 +92,21 @@ class CookieUtil {
         URI uri = CookieUtil.asUri(url);
         manager.put(uri, resHeaders); // stores cookies for session
 
-        // set up the simple cookie(name, value) map:
-        Map<String, List<String>> cookieMap = manager.get(uri, resHeaders); // get cookies for url; may have been set on this or earlier requests. the headers here are ignored other than a null check
-        for (List<String> values : cookieMap.values()) {
-            for (String headerVal : values) {
-                List<HttpCookie> cookies = HttpCookie.parse(headerVal);
-                for (HttpCookie cookie : cookies) {
-                    res.cookie(cookie.getName(), cookie.getValue());
+        // set up the simple cookies() map
+        // the response may include cookies that are not relevant to this request, but users may require them if they are not using the cookie manager (setting request cookies only from the simple cookies() response):
+        for (Map.Entry<String, List<String>> entry : resHeaders.entrySet()) {
+            String name = entry.getKey();
+            List<String> values = entry.getValue();
+            if (name.equalsIgnoreCase("Set-Cookie")) {
+                for (String value : values) {
+                    if (value == null)
+                        continue;
+                    TokenQueue cd = new TokenQueue(value);
+                    String cookieName = cd.chompTo("=").trim();
+                    String cookieVal = cd.consumeTo(";").trim();
+                    // ignores path, date, domain, validateTLSCertificates et al. full details will be available in cookiestore if required
+                    // name not blank, value not null
+                    res.cookie(cookieName, cookieVal); // if duplicate names, last set will win
                 }
             }
         }
diff --git a/src/main/java/org/jsoup/helper/HttpConnection.java b/src/main/java/org/jsoup/helper/HttpConnection.java
index 71201910..a33bcd91 100644
--- a/src/main/java/org/jsoup/helper/HttpConnection.java
+++ b/src/main/java/org/jsoup/helper/HttpConnection.java
@@ -1136,6 +1136,11 @@ public class HttpConnection implements Connection {
             CookieUtil.storeCookies(req, this, url, resHeaders); // add set cookies to cookie store
 
             if (previousResponse != null) { // was redirected
+                // map previous response cookies into this response cookies() object
+                for (Map.Entry<String, String> prevCookie : previousResponse.cookies().entrySet()) {
+                    if (!hasCookie(prevCookie.getKey()))
+                        cookie(prevCookie.getKey(), prevCookie.getValue());
+                }
                 previousResponse.safeClose();
 
                 // enforce too many redirects:


Test patch:
diff --git a/src/test/java/org/jsoup/integration/ConnectTest.java b/src/test/java/org/jsoup/integration/ConnectTest.java
index 6042f81c..8929dcac 100644
--- a/src/test/java/org/jsoup/integration/ConnectTest.java
+++ b/src/test/java/org/jsoup/integration/ConnectTest.java
@@ -1026,4 +1026,3 @@ public class ConnectTest {
         assertEquals(LargeDocTextLen, document.text().length());
     }
 }
-
diff --git a/src/test/java/org/jsoup/integration/SessionTest.java b/src/test/java/org/jsoup/integration/SessionTest.java
index 89264050..4ae5c5e6 100644
--- a/src/test/java/org/jsoup/integration/SessionTest.java
+++ b/src/test/java/org/jsoup/integration/SessionTest.java
@@ -48,9 +48,12 @@ public class SessionTest {
         Connection con2 = session.newRequest().data(CookieServlet.SetCookiesParam, "1");
         Document doc2 = con2.get();
         assertEquals(0, doc2.select("table tr").size());  // none sent to servlet - we just got them!
-        Map<String, String> cookies = con2.response().cookies(); // simple cookie response, all named "One", so should be first sent
-        assertEquals(1, cookies.size());
-        assertEquals("Root", cookies.get("One"));
+        Map<String, String> cookies = con2.response().cookies(); // simple cookie response, all named "One", so should be last sent
+        assertEquals(2, cookies.size());
+        assertEquals("EchoServlet", cookies.get("One"));
+
+        // test that all response cookies are present, even if would not be sent for this request path (i.e. cookies() res can be set on a req, without using sessions)
+        assertEquals("Override", cookies.get("Two"));
 
         // todo - interrogate cookie-store
 
diff --git a/src/test/java/org/jsoup/integration/servlets/CookieServlet.java b/src/test/java/org/jsoup/integration/servlets/CookieServlet.java
index ef5955f5..ccddee72 100644
--- a/src/test/java/org/jsoup/integration/servlets/CookieServlet.java
+++ b/src/test/java/org/jsoup/integration/servlets/CookieServlet.java
@@ -61,6 +61,14 @@ public class CookieServlet extends BaseServlet {
         Cookie three = new Cookie("One", "EchoServlet");
         three.setPath("/EchoServlet");
         res.addCookie(three);
+
+        Cookie four = new Cookie("Two", "NoSuchPath");
+        four.setPath("/bogus");
+        res.addCookie(four);
+
+        Cookie five = new Cookie("Two", "Override");
+        five.setPath("/bogus");
+        res.addCookie(five);
     }
 
 }