<div dir="ltr"><div dir="ltr">Hi,<div><br></div><div>While working on JDK-8378398 / <a href="https://github.com/openjdk/jdk/pull/29864" target="_blank">https://github.com/openjdk/jdk/pull/29864</a>, I made some observations about URLClassLoader / URLClassPath and the way it handles HTTP redirects.</div><div><br></div><div>Let's say that user code does:</div><div><br></div><div>loader = new URLClassLoader(new URL[] {new URL("<a href="http://localhost/" target="_blank">http://localhost/</a>"});</div><div>loader.getResourceAsStream("image.gif");</div><div><br></div><div>This will internally call URLClassPath::findResource, which will do a HEAD to check if the resource exists. If the server responds with 301 Location: /target.gif, then HttpURLConnection will follow this redirect and do another HEAD for /target.gif.</div><div><br></div><div>Then, URLClassLoader will call openConnection and getInputStream on the URL. The HttpURLConnection does a GET for /image.gif and the server will respond with the same 301. HttpURLConnection does another GET, this time for /target.gif.</div><div><br></div><div>I found two observations interesting and somewhat unexpected:</div><div><br></div><div>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.</div><div><br></div><div>2) getResourceAsStream follows redirects for the HEAD request, but then completely forgets that this URL was just redirected and immediately performs a GET against the original URL, not the permanently redirected one. This seems wasteful.</div><div><br></div><div>To summarize, the code above currently produces the following requests:</div><div><br></div><div>HEAD /image.gif</div><div>HEAD /target.gif</div><div>GET /image.gif</div><div>GET /target.gif </div><div><br></div><div>While my intuition tells me the following should be sufficient:</div><div><br></div><div>GET /image.gif</div><div>GET /target.gif</div><div><br></div><div>Just wanted to share these observations, not sure if anything should be done to improve the current state of affairs. </div><div><br></div><div>Cheers,</div><div>Eirik.</div><div><br></div><div><br></div></div>
</div>