域名的匹配,即测试两个网站的url是不是层次匹配的,

来源:互联网 发布:淘宝购物车营销下架了 编辑:程序博客网 时间:2024/06/03 23:44
public class DNSStringMatching {public static boolean DNSStrMatch(String dns,String url){boolean flag = false ;String tmpdns = dns.trim();//去空格String tmpstr="";String tmpurl = url.trim();if(tmpdns.length() > tmpurl.length()) return flag;if(tmpdns.length()>=7){tmpstr = tmpdns.substring(0, 7);if(tmpstr.equals("http://")){tmpdns = tmpdns.substring(7);//截取http://后面的内容}}if(tmpdns.length()>=4){tmpstr = tmpdns.substring(0, 4);if(tmpstr.equals("www.")){tmpdns = tmpdns.substring(4);//截取www.后面的内容}}if(tmpurl.length()>=7){tmpstr = tmpurl.substring(0, 7);if(tmpstr.equals("http://")){tmpurl = tmpurl.substring(7);//截取http://后面的内容}}if(tmpurl.length()>=4){tmpstr = tmpurl.substring(0, 4);if(tmpstr.equals("www.")){tmpurl = tmpurl.substring(4);//截取www.后面的内容}}int pos = tmpurl.indexOf(tmpdns);//tmpdns在url中的起始位置String ss = "";if(pos > 0) ss = tmpurl.substring( 0 , pos);int ishx = ss.indexOf("/");//判断是否是伪造的int strlen = tmpdns.length();//tmpdns的长度int dotposd = tmpdns.indexOf(".");int dotposu = tmpurl.indexOf(".");if(tmpdns.substring(strlen-1).equals("/"))//tmpdns最后一个字符是'/',则只要url中存在tmpdns则肯定能匹配{if(-1 != pos){if(-1 != ishx)//存在"/",则说明是伪造的return false ;else{if(!tmpdns.substring(0, dotposd).equals(tmpurl.substring(0, dotposu))){while(0 <= (pos-1)){if(tmpurl.substring(pos - 1, pos).equals("."))return true ;else return false ;}}else{flag = true;}}}}else{String tag="";if(-1 != pos){if(-1 != ishx)//存在"/",则说明是伪造的return false ;else{int ulen = tmpurl.length() - pos;if(ulen == tmpdns.length()){//dns与url右边的子串匹配if(!tmpdns.substring(0, dotposd).equals(tmpurl.substring(0, dotposu))){while(0 <= (pos-1)){if(tmpurl.substring(pos - 1, pos).equals("."))return true ;else return false ;}}else return true ;}else{//不是和右边的子串匹配tag = tmpurl.substring(pos + strlen, pos + strlen +1);}}}if(tag.equals("/")){ if(!tmpdns.substring(0, dotposd).equals(tmpurl.substring(0, dotposu))){while(0 <= (pos-1)){if(tmpurl.substring(pos - 1, pos).equals("."))return true ;else return false ;}}else return true ;}}return flag;}public static void main(String[] args){String dns[]={"http://www.people.com.cn/","http://xinhuanet.com","www.hb.xinhuanet.com/","www.hb.xinhuanet.com","www.b.xinhuanet.com/","www.b.xinhuanet.com","www.xinhuanet.com/","xinhuanet.com/2013-08/14","http://www.hnb.n.xinhuanet.com.cn","www.b.xinhuanet.com/","xinhuanet.com/2013-08/14/c_116945022.htm","http://xinhuanet.com/ ","b.xinhuanet.com/","http://xinhuanet.com/ ","tv.cn","xinhuanet.com","www.xinhuanet.com","http://www.xinhuanet.com","xinhuanet.org"};//这些是测试用例String url="http://fujian.people.com.cn/n/2013/0705/c181466-19006919.html";for(String str:dns){if(DNSStringMatching.DNSStrMatch(str, url))System.out.println("DNS:"+str+"  URL:"+ url + "  Match success!");elseSystem.out.println("DNS:"+str + "  URL:"+url+ "  Match failed!");}}}

0 0
原创粉丝点击