Commit Diff


commit - c6e8f3f771fbf7f948086eedb7cc0f3567ab88b3
commit + 10c8a6d2c4dd95be186b453e95746d68bb2a999b
blob - 4af7e16b04b98418e28a04a0aaa4f1259fa4b0af (mode 644)
blob + /dev/null
--- java/com/omarpolo/gemini/Response.java
+++ /dev/null
@@ -1,134 +0,0 @@
-package com.omarpolo.gemini;
-
-import javax.net.ssl.*;
-import java.io.*;
-import java.net.*;
-import java.security.KeyManagementException;
-import java.security.NoSuchAlgorithmException;
-import java.security.SecureRandom;
-import java.security.cert.X509Certificate;
-import java.util.Collections;
-import java.util.NoSuchElementException;
-import java.util.Scanner;
-
-public class Response implements AutoCloseable {
-
-    private final BufferedReader in;
-    private final PrintWriter out;
-    private final SSLSocket sock;
-
-    private final int code;
-    private final String meta;
-
-    public static class DummyManager extends X509ExtendedTrustManager {
-
-        @Override
-        public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) {
-        }
-
-        @Override
-        public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) {
-        }
-
-        @Override
-        public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
-        }
-
-        @Override
-        public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
-        }
-
-        @Override
-        public void checkClientTrusted(X509Certificate[] chain, String authType) {
-        }
-
-        @Override
-        public void checkServerTrusted(X509Certificate[] chain, String authType) {
-        }
-
-        @Override
-        public X509Certificate[] getAcceptedIssuers() {
-            return null;
-        }
-    }
-
-    public static class MalformedResponse extends Exception {}
-
-    public Response(String uri) throws IOException, MalformedResponse, URISyntaxException {
-        this(new URI(uri));
-    }
-
-    public Response(URI url) throws IOException, MalformedResponse {
-        this(url.getHost(), url.getPort(), url.toString() + "\r\n");
-    }
-
-    public Response(String host, int port, String req) throws IOException, MalformedResponse {
-        if (port == -1) {
-            port = 1965;
-        }
-
-        sock = connect(host, port);
-
-        var outStream = sock.getOutputStream();
-        out = new PrintWriter(
-                new BufferedWriter(new OutputStreamWriter(outStream)));
-
-        out.print(req);
-        out.flush();
-
-        var inStream = sock.getInputStream();
-        in = new BufferedReader(new InputStreamReader(inStream));
-
-        var reply = in.readLine();
-
-        if (reply.length() > 1027) {
-            throw new MalformedResponse();
-        }
-
-        var s = new Scanner(new StringReader(reply));
-        try {
-            code = s.nextInt();
-            s.skip(" ");
-            meta = s.nextLine();
-        } catch (NoSuchElementException e) {
-            throw new MalformedResponse();
-        }
-    }
-
-    public SSLSocket connect(String host, int port) throws IOException {
-        try {
-            var params = new SSLParameters();
-            params.setServerNames(Collections.singletonList(new SNIHostName(host)));
-
-            var ctx = SSLContext.getInstance("TLS");
-            ctx.init(null, new DummyManager[]{new DummyManager()}, new SecureRandom());
-            var factory = (SSLSocketFactory) ctx.getSocketFactory();
-
-            var socket = (SSLSocket) factory.createSocket(host, port);
-            socket.setSSLParameters(params);
-            socket.startHandshake();
-            return socket;
-        }
-        catch (NoSuchAlgorithmException | KeyManagementException e) {
-            throw new RuntimeException("Unexpected failure", e);
-        }
-    }
-
-    public int getCode() {
-        return code;
-    }
-
-    public String getMeta() {
-        return meta;
-    }
-
-    public BufferedReader body() {
-        return in;
-    }
-
-    public void close() throws IOException {
-        in.close();
-        out.close();
-        sock.close();
-    }
-}
blob - /dev/null
blob + 195ef9647afca4eccc7f8d34f99c6d95aea998a6 (mode 644)
--- /dev/null
+++ java/com/omarpolo/gemini/Request.java
@@ -0,0 +1,134 @@
+package com.omarpolo.gemini;
+
+import javax.net.ssl.*;
+import java.io.*;
+import java.net.*;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.security.cert.X509Certificate;
+import java.util.Collections;
+import java.util.NoSuchElementException;
+import java.util.Scanner;
+
+public class Request implements AutoCloseable {
+
+    private final BufferedReader in;
+    private final PrintWriter out;
+    private final SSLSocket sock;
+
+    private final int code;
+    private final String meta;
+
+    public static class DummyManager extends X509ExtendedTrustManager {
+
+        @Override
+        public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) {
+        }
+
+        @Override
+        public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) {
+        }
+
+        @Override
+        public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
+        }
+
+        @Override
+        public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
+        }
+
+        @Override
+        public void checkClientTrusted(X509Certificate[] chain, String authType) {
+        }
+
+        @Override
+        public void checkServerTrusted(X509Certificate[] chain, String authType) {
+        }
+
+        @Override
+        public X509Certificate[] getAcceptedIssuers() {
+            return null;
+        }
+    }
+
+    public static class MalformedResponse extends Exception {}
+
+    public Request(String uri) throws IOException, MalformedResponse, URISyntaxException {
+        this(new URI(uri));
+    }
+
+    public Request(URI url) throws IOException, MalformedResponse {
+        this(url.getHost(), url.getPort(), url.toString() + "\r\n");
+    }
+
+    public Request(String host, int port, String req) throws IOException, MalformedResponse {
+        if (port == -1) {
+            port = 1965;
+        }
+
+        sock = connect(host, port);
+
+        var outStream = sock.getOutputStream();
+        out = new PrintWriter(
+                new BufferedWriter(new OutputStreamWriter(outStream)));
+
+        out.print(req);
+        out.flush();
+
+        var inStream = sock.getInputStream();
+        in = new BufferedReader(new InputStreamReader(inStream));
+
+        var reply = in.readLine();
+
+        if (reply.length() > 1027) {
+            throw new MalformedResponse();
+        }
+
+        var s = new Scanner(new StringReader(reply));
+        try {
+            code = s.nextInt();
+            s.skip(" ");
+            meta = s.nextLine();
+        } catch (NoSuchElementException e) {
+            throw new MalformedResponse();
+        }
+    }
+
+    public SSLSocket connect(String host, int port) throws IOException {
+        try {
+            var params = new SSLParameters();
+            params.setServerNames(Collections.singletonList(new SNIHostName(host)));
+
+            var ctx = SSLContext.getInstance("TLS");
+            ctx.init(null, new DummyManager[]{new DummyManager()}, new SecureRandom());
+            var factory = (SSLSocketFactory) ctx.getSocketFactory();
+
+            var socket = (SSLSocket) factory.createSocket(host, port);
+            socket.setSSLParameters(params);
+            socket.startHandshake();
+            return socket;
+        }
+        catch (NoSuchAlgorithmException | KeyManagementException e) {
+            throw new RuntimeException("Unexpected failure", e);
+        }
+    }
+
+    public int getCode() {
+        return code;
+    }
+
+    public String getMeta() {
+        return meta;
+    }
+
+    public BufferedReader body() {
+        return in;
+    }
+
+    public void close() throws IOException {
+        in.close();
+        out.close();
+        sock.close();
+    }
+}
blob - be7aae2769a2bfbb000e20794f4bb33d80f36120
blob + 758ee354614329ac319d96cd0b8c6a0c97636e55
--- src/blog/net_gemini.clj
+++ src/blog/net_gemini.clj
@@ -1,7 +1,13 @@
 (ns blog.net-gemini
-  (:import (com.omarpolo.gemini Response)))
+  (:import (com.omarpolo.gemini Request)))
 
 (defn head [host port req]
-  (with-open [res (Response. host port (str req "\r\n"))]
+  (with-open [res (Request. host port (str req "\r\n"))]
     {:code (.getCode res)
      :meta (.getMeta res)}))
+
+(comment
+  (with-open [res (Request. "gemini://localhost/index.gmi")]
+    {:code (.getCode res)
+     :meta (.getMeta res)})
+)