253 * Returns info for the URL, for an HTTP server auth. Used when we 254 * don't yet know the realm 255 * (i.e. when we're preemptively setting the auth). 256 */ 257 static AuthenticationInfo getServerAuth(URL url) { 258 int port = url.getPort(); 259 if (port == -1) { 260 port = url.getDefaultPort(); 261 } 262 String key = SERVER_AUTHENTICATION + ":" + url.getProtocol().toLowerCase() 263 + ":" + url.getHost().toLowerCase() + ":" + port; 264 return getAuth(key, url); 265 } 266 267 /** 268 * Returns info for the URL, for an HTTP server auth. Used when we 269 * do know the realm (i.e. when we're responding to a challenge). 270 * In this case we do not use the path because the protection space 271 * is identified by the host:port:realm only 272 */ 273 static AuthenticationInfo getServerAuth(URL url, String realm, AuthScheme scheme) { 274 int port = url.getPort(); 275 if (port == -1) { 276 port = url.getDefaultPort(); 277 } 278 String key = SERVER_AUTHENTICATION + ":" + scheme + ":" + url.getProtocol().toLowerCase() 279 + ":" + url.getHost().toLowerCase() + ":" + port + ":" + realm; 280 AuthenticationInfo cached = getAuth(key, null); 281 if ((cached == null) && requestIsInProgress (key)) { 282 /* check the cache again, it might contain an entry */ 283 cached = getAuth(key, null); 284 } 285 return cached; 286 } 287 288 289 /** 290 * Return the AuthenticationInfo object from the cache if it's path is 291 * a substring of the supplied URLs path. 292 */ 293 static AuthenticationInfo getAuth(String key, URL url) { 294 if (url == null) { 295 return (AuthenticationInfo)cache.get (key, null); 296 } else { 297 return (AuthenticationInfo)cache.get (key, url.getPath()); 298 } 299 } 300 301 /** 302 * Returns a firewall authentication, for the given host/port. Used 303 * for preemptive header-setting. Note, the protocol field is always 304 * blank for proxies. 305 */ 306 static AuthenticationInfo getProxyAuth(String host, int port) { 307 String key = PROXY_AUTHENTICATION + "::" + host.toLowerCase() + ":" + port; 308 AuthenticationInfo result = (AuthenticationInfo) cache.get(key, null); 309 return result; 310 } 311 312 /** 313 * Returns a firewall authentication, for the given host/port and realm. 314 * Used in response to a challenge. Note, the protocol field is always 315 * blank for proxies. 316 */ 317 static AuthenticationInfo getProxyAuth(String host, int port, String realm, AuthScheme scheme) { 318 String key = PROXY_AUTHENTICATION + ":" + scheme + "::" + host.toLowerCase() 319 + ":" + port + ":" + realm; 320 AuthenticationInfo cached = (AuthenticationInfo) cache.get(key, null); 321 if ((cached == null) && requestIsInProgress (key)) { 322 /* check the cache again, it might contain an entry */ 323 cached = (AuthenticationInfo) cache.get(key, null); 324 } 325 return cached; 326 } 327 328 329 /** 330 * Add this authentication to the cache 331 */ 332 void addToCache() { 333 cache.put (cacheKey(true), this); 334 if (supportsPreemptiveAuthorization()) { 335 cache.put (cacheKey(false), this); 336 } 337 endAuthRequest(); 338 } 339 340 void endAuthRequest () { 341 if (!serializeAuth) { 342 return; 343 } 344 synchronized (requests) { 345 requestCompleted (cacheKey(true)); 346 } 347 } 348 349 /** 350 * Remove this authentication from the cache 351 */ 352 void removeFromCache() { 353 cache.remove(cacheKey(true), this); 354 if (supportsPreemptiveAuthorization()) { 355 cache.remove(cacheKey(false), this); 356 } 357 } 358 359 /** 360 * @return true if this authentication supports preemptive authorization 361 */ 362 public abstract boolean supportsPreemptiveAuthorization(); 363 364 /** 365 * @return the name of the HTTP header this authentication wants set. | 253 * Returns info for the URL, for an HTTP server auth. Used when we 254 * don't yet know the realm 255 * (i.e. when we're preemptively setting the auth). 256 */ 257 static AuthenticationInfo getServerAuth(URL url) { 258 int port = url.getPort(); 259 if (port == -1) { 260 port = url.getDefaultPort(); 261 } 262 String key = SERVER_AUTHENTICATION + ":" + url.getProtocol().toLowerCase() 263 + ":" + url.getHost().toLowerCase() + ":" + port; 264 return getAuth(key, url); 265 } 266 267 /** 268 * Returns info for the URL, for an HTTP server auth. Used when we 269 * do know the realm (i.e. when we're responding to a challenge). 270 * In this case we do not use the path because the protection space 271 * is identified by the host:port:realm only 272 */ 273 static String getServerAuthKey(URL url, String realm, AuthScheme scheme) { 274 int port = url.getPort(); 275 if (port == -1) { 276 port = url.getDefaultPort(); 277 } 278 String key = SERVER_AUTHENTICATION + ":" + scheme + ":" + url.getProtocol().toLowerCase() 279 + ":" + url.getHost().toLowerCase() + ":" + port + ":" + realm; 280 return key; 281 } 282 283 static AuthenticationInfo getServerAuth(String key) { 284 AuthenticationInfo cached = getAuth(key, null); 285 if ((cached == null) && requestIsInProgress (key)) { 286 /* check the cache again, it might contain an entry */ 287 cached = getAuth(key, null); 288 } 289 return cached; 290 } 291 292 293 /** 294 * Return the AuthenticationInfo object from the cache if it's path is 295 * a substring of the supplied URLs path. 296 */ 297 static AuthenticationInfo getAuth(String key, URL url) { 298 if (url == null) { 299 return (AuthenticationInfo)cache.get (key, null); 300 } else { 301 return (AuthenticationInfo)cache.get (key, url.getPath()); 302 } 303 } 304 305 /** 306 * Returns a firewall authentication, for the given host/port. Used 307 * for preemptive header-setting. Note, the protocol field is always 308 * blank for proxies. 309 */ 310 static AuthenticationInfo getProxyAuth(String host, int port) { 311 String key = PROXY_AUTHENTICATION + "::" + host.toLowerCase() + ":" + port; 312 AuthenticationInfo result = (AuthenticationInfo) cache.get(key, null); 313 return result; 314 } 315 316 /** 317 * Returns a firewall authentication, for the given host/port and realm. 318 * Used in response to a challenge. Note, the protocol field is always 319 * blank for proxies. 320 */ 321 static String getProxyAuthKey(String host, int port, String realm, AuthScheme scheme) { 322 String key = PROXY_AUTHENTICATION + ":" + scheme + "::" + host.toLowerCase() 323 + ":" + port + ":" + realm; 324 return key; 325 } 326 327 static AuthenticationInfo getProxyAuth(String key) { 328 AuthenticationInfo cached = (AuthenticationInfo) cache.get(key, null); 329 if ((cached == null) && requestIsInProgress (key)) { 330 /* check the cache again, it might contain an entry */ 331 cached = (AuthenticationInfo) cache.get(key, null); 332 } 333 return cached; 334 } 335 336 337 /** 338 * Add this authentication to the cache 339 */ 340 void addToCache() { 341 String key = cacheKey(true); 342 cache.put(key, this); 343 if (supportsPreemptiveAuthorization()) { 344 cache.put(cacheKey(false), this); 345 } 346 endAuthRequest(key); 347 } 348 349 static void endAuthRequest (String key) { 350 if (!serializeAuth) { 351 return; 352 } 353 synchronized (requests) { 354 requestCompleted(key); 355 } 356 } 357 358 /** 359 * Remove this authentication from the cache 360 */ 361 void removeFromCache() { 362 cache.remove(cacheKey(true), this); 363 if (supportsPreemptiveAuthorization()) { 364 cache.remove(cacheKey(false), this); 365 } 366 } 367 368 /** 369 * @return true if this authentication supports preemptive authorization 370 */ 371 public abstract boolean supportsPreemptiveAuthorization(); 372 373 /** 374 * @return the name of the HTTP header this authentication wants set. |