]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/util/PublicSuffixes.java
add: Highlight certificates outside validity period in Cert Overview
[gigi.git] / src / org / cacert / gigi / util / PublicSuffixes.java
index bb0d027a9657c457ab3930b87504adaa9f28dbf6..19c7b1dc952d15bf6255f98209c0ce7ebe1e1a5d 100644 (file)
@@ -2,35 +2,39 @@ package org.cacert.gigi.util;
 
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
 import java.net.IDN;
-import java.net.URL;
 import java.util.HashSet;
 
 public class PublicSuffixes {
 
-    HashSet<String> suffixes = new HashSet<>();
+    private HashSet<String> suffixes = new HashSet<>();
 
-    HashSet<String> wildcards = new HashSet<>();
+    private HashSet<String> wildcards = new HashSet<>();
 
-    HashSet<String> exceptions = new HashSet<>();
+    private HashSet<String> exceptions = new HashSet<>();
 
-    private static final String url = "https://publicsuffix.org/list/effective_tld_names.dat";
+    static final String url = "https://publicsuffix.org/list/effective_tld_names.dat";
 
     private static PublicSuffixes instance;
 
-    private static void generateDefault() throws IOException {
-        URL u = new URL(url);
-        HttpURLConnection huc = (HttpURLConnection) u.openConnection();
-        BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream(), "UTF-8"));
-        instance = new PublicSuffixes(br);
+    private static PublicSuffixes generateDefault() throws IOException {
+        InputStream res = PublicSuffixes.class.getResourceAsStream("effective_tld_names.dat");
+
+        if (null == res) {
+            throw new Error("Public Suffix List could not be loaded.");
+        }
+
+        try (BufferedReader br = new BufferedReader(new InputStreamReader(res, "UTF-8"))) {
+            return new PublicSuffixes(br);
+        }
     }
 
-    public static PublicSuffixes getInstance() {
+    public synchronized static PublicSuffixes getInstance() {
         if (instance == null) {
             try {
-                generateDefault();
+                instance = generateDefault();
             } catch (IOException e) {
                 throw new Error(e);
             }
@@ -47,7 +51,11 @@ public class PublicSuffixes {
             if (line.isEmpty()) {
                 continue;
             }
-            line = line.split("\\s", 2)[0];
+            String[] lineParts = line.split("\\s", 2);
+            if (lineParts.length == 0) {
+                throw new Error("split had strange behavior");
+            }
+            line = lineParts[0];
             if (line.startsWith("*.")) {
                 String data = line.substring(2);
                 if (data.contains("*") || data.contains("!")) {
@@ -127,4 +135,5 @@ public class PublicSuffixes {
         }
         return false;
     }
+
 }