Observations: URLClassPath and HTTP request
Eirik Bjørsnøs
eirbjo at gmail.com
Sun Feb 22 15:17:24 UTC 2026
On Sun, Feb 22, 2026 at 3:43 PM Alan Bateman <alan.bateman at oracle.com>
wrote:
> On 22/02/2026 14:29, Eirik Bjørsnøs wrote:
> > :
> >
> > 1) getResourceAsStream calls HEAD before calling GET. I don't quite
> > understand why this is necessary, it seems it would be better to do
> > the GET unconditionally to save a request.
> >
> I don't know if URLClassLoader is used much these days for remote class
> loading but it may be that getResourceAsStream is using getResource to
> get a URL to the resource, then it opens a connection.
Indeed, getResourceAsStream does call getResource to find the URL object,
which triggers the HEAD verification.
It seems we have the following choices:
0: Do nothing, HTTP URLs is not used much in URLClassPath
1: Teach Loader::findResource to return the resolved HttpURLConnection
target URL after following a redirect, instead of the original URL. This
would allow the GET request in getResourceAsStream to go directly to GET on
the redirect location, skipping the first GET. The change would be small
and look like:
diff --git
a/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java
b/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java
--- a/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java
(revision 68c0239618f36aaec2b390a28bccb1fddca421ba)
+++ b/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java
(date 1771772721240)
@@ -534,6 +534,8 @@
hconn.setRequestMethod("HEAD");
if (hconn.getResponseCode() >=
HttpURLConnection.HTTP_BAD_REQUEST) {
return null;
+ } else {
+ return hconn.getURL();
}
} else {
// our best guess for the other cases
2: Teach getResourceAsStream to find resources without doing GET first.
This would be a more involved change, since the "finding" algorithm would
need to involve GET instead of the current HEAD. Probably more risk than
reward.
Cheers,
Eirik.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/net-dev/attachments/20260222/3904adb8/attachment-0001.htm>
More information about the net-dev
mailing list