Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support http/2 requests via HttpClient #2257

Merged
merged 2 commits into from
Jan 8, 2025
Merged

Support http/2 requests via HttpClient #2257

merged 2 commits into from
Jan 8, 2025

Conversation

jhy
Copy link
Owner

@jhy jhy commented Jan 7, 2025

This adds support for executing HTTP requests using the Java 11+ HttpClient on systems that support it, enabling http/2 requests. On Java 8, and on Android, requests will still go via the existing HttpURLConnection.

This also gives a path for http/3 support when JEP HTTP/3 for the HTTP Client API lands in Java.

This is currently disabled by default -- set the system property jsoup.useHttpClient to enable it. Once it gets tested and used by folks for a while, I plan to make it the default (for 1.20.x). Please do test it and let me know of any issues. It should largely be a drop-in replacement for the existing behavior.

This is implemented using the multi-release JAR feature, where a HttpClientExecutor is provided in the /java11 version directory. When executed on earlier Java versions, that code is not found and the compatible UrlConnectionExecutor is used instead.

A couple notes:

  • I moved the existing module-info.java from Java 9 to Java 11, so that we only need one module definition. That allows simple builds using IntelliJ (for e.g.) to continue to work, without requiring the full Maven build on each iteration. Given that 9 is not a LTS release, I don't expect that change to be an issue.
  • For future work, would like to look at moving the HttpClient initiation from per-request to per-session; this may streamline repeated connection requests to the same hosts.
  • All the 67 existing tests in ConnectTest are run both with HttpURLConnection and the new HttpClient impl and pass. As do the Session tests, and integration tests -- with the exception of the ability to interrupt the execution via a Thread.interrupt (as it executes in a managed worker thread; but timeouts are reliably and a better way to stop execution).

This adds support for the Java 11+ HttpClient on systems that support it, enabling http/2 requests. On Java 8, and on Android, requests will still go via the existing HttpURLConnection.

This is currently disabled by default -- set the system property `jsoup.useHttpClient` to enable it.
@jhy jhy added the feature label Jan 7, 2025
@jhy jhy added this to the 1.19.1 milestone Jan 7, 2025
Java 21 exhibits more progress events when using HttpClient
@jhy jhy changed the title Support http/2 requests via optional HttpClient Support http/2 requests via HttpClient Jan 7, 2025
@jhy jhy merged commit 18f87f1 into master Jan 8, 2025
22 checks passed
@jhy jhy deleted the httpclient branch January 8, 2025 04:01
jhy added a commit that referenced this pull request Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant