069645613112 — Quintillus 2 years ago
Parse out the header meta data, and follow redirects (status 31).

Note that this does not yet implement the best practices for redirects, particularly loop detection and insecure redirect warnings.
M src/main/java/com/ajtjp/geminiclient/GeminiClient.java +9 -2
@@ 45,7 45,14 @@ public class GeminiClient {
         os.flush();
         
         InputStream is = s.getInputStream();
-        doc.setHeaders(parseHeaders(is));
+        doc.setHeaders(readHeaders(is));
+        
+        if (doc.statusCode == 31) {
+            //TODO: Redirect loop detection
+            //TODO: Insecure redirect warnings
+            //TODO: Ports other than 1965 (this one applies elsewhere, too)
+            return connect(doc.meta, 1965);
+        }
         
         StringBuilder sb = new StringBuilder();
         byte[] buffer = new byte[2000];

          
@@ 77,7 84,7 @@ public class GeminiClient {
         return host;
     }
     
-    private static String parseHeaders(InputStream is) throws IOException {
+    private static String readHeaders(InputStream is) throws IOException {
         //Max size is 1029 bytes, 1024 for meta + 2 for status + 3 for whitespace
         byte[] buffer = new byte[1200];
         int bytesRead = is.read(buffer, 0, 1200);

          
M src/main/java/com/ajtjp/geminiclient/GeminiDocument.java +2 -0
@@ 8,6 8,7 @@ public class GeminiDocument {
     String documentURL;
     String headers; //eventually, will parse this out more semantically
     int statusCode;
+    String meta;
     
     List<GeminiLine> lines;
     

          
@@ 36,6 37,7 @@ public class GeminiDocument {
         this.headers = headers;
         try {
             statusCode = Integer.parseInt(headers.substring(0, 2));
+            meta = headers.substring(3);
         }
         catch(NumberFormatException ex) {
             statusCode = 50;