support for HTTP 3xx redirects
authoradam <adam@megacz.com>
Fri, 3 Sep 2004 00:50:19 +0000 (00:50 +0000)
committeradam <adam@megacz.com>
Fri, 3 Sep 2004 00:50:19 +0000 (00:50 +0000)
darcs-hash:20040903005019-5007d-9c8b15091c6bf87ae3c8848273a8c0f4e36bd458.gz

src/org/ibex/net/HTTP.java

index c95183c..69f54cd 100644 (file)
@@ -16,8 +16,9 @@ public class HTTP {
     public static InetAddress originAddr = null;
     public static String      originHost = null;
 
-    // FIXME: HACK
+    // FIXME: HACK: these shouldn't be set globally
     public static String userAgent = "Ibex";
+    public static boolean allowRedirects = true;
     
     // Cookies //////////////////////////////////////////////////////////////////////////////
 
@@ -200,6 +201,15 @@ public class HTTP {
                 releaseMe.release();
                 return makeRequest(contentType, content, referer, cookies);
                 
+            } else if (reply.startsWith("3") && allowRedirects) {
+                String location = (String)h.get("location");
+                if (location == null) throw new HTTPException("Got HTTP " + reply.substring(0, 3) + " but no Location header");
+                Log.info(HTTP.class, "redirecting to " + location);
+                if (content != null)
+                    return new HTTP(location).POST(contentType, content, url, cookies);
+                else
+                    return new HTTP(location).GET(url, cookies);
+                
             } else if (reply.startsWith("2")) {
                 if (h.get("HTTP").equals("1.0") && h.get("content-length") == null)
                     throw new HTTPException("Ibex does not support HTTP/1.0 servers which fail to return the Content-Length header");