295 "Authorization"
296 };
297
298 // also exclude system cookies when any might be set
299 private static final String[] EXCLUDE_HEADERS2= {
300 "Proxy-Authorization",
301 "Authorization",
302 "Cookie",
303 "Cookie2"
304 };
305
306 protected HttpClient http;
307 protected Handler handler;
308 protected Proxy instProxy;
309 protected volatile Authenticator authenticator;
310 protected volatile String authenticatorKey;
311
312 private CookieHandler cookieHandler;
313 private final ResponseCache cacheHandler;
314
315 // the cached response, and cached response headers and body
316 protected CacheResponse cachedResponse;
317 private MessageHeader cachedHeaders;
318 private InputStream cachedInputStream;
319
320 /* output stream to server */
321 protected PrintStream ps = null;
322
323
324 /* buffered error stream */
325 private InputStream errorStream = null;
326
327 /* User set Cookies */
328 private boolean setUserCookies = true;
329 private String userCookies = null;
330 private String userCookies2 = null;
331
332 /* We only have a single static authenticator for now.
333 * REMIND: backwards compatibility with JDK 1.1. Should be
334 * eliminated for JDK 2.0.
335 */
336 @Deprecated
337 private static HttpAuthenticator defaultAuth;
338
339 /* all the headers we send
340 * NOTE: do *NOT* dump out the content of 'requests' in the
341 * output or stacktrace since it may contain security-sensitive
342 * headers such as those defined in EXCLUDE_HEADERS.
343 */
1223 http.setReadTimeout(readTimeout);
1224 } else {
1225 // make sure to construct new connection if first
1226 // attempt failed
1227 http = getNewHttpClient(url, null, connectTimeout, false);
1228 http.setReadTimeout(readTimeout);
1229 }
1230 }
1231 } else {
1232 if (!failedOnce) {
1233 http = getNewHttpClient(url, instProxy, connectTimeout);
1234 http.setReadTimeout(readTimeout);
1235 } else {
1236 // make sure to construct new connection if first
1237 // attempt failed
1238 http = getNewHttpClient(url, instProxy, connectTimeout, false);
1239 http.setReadTimeout(readTimeout);
1240 }
1241 }
1242
1243 ps = (PrintStream)http.getOutputStream();
1244 } catch (IOException e) {
1245 throw e;
1246 }
1247 // constructor to HTTP client calls openserver
1248 connected = true;
1249 }
1250
1251 // subclass HttpsClient will overwrite & return an instance of HttpsClient
1252 protected HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout)
1253 throws IOException {
1254 return HttpClient.New(url, p, connectTimeout, this);
1255 }
1256
1257 // subclass HttpsClient will overwrite & return an instance of HttpsClient
1258 protected HttpClient getNewHttpClient(URL url, Proxy p,
1259 int connectTimeout, boolean useCache)
1260 throws IOException {
1261 return HttpClient.New(url, p, connectTimeout, useCache, this);
1262 }
2900 disconnectInternal();
2901 return;
2902 }
2903 try {
2904 if (is instanceof MeteredStream) {
2905 is.close();
2906 }
2907 } catch (IOException e) { }
2908 }
2909 responseCode = -1;
2910 responses = new MessageHeader();
2911 connected = false;
2912 }
2913
2914 /**
2915 * Disconnect from the web server at the first 401 error. Do not
2916 * disconnect when using a proxy, a good proxy should have already
2917 * closed the connection to the web server.
2918 */
2919 private void disconnectWeb() throws IOException {
2920 if (usingProxy() && http.isKeepingAlive()) {
2921 responseCode = -1;
2922 // clean up, particularly, skip the content part
2923 // of a 401 error response
2924 reset();
2925 } else {
2926 disconnectInternal();
2927 }
2928 }
2929
2930 /**
2931 * Disconnect from the server (for internal use)
2932 */
2933 private void disconnectInternal() {
2934 responseCode = -1;
2935 inputStream = null;
2936 if (pi != null) {
2937 pi.finishTracking();
2938 pi = null;
2939 }
2940 if (http != null) {
3003
3004 } else {
3005 // We are deliberatly being disconnected so HttpClient
3006 // should not try to resend the request no matter what stage
3007 // of the connection we are in.
3008 http.setDoNotRetry(true);
3009
3010 http.closeServer();
3011 }
3012
3013 // poster = null;
3014 http = null;
3015 connected = false;
3016 }
3017 cachedInputStream = null;
3018 if (cachedHeaders != null) {
3019 cachedHeaders.reset();
3020 }
3021 }
3022
3023 public boolean usingProxy() {
3024 if (http != null) {
3025 return (http.getProxyHostUsed() != null);
3026 }
3027 return false;
3028 }
3029
3030 // constant strings represent set-cookie header names
3031 private static final String SET_COOKIE = "set-cookie";
3032 private static final String SET_COOKIE2 = "set-cookie2";
3033
3034 /**
3035 * Returns a filtered version of the given headers value.
3036 *
3037 * Note: The implementation currently only filters out HttpOnly cookies
3038 * from Set-Cookie and Set-Cookie2 headers.
3039 */
3040 private String filterHeaderField(String name, String value) {
3041 if (value == null)
3042 return null;
3043
3044 if (SET_COOKIE.equalsIgnoreCase(name) ||
3045 SET_COOKIE2.equalsIgnoreCase(name)) {
3046
3047 // Filtering only if there is a cookie handler. [Assumption: the
3048 // cookie handler will store/retrieve the HttpOnly cookies]
3049 if (cookieHandler == null || value.isEmpty())
|
295 "Authorization"
296 };
297
298 // also exclude system cookies when any might be set
299 private static final String[] EXCLUDE_HEADERS2= {
300 "Proxy-Authorization",
301 "Authorization",
302 "Cookie",
303 "Cookie2"
304 };
305
306 protected HttpClient http;
307 protected Handler handler;
308 protected Proxy instProxy;
309 protected volatile Authenticator authenticator;
310 protected volatile String authenticatorKey;
311
312 private CookieHandler cookieHandler;
313 private final ResponseCache cacheHandler;
314
315 private volatile boolean usingProxy;
316
317 // the cached response, and cached response headers and body
318 protected CacheResponse cachedResponse;
319 private MessageHeader cachedHeaders;
320 private InputStream cachedInputStream;
321
322 /* output stream to server */
323 protected PrintStream ps = null;
324
325 /* buffered error stream */
326 private InputStream errorStream = null;
327
328 /* User set Cookies */
329 private boolean setUserCookies = true;
330 private String userCookies = null;
331 private String userCookies2 = null;
332
333 /* We only have a single static authenticator for now.
334 * REMIND: backwards compatibility with JDK 1.1. Should be
335 * eliminated for JDK 2.0.
336 */
337 @Deprecated
338 private static HttpAuthenticator defaultAuth;
339
340 /* all the headers we send
341 * NOTE: do *NOT* dump out the content of 'requests' in the
342 * output or stacktrace since it may contain security-sensitive
343 * headers such as those defined in EXCLUDE_HEADERS.
344 */
1224 http.setReadTimeout(readTimeout);
1225 } else {
1226 // make sure to construct new connection if first
1227 // attempt failed
1228 http = getNewHttpClient(url, null, connectTimeout, false);
1229 http.setReadTimeout(readTimeout);
1230 }
1231 }
1232 } else {
1233 if (!failedOnce) {
1234 http = getNewHttpClient(url, instProxy, connectTimeout);
1235 http.setReadTimeout(readTimeout);
1236 } else {
1237 // make sure to construct new connection if first
1238 // attempt failed
1239 http = getNewHttpClient(url, instProxy, connectTimeout, false);
1240 http.setReadTimeout(readTimeout);
1241 }
1242 }
1243
1244 usingProxy = usingProxy || usingProxyInternal();
1245 ps = (PrintStream)http.getOutputStream();
1246 } catch (IOException e) {
1247 throw e;
1248 }
1249 // constructor to HTTP client calls openserver
1250 connected = true;
1251 }
1252
1253 // subclass HttpsClient will overwrite & return an instance of HttpsClient
1254 protected HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout)
1255 throws IOException {
1256 return HttpClient.New(url, p, connectTimeout, this);
1257 }
1258
1259 // subclass HttpsClient will overwrite & return an instance of HttpsClient
1260 protected HttpClient getNewHttpClient(URL url, Proxy p,
1261 int connectTimeout, boolean useCache)
1262 throws IOException {
1263 return HttpClient.New(url, p, connectTimeout, useCache, this);
1264 }
2902 disconnectInternal();
2903 return;
2904 }
2905 try {
2906 if (is instanceof MeteredStream) {
2907 is.close();
2908 }
2909 } catch (IOException e) { }
2910 }
2911 responseCode = -1;
2912 responses = new MessageHeader();
2913 connected = false;
2914 }
2915
2916 /**
2917 * Disconnect from the web server at the first 401 error. Do not
2918 * disconnect when using a proxy, a good proxy should have already
2919 * closed the connection to the web server.
2920 */
2921 private void disconnectWeb() throws IOException {
2922 if (usingProxyInternal() && http.isKeepingAlive()) {
2923 responseCode = -1;
2924 // clean up, particularly, skip the content part
2925 // of a 401 error response
2926 reset();
2927 } else {
2928 disconnectInternal();
2929 }
2930 }
2931
2932 /**
2933 * Disconnect from the server (for internal use)
2934 */
2935 private void disconnectInternal() {
2936 responseCode = -1;
2937 inputStream = null;
2938 if (pi != null) {
2939 pi.finishTracking();
2940 pi = null;
2941 }
2942 if (http != null) {
3005
3006 } else {
3007 // We are deliberatly being disconnected so HttpClient
3008 // should not try to resend the request no matter what stage
3009 // of the connection we are in.
3010 http.setDoNotRetry(true);
3011
3012 http.closeServer();
3013 }
3014
3015 // poster = null;
3016 http = null;
3017 connected = false;
3018 }
3019 cachedInputStream = null;
3020 if (cachedHeaders != null) {
3021 cachedHeaders.reset();
3022 }
3023 }
3024
3025 /**
3026 * Returns true only if the established connection is using a proxy
3027 */
3028 boolean usingProxyInternal() {
3029 if (http != null) {
3030 return (http.getProxyHostUsed() != null);
3031 }
3032 return false;
3033 }
3034
3035 /**
3036 * Returns true if the established connection is using a proxy
3037 * or if a proxy is specified for the inactive connection
3038 */
3039 @Override
3040 public boolean usingProxy() {
3041 if (usingProxy || usingProxyInternal())
3042 return true;
3043
3044 if (instProxy != null)
3045 return instProxy.type().equals(Proxy.Type.HTTP);
3046
3047 return false;
3048 }
3049
3050 // constant strings represent set-cookie header names
3051 private static final String SET_COOKIE = "set-cookie";
3052 private static final String SET_COOKIE2 = "set-cookie2";
3053
3054 /**
3055 * Returns a filtered version of the given headers value.
3056 *
3057 * Note: The implementation currently only filters out HttpOnly cookies
3058 * from Set-Cookie and Set-Cookie2 headers.
3059 */
3060 private String filterHeaderField(String name, String value) {
3061 if (value == null)
3062 return null;
3063
3064 if (SET_COOKIE.equalsIgnoreCase(name) ||
3065 SET_COOKIE2.equalsIgnoreCase(name)) {
3066
3067 // Filtering only if there is a cookie handler. [Assumption: the
3068 // cookie handler will store/retrieve the HttpOnly cookies]
3069 if (cookieHandler == null || value.isEmpty())
|