001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.io.res.type.http; 020 021import lucee.commons.lang.StringUtil; 022import lucee.runtime.Info; 023import lucee.runtime.net.proxy.ProxyData; 024 025public final class HTTPConnectionData { 026 027 public static final String DEFAULT_USER_AGENT = "Lucee "+Info.getVersionAsString()+" "+Info.getStateAsString(); 028 029 030 public String username=""; 031 public String password=""; 032 public String host="localhost"; 033 public int port=0; 034 public ProxyData proxyData; 035 public String path; 036 public String userAgent=DEFAULT_USER_AGENT; 037 038 039 public int timeout; 040 041 public HTTPConnectionData(String username, String password, String host, int port, String path,ProxyData proxyData, String userAgent) { 042 this.username = username; 043 this.password = password; 044 this.host = host; 045 this.port = port; 046 this.proxyData = proxyData; 047 this.path = path; 048 if(!StringUtil.isEmpty(userAgent))this.userAgent = userAgent; 049 } 050 051 052 053 public HTTPConnectionData(String path,int timeout) { 054 load(path); 055 this.timeout=timeout; 056 057 } 058 public HTTPConnectionData(String path) { 059 load(path); 060 } 061 062 063 064 public void load(String path) { 065 username=""; 066 password=""; 067 host=null; 068 port=-1; 069 // TODO impl proxy 070 071 int atIndex=path.indexOf('@'); 072 int slashIndex=path.indexOf('/'); 073 if(atIndex>slashIndex)atIndex=-1; 074 075 if(slashIndex==-1){ 076 slashIndex=path.length(); 077 path+="/"; 078 } 079 int index; 080 081 // username/password 082 if(atIndex!=-1) { 083 index=path.indexOf(':'); 084 if(index!=-1 && index<atIndex) { 085 username=path.substring(0,index); 086 password=path.substring(index+1,atIndex); 087 } 088 else username=path.substring(0,atIndex); 089 } 090 // host port 091 if(slashIndex>atIndex+1) { 092 index=path.indexOf(':',atIndex+1); 093 if(index!=-1 && index>atIndex && index<slashIndex) { 094 host=path.substring(atIndex+1,index); 095 port=Integer.parseInt(path.substring(index+1,slashIndex)); 096 } 097 else host=path.substring(atIndex+1,slashIndex); 098 } 099 100 this.path= path.substring(slashIndex); 101 } 102 103 104 105 @Override 106 public String toString() { 107 return "username:"+username+";password:"+password+";hostname:"+host+";port:"+port+";path:"+path; 108 } 109 110 public String key() { 111 if(StringUtil.isEmpty(username)) 112 return host+_port(); 113 return username+":"+password+"@"+host+_port(); 114 } 115 116 117 118 private String _port() { 119 if(port>0) return ":"+port; 120 return ""; 121 } 122 123 124 125 public boolean hasProxyData() { 126 return proxyData!=null && proxyData.getServer()!=null; 127 } 128 129 130 131 132 @Override 133 public boolean equals(Object obj) { 134 if(this==obj)return true; 135 if(!(obj instanceof HTTPConnectionData)) return false; 136 return key().equals(((HTTPConnectionData)obj).key()); 137 } 138 139 140 141 public void setProxyData(ProxyData proxyData) { 142 this.proxyData=proxyData; 143 } 144 145 /*public static void main(String[] args) { 146 test("search.twitter.com/search.atom?q=@mktweetup"); 147 test("search.twitter.com/search.atom?q=mktweetup"); 148 test("u@search.twitter.com/search.atom?q=mktweetup"); 149 test("u:p@search.twitter.com/search.atom?q=mktweetup"); 150 } 151 private static void test(String string) { 152 print.out(string); 153 HTTPConnectionData data = new HTTPConnectionData(string); 154 print.out(data.toString()); 155 }*/ 156}