1 21 24 package org.lobobrowser.util; 25 26 import java.util.*; 27 28 31 public class Domains { 32 private static final Collection gTLDs; 33 34 static { 35 gTLDs = new HashSet(); 36 gTLDs.add(".com"); 37 gTLDs.add(".edu"); 38 gTLDs.add(".gov"); 39 gTLDs.add(".int"); 40 gTLDs.add(".mil"); 41 gTLDs.add(".net"); 42 gTLDs.add(".org"); 43 gTLDs.add(".biz"); 44 gTLDs.add(".info"); 45 gTLDs.add(".name"); 46 gTLDs.add(".pro"); 47 gTLDs.add(".aero"); 48 gTLDs.add(".coop"); 49 gTLDs.add(".museum"); 50 } 52 53 56 private Domains() { 57 super(); 58 } 59 60 public static boolean isValidCookieDomain(String domain, String hostName) { 61 String plainDomain; 62 if(!domain.startsWith(".")) { 63 plainDomain = domain; 68 domain = "." + domain; 69 } 70 else { 71 plainDomain = domain.substring(1); 72 } 73 String plainDomainTL = plainDomain.toLowerCase(); 74 String hostNameTL = hostName.toLowerCase(); 75 if(!hostNameTL.endsWith(plainDomainTL)) { 76 return false; 77 } 78 int lastDotIdx = domain.lastIndexOf('.'); 79 if(lastDotIdx == -1) { 80 return false; 81 } 82 String suffix = domain.substring(lastDotIdx).toLowerCase(); 83 if(gTLDs.contains(suffix)) { 84 return Strings.countChars(domain, '.') >= 2; 85 } 86 else { 87 return Strings.countChars(domain, '.') >= 3; 88 } 89 } 90 91 96 public static boolean endsWithGTLD(String host) { 97 Iterator i = gTLDs.iterator(); 98 while(i.hasNext()) { 99 String ending = (String ) i.next(); 100 if(host.endsWith(ending)) { 101 return true; 102 } 103 } 104 return false; 105 } 106 107 public static boolean isLikelyHostName(String name) { 108 String nameTL = name.toLowerCase(); 109 if(nameTL.startsWith("www.")) { 110 return true; 111 } 112 if(endsWithGTLD(name)) { 113 return true; 114 } 115 int lastDotIdx = nameTL.lastIndexOf('.'); 116 if(lastDotIdx == -1) { 117 return false; 118 } 119 return lastDotIdx == nameTL.length() - 3; 121 } 122 123 public static Collection getPossibleDomains(String hostName) { 124 Collection domains = new LinkedList(); 125 domains.add(hostName); 126 int dotIdx = hostName.indexOf('.', 1); 127 if(dotIdx == -1) { 128 return domains; 129 } 130 String testDomain = hostName.substring(dotIdx); 131 if(!Domains.isValidCookieDomain(testDomain, hostName)) { 132 return domains; 133 } 134 domains.addAll(Domains.getPossibleDomains(testDomain.substring(1))); 135 return domains; 136 } 137 } 138 | Popular Tags |