1 29 30 package com.caucho.quercus.lib.curl; 31 32 import com.caucho.quercus.QuercusModuleException; 33 34 import java.io.Closeable ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.OutputStream ; 38 import java.net.*; 39 40 43 public class HttpConnection 44 implements Closeable 45 { 46 private HttpURLConnection _conn; 47 48 private URL _URL; 49 private String _username; 50 private String _password; 51 52 private URL _proxyURL; 53 private String _proxyUsername; 54 private String _proxyPassword; 55 private String _proxyType; 56 57 private int _responseCode; 58 private boolean _hadSentAuthorization = false; 59 private boolean _hadSentProxyAuthorization = false; 60 61 private String _authorization; 62 private String _proxyAuthorization; 63 64 public HttpConnection(URL url, 65 String username, 66 String password) 67 throws IOException 68 { 69 _URL = url; 70 _username = username; 71 _password = password; 72 73 init(); 74 } 75 76 public HttpConnection(URL url, 77 String username, 78 String password, 79 URL proxyURL, 80 String proxyUsername, 81 String proxyPassword, 82 String proxyType) 83 throws IOException 84 { 85 _URL = url; 86 _proxyURL = proxyURL; 87 _proxyType = proxyType; 88 89 _username = username; 90 _password = password; 91 _proxyUsername = proxyUsername; 92 _proxyPassword = proxyPassword; 93 94 init(); 95 } 96 97 public void setConnectTimeout(int time) 98 { 99 _conn.setConnectTimeout(time); 100 } 101 102 public void setDoOutput(boolean doOutput) 103 { 104 _conn.setDoOutput(doOutput); 105 } 106 107 public void setInstanceFollowRedirects(boolean isToFollowRedirects) 108 { 109 _conn.setInstanceFollowRedirects(isToFollowRedirects); 110 } 111 112 public void setReadTimeout(int time) 113 { 114 _conn.setReadTimeout(time); 115 } 116 117 public void setRequestMethod(String method) 118 throws ProtocolException 119 { 120 _conn.setRequestMethod(method); 121 } 122 123 public void setRequestProperty(String key, String value) 124 { 125 _conn.setRequestProperty(key, value); 126 } 127 128 private void init() 129 throws IOException 130 { 131 Proxy proxy = Proxy.NO_PROXY; 132 133 if (_proxyURL != null) { 134 InetSocketAddress address 135 = new InetSocketAddress(_proxyURL.getHost(), _proxyURL.getPort()); 136 137 proxy = new Proxy(Proxy.Type.valueOf(_proxyType), address); 138 } 139 140 _conn = (HttpURLConnection)_URL.openConnection(proxy); 141 } 142 143 146 public void authenticate() 147 throws ConnectException, ProtocolException, SocketTimeoutException, 148 IOException 149 { 150 Proxy proxy = Proxy.NO_PROXY; 151 152 if (_proxyURL != null) { 153 InetSocketAddress address 154 = new InetSocketAddress(_proxyURL.getHost(), _proxyURL.getPort()); 155 156 proxy = new Proxy(Proxy.Type.valueOf(_proxyType), address); 157 } 158 159 HttpURLConnection headConn = (HttpURLConnection)_URL.openConnection(proxy); 160 headConn.setRequestMethod("HEAD"); 161 162 if (_proxyAuthorization != null) 163 headConn.setRequestProperty("Proxy-Authorization", _proxyAuthorization); 164 165 if (_authorization != null) 166 headConn.setRequestProperty("Authorization", _authorization); 167 168 headConn.connect(); 169 170 int responseCode = headConn.getResponseCode(); 171 172 if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH 173 && _proxyAuthorization == null) 174 { 175 String header = headConn.getHeaderField("Proxy-Authenticate"); 176 177 _proxyAuthorization = getAuthorization(_URL, 178 _conn.getRequestMethod(), 179 header, 180 "Proxy-Authorization", 181 _proxyUsername, 182 _proxyPassword); 183 authenticate(); 184 } 185 else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED 186 && _authorization == null) 187 { 188 String header = headConn.getHeaderField("WWW-Authenticate"); 189 190 _authorization = getAuthorization(_URL, 191 _conn.getRequestMethod(), 192 header, 193 "Authorization", 194 _username, 195 _password); 196 authenticate(); 197 } 198 199 headConn.disconnect(); 200 } 201 202 205 public void connect() 206 throws ConnectException, ProtocolException, SocketTimeoutException, 207 IOException 208 { 209 if (_username != null || _proxyUsername != null) 210 authenticate(); 211 212 if (_proxyAuthorization != null) 213 _conn.setRequestProperty("Proxy-Authorization", _proxyAuthorization); 214 if (_authorization != null) 215 _conn.setRequestProperty("Authorization", _authorization); 216 217 _conn.connect(); 218 } 219 220 223 private final String getAuthorization(URL url, 224 String requestMethod, 225 String header, 226 String clientField, 227 String username, 228 String password) 229 throws ConnectException, SocketTimeoutException, IOException 230 { 231 if (username == null || password == null) 232 return ""; 233 234 String uri = url.getFile(); 235 if (uri.length() == 0) 236 uri = "/"; 237 238 String auth = Authentication.getAuthorization(username, 239 password, 240 requestMethod, 241 uri, 242 header); 243 244 return auth; 245 } 246 247 public int getContentLength() 248 { 249 return _conn.getContentLength(); 250 } 251 252 public InputStream getErrorStream() 253 { 254 return _conn.getErrorStream(); 255 } 256 257 public String getHeaderField(String key) 258 { 259 return _conn.getHeaderField(key); 260 } 261 262 public String getHeaderField(int i) 263 { 264 return _conn.getHeaderField(i); 265 } 266 267 public String getHeaderFieldKey(int i) 268 { 269 return _conn.getHeaderFieldKey(i); 270 } 271 272 public InputStream getInputStream() 273 throws IOException 274 { 275 return _conn.getInputStream(); 276 } 277 278 public OutputStream getOutputStream() 279 throws IOException 280 { 281 return _conn.getOutputStream(); 282 } 283 284 public int getResponseCode() 285 { 286 try { 287 return _conn.getResponseCode(); 288 } 289 catch (IOException e) { 290 throw new QuercusModuleException(e); 291 } 292 } 293 294 public void disconnect() 295 { 296 close(); 297 } 298 299 public void close() 300 { 301 if (_conn != null) 302 _conn.disconnect(); 303 } 304 } 305 | Popular Tags |