Bug 22927 - URL.openStream doesn't handle 302 and 404 HTTP response codes properly
Summary: URL.openStream doesn't handle 302 and 404 HTTP response codes properly
Status: RESOLVED FIXED
Alias: None
Product: classpath
Classification: Unclassified
Component: classpath (show other bugs)
Version: unspecified
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-04-26 12:05 UTC by from-classpath
Modified: 2005-07-23 22:54 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description from-classpath 2005-04-26 12:05:39 UTC
For HTTP URLs that return response code 302 (Moved Temporarily), URL.openStream should follow the redirect to the new location. Currently it returns null.

For HTTP URLs that return response code 404 (Not Found), URL.openStream should raise a FileNotFoundException. Currently it returns a stream with the error document as if it received the response code 200 (OK).
Comment 1 from-classpath 2005-04-26 12:40:32 UTC
The 30x redirect issue was explained a bit more on irc.
What happens is that some servers just sent a absolutePath as Location: header while the spec says (and our code expects) a full absoluteUri. Proposed patch to be liberal in what we accept:

diff -u -r1.6 HTTPURLConnection.java
--- gnu/java/net/protocol/http/HTTPURLConnection.java   2 Mar 2005 17:29:09 -0000       1.6
+++ gnu/java/net/protocol/http/HTTPURLConnection.java   26 Apr 2005 12:34:05 -0000
@@ -239,6 +239,13 @@
                 file = location.substring(start);
                 retry = true;
               }
+           if (location.charAt(0) == '/')
+             {
+               // Location URLs should be full absoluteURIs,
+               // but in practise some servers send just an absolute path.
+               file = location;
+               retry = true;
+             }
             else if (location.startsWith("http:"))
               {
                 connection.close();


The 40x transformation to FileNotFoundException looks strange to me. We do properly return the 404 when you ask for getResponseCode(). Then we do indeed return as InputStream the page that the server sends us.

Maybe the behavior should different between uc.openConnection() and uc.openStream()?
Comment 2 from-classpath 2005-04-27 08:57:01 UTC
Fixed by the following two changes:

2005-04-27  Chris Burdess  <dog@gnu.org>

        * java/net/protocol/http/HTTPURLConnection.java (connect): Accept
        absolute and relative paths in Location header.

2005-04-26  Chris Burdess  <dog@gnu.org>

        * gnu/java/net/protocol/http/HTTPURLConnection.java: Throw
        FileNotFoundException and implement getErrorStream on 404.