提交 452c37e7 编写于 作者: J jccollet

6856856: NPE in HTTP protocol handler logging

Summary: Fixed the NPE and Moved the java.util.logging dependency to a single class and used reflection to make it a soft one.
Reviewed-by: chegar
上级 1d27b05f
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
package sun.net.www.http; package sun.net.www.http;
import java.io.*; import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -60,6 +62,76 @@ public class HttpCapture { ...@@ -60,6 +62,76 @@ public class HttpCapture {
private static boolean initialized = false; private static boolean initialized = false;
private static volatile ArrayList<Pattern> patterns = null; private static volatile ArrayList<Pattern> patterns = null;
private static volatile ArrayList<String> capFiles = null; private static volatile ArrayList<String> capFiles = null;
/* Logging is done in an ugly way so that it does not require the presence
* the java.util.logging package. If the Logger class is not available, then
* logging is turned off. This is for helping the modularization effort.
*/
private static Object logger = null;
private static boolean logging = false;
static {
Class cl;
try {
cl = Class.forName("java.util.logging.Logger");
} catch (ClassNotFoundException ex) {
cl = null;
}
if (cl != null) {
try {
Method m = cl.getMethod("getLogger", String.class);
logger = m.invoke(null, "sun.net.www.protocol.http.HttpURLConnection");
logging = true;
} catch (NoSuchMethodException noSuchMethodException) {
} catch (SecurityException securityException) {
} catch (IllegalAccessException illegalAccessException) {
} catch (IllegalArgumentException illegalArgumentException) {
} catch (InvocationTargetException invocationTargetException) {
}
}
}
public static void fine(String s) {
if (logging) {
((Logger)logger).fine(s);
}
}
public static void finer(String s) {
if (logging) {
((Logger)logger).finer(s);
}
}
public static void finest(String s) {
if (logging) {
((Logger)logger).finest(s);
}
}
public static void severe(String s) {
if (logging) {
((Logger)logger).finest(s);
}
}
public static void info(String s) {
if (logging) {
((Logger)logger).info(s);
}
}
public static void warning(String s) {
if (logging) {
((Logger)logger).warning(s);
}
}
public static boolean isLoggable(String level) {
if (!logging) {
return false;
}
return ((Logger)logger).isLoggable(Level.parse(level));
}
private static synchronized void init() { private static synchronized void init() {
initialized = true; initialized = true;
......
...@@ -28,8 +28,6 @@ package sun.net.www.http; ...@@ -28,8 +28,6 @@ package sun.net.www.http;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.Locale; import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.net.NetworkClient; import sun.net.NetworkClient;
import sun.net.ProgressSource; import sun.net.ProgressSource;
import sun.net.www.MessageHeader; import sun.net.www.MessageHeader;
...@@ -66,10 +64,6 @@ public class HttpClient extends NetworkClient { ...@@ -66,10 +64,6 @@ public class HttpClient extends NetworkClient {
/** Default port number for http daemons. REMIND: make these private */ /** Default port number for http daemons. REMIND: make these private */
static final int httpPortNumber = 80; static final int httpPortNumber = 80;
// Use same logger as HttpURLConnection since we want to combine both event
// streams into one single HTTP log
private static Logger logger = Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
/** return default port number (subclasses may override) */ /** return default port number (subclasses may override) */
protected int getDefaultPort () { return httpPortNumber; } protected int getDefaultPort () { return httpPortNumber; }
...@@ -810,8 +804,8 @@ public class HttpClient extends NetworkClient { ...@@ -810,8 +804,8 @@ public class HttpClient extends NetworkClient {
if (isKeepingAlive()) { if (isKeepingAlive()) {
// Wrap KeepAliveStream if keep alive is enabled. // Wrap KeepAliveStream if keep alive is enabled.
if (logger.isLoggable(Level.FINEST)) { if (HttpCapture.isLoggable("FINEST")) {
logger.finest("KeepAlive stream used: " + url); HttpCapture.finest("KeepAlive stream used: " + url);
} }
serverInput = new KeepAliveStream(serverInput, pi, cl, this); serverInput = new KeepAliveStream(serverInput, pi, cl, this);
failedOnce = false; failedOnce = false;
......
...@@ -49,8 +49,7 @@ public class HttpLogFormatter extends java.util.logging.SimpleFormatter { ...@@ -49,8 +49,7 @@ public class HttpLogFormatter extends java.util.logging.SimpleFormatter {
@Override @Override
public String format(LogRecord record) { public String format(LogRecord record) {
if (!"sun.net.www.protocol.http.HttpURLConnection".equalsIgnoreCase(record.getSourceClassName()) if (!"sun.net.www.http.HttpCapture".equalsIgnoreCase(record.getSourceClassName())) {
&& !"sun.net.www.http.HttpClient".equalsIgnoreCase(record.getSourceClassName())) {
// Don't change format for stuff that doesn't concern us // Don't change format for stuff that doesn't concern us
return super.format(record); return super.format(record);
} }
......
...@@ -51,14 +51,13 @@ import java.util.List; ...@@ -51,14 +51,13 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Iterator; import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.net.*; import sun.net.*;
import sun.net.www.*; import sun.net.www.*;
import sun.net.www.http.HttpClient; import sun.net.www.http.HttpClient;
import sun.net.www.http.PosterOutputStream; import sun.net.www.http.PosterOutputStream;
import sun.net.www.http.ChunkedInputStream; import sun.net.www.http.ChunkedInputStream;
import sun.net.www.http.ChunkedOutputStream; import sun.net.www.http.ChunkedOutputStream;
import sun.net.www.http.HttpCapture;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.TimeZone; import java.util.TimeZone;
import java.net.MalformedURLException; import java.net.MalformedURLException;
...@@ -71,8 +70,6 @@ import java.nio.ByteBuffer; ...@@ -71,8 +70,6 @@ import java.nio.ByteBuffer;
public class HttpURLConnection extends java.net.HttpURLConnection { public class HttpURLConnection extends java.net.HttpURLConnection {
private static Logger logger = Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
static String HTTP_CONNECT = "CONNECT"; static String HTTP_CONNECT = "CONNECT";
static final String version; static final String version;
...@@ -304,14 +301,14 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -304,14 +301,14 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
return java.security.AccessController.doPrivileged( return java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<PasswordAuthentication>() { new java.security.PrivilegedAction<PasswordAuthentication>() {
public PasswordAuthentication run() { public PasswordAuthentication run() {
if (logger.isLoggable(Level.FINEST)) { if (HttpCapture.isLoggable("FINEST")) {
logger.finest("Requesting Authentication: host =" + host + " url = " + url); HttpCapture.finest("Requesting Authentication: host =" + host + " url = " + url);
} }
PasswordAuthentication pass = Authenticator.requestPasswordAuthentication( PasswordAuthentication pass = Authenticator.requestPasswordAuthentication(
host, addr, port, protocol, host, addr, port, protocol,
prompt, scheme, url, authType); prompt, scheme, url, authType);
if (pass != null && logger.isLoggable(Level.FINEST)) { if (HttpCapture.isLoggable("FINEST")) {
logger.finest("Authentication returned: " + pass.toString()); HttpCapture.finest("Authentication returned: " + (pass != null ? pass.toString() : "null"));
} }
return pass; return pass;
} }
...@@ -466,8 +463,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -466,8 +463,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
setRequests=true; setRequests=true;
} }
if (logger.isLoggable(Level.FINE)) { if (HttpCapture.isLoggable("FINE")) {
logger.fine(requests.toString()); HttpCapture.fine(requests.toString());
} }
http.writeRequests(requests, poster); http.writeRequests(requests, poster);
if (ps.checkError()) { if (ps.checkError()) {
...@@ -723,11 +720,9 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -723,11 +720,9 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
&& !(cachedResponse instanceof SecureCacheResponse)) { && !(cachedResponse instanceof SecureCacheResponse)) {
cachedResponse = null; cachedResponse = null;
} }
if (logger.isLoggable(Level.FINEST)) { if (HttpCapture.isLoggable("FINEST")) {
logger.finest("Cache Request for " + uri + " / " + getRequestMethod()); HttpCapture.finest("Cache Request for " + uri + " / " + getRequestMethod());
if (cachedResponse != null) { HttpCapture.finest("From cache: " + (cachedResponse != null ? cachedResponse.toString() : "null"));
logger.finest("From cache: "+cachedResponse.toString());
}
} }
if (cachedResponse != null) { if (cachedResponse != null) {
cachedHeaders = mapToMessageHeader(cachedResponse.getHeaders()); cachedHeaders = mapToMessageHeader(cachedResponse.getHeaders());
...@@ -766,8 +761,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -766,8 +761,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
}); });
if (sel != null) { if (sel != null) {
URI uri = sun.net.www.ParseUtil.toURI(url); URI uri = sun.net.www.ParseUtil.toURI(url);
if (logger.isLoggable(Level.FINEST)) { if (HttpCapture.isLoggable("FINEST")) {
logger.finest("ProxySelector Request for " + uri); HttpCapture.finest("ProxySelector Request for " + uri);
} }
Iterator<Proxy> it = sel.select(uri).iterator(); Iterator<Proxy> it = sel.select(uri).iterator();
Proxy p; Proxy p;
...@@ -783,9 +778,9 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -783,9 +778,9 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
http = getNewHttpClient(url, p, connectTimeout, false); http = getNewHttpClient(url, p, connectTimeout, false);
http.setReadTimeout(readTimeout); http.setReadTimeout(readTimeout);
} }
if (logger.isLoggable(Level.FINEST)) { if (HttpCapture.isLoggable("FINEST")) {
if (p != null) { if (p != null) {
logger.finest("Proxy used: " + p.toString()); HttpCapture.finest("Proxy used: " + p.toString());
} }
} }
break; break;
...@@ -1015,15 +1010,15 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -1015,15 +1010,15 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
URI uri = ParseUtil.toURI(url); URI uri = ParseUtil.toURI(url);
if (uri != null) { if (uri != null) {
if (logger.isLoggable(Level.FINEST)) { if (HttpCapture.isLoggable("FINEST")) {
logger.finest("CookieHandler request for " + uri); HttpCapture.finest("CookieHandler request for " + uri);
} }
Map<String, List<String>> cookies Map<String, List<String>> cookies
= cookieHandler.get( = cookieHandler.get(
uri, requests.getHeaders(EXCLUDE_HEADERS)); uri, requests.getHeaders(EXCLUDE_HEADERS));
if (!cookies.isEmpty()) { if (!cookies.isEmpty()) {
if (logger.isLoggable(Level.FINEST)) { if (HttpCapture.isLoggable("FINEST")) {
logger.finest("Cookies retrieved: " + cookies.toString()); HttpCapture.finest("Cookies retrieved: " + cookies.toString());
} }
for (Map.Entry<String, List<String>> entry : for (Map.Entry<String, List<String>> entry :
cookies.entrySet()) { cookies.entrySet()) {
...@@ -1154,8 +1149,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -1154,8 +1149,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
writeRequests(); writeRequests();
} }
http.parseHTTP(responses, pi, this); http.parseHTTP(responses, pi, this);
if (logger.isLoggable(Level.FINE)) { if (HttpCapture.isLoggable("FINE")) {
logger.fine(responses.toString()); HttpCapture.fine(responses.toString());
} }
inputStream = http.getInputStream(); inputStream = http.getInputStream();
...@@ -1599,8 +1594,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -1599,8 +1594,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
http.parseHTTP(responses, null, this); http.parseHTTP(responses, null, this);
/* Log the response to the CONNECT */ /* Log the response to the CONNECT */
if (logger.isLoggable(Level.FINE)) { if (HttpCapture.isLoggable("FINE")) {
logger.fine(responses.toString()); HttpCapture.fine(responses.toString());
} }
statusLine = responses.getValue(0); statusLine = responses.getValue(0);
...@@ -1727,8 +1722,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -1727,8 +1722,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
setPreemptiveProxyAuthentication(requests); setPreemptiveProxyAuthentication(requests);
/* Log the CONNECT request */ /* Log the CONNECT request */
if (logger.isLoggable(Level.FINE)) { if (HttpCapture.isLoggable("FINE")) {
logger.fine(requests.toString()); HttpCapture.fine(requests.toString());
} }
http.writeRequests(requests, null); http.writeRequests(requests, null);
...@@ -1872,8 +1867,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -1872,8 +1867,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
} }
} }
} }
if (logger.isLoggable(Level.FINER)) { if (HttpCapture.isLoggable("FINER")) {
logger.finer("Proxy Authentication for " + authhdr.toString() +" returned " + ret.toString()); HttpCapture.finer("Proxy Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null"));
} }
return ret; return ret;
} }
...@@ -2002,8 +1997,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -2002,8 +1997,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
} }
} }
} }
if (logger.isLoggable(Level.FINER)) { if (HttpCapture.isLoggable("FINER")) {
logger.finer("Server Authentication for " + authhdr.toString() +" returned " + ret.toString()); HttpCapture.finer("Server Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null"));
} }
return ret; return ret;
} }
...@@ -2078,8 +2073,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -2078,8 +2073,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
if (streaming()) { if (streaming()) {
throw new HttpRetryException (RETRY_MSG3, stat, loc); throw new HttpRetryException (RETRY_MSG3, stat, loc);
} }
if (logger.isLoggable(Level.FINE)) { if (HttpCapture.isLoggable("FINE")) {
logger.fine("Redirected from " + url + " to " + locUrl); HttpCapture.fine("Redirected from " + url + " to " + locUrl);
} }
// clear out old response headers!!!! // clear out old response headers!!!!
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册