use of android.net.util.Stopwatch in project platform_frameworks_base by android.
the class NetworkMonitor method sendHttpProbe.
/**
* Do a URL fetch on a known web server to see if we get the data we expect.
* @return a CaptivePortalProbeResult inferred from the HTTP response.
*/
@VisibleForTesting
protected CaptivePortalProbeResult sendHttpProbe(URL url, int probeType) {
HttpURLConnection urlConnection = null;
int httpResponseCode = 599;
String redirectUrl = null;
final Stopwatch probeTimer = new Stopwatch().start();
try {
urlConnection = (HttpURLConnection) mNetworkAgentInfo.network.openConnection(url);
urlConnection.setInstanceFollowRedirects(probeType == ValidationProbeEvent.PROBE_PAC);
urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
urlConnection.setUseCaches(false);
final String userAgent = getCaptivePortalUserAgent(mContext);
if (userAgent != null) {
urlConnection.setRequestProperty("User-Agent", userAgent);
}
// cannot read request header after connection
String requestHeader = urlConnection.getRequestProperties().toString();
// Time how long it takes to get a response to our request
long requestTimestamp = SystemClock.elapsedRealtime();
httpResponseCode = urlConnection.getResponseCode();
redirectUrl = urlConnection.getHeaderField("location");
// Time how long it takes to get a response to our request
long responseTimestamp = SystemClock.elapsedRealtime();
validationLog(ValidationProbeEvent.getProbeName(probeType) + " " + url + " time=" + (responseTimestamp - requestTimestamp) + "ms" + " ret=" + httpResponseCode + " request=" + requestHeader + " headers=" + urlConnection.getHeaderFields());
// proxy server.
if (httpResponseCode == 200) {
if (probeType == ValidationProbeEvent.PROBE_PAC) {
validationLog("PAC fetch 200 response interpreted as 204 response.");
httpResponseCode = 204;
} else if (urlConnection.getContentLengthLong() == 0) {
// Consider 200 response with "Content-length=0" to not be a captive portal.
// There's no point in considering this a captive portal as the user cannot
// sign-in to an empty page. Probably the result of a broken transparent proxy.
// See http://b/9972012.
validationLog("200 response with Content-length=0 interpreted as 204 response.");
httpResponseCode = 204;
} else if (urlConnection.getContentLengthLong() == -1) {
// response. Do not use available() as it is unreliable. See http://b/33498325.
if (urlConnection.getInputStream().read() == -1) {
validationLog("Empty 200 response interpreted as 204 response.");
httpResponseCode = 204;
}
}
}
} catch (IOException e) {
validationLog("Probably not a portal: exception " + e);
if (httpResponseCode == 599) {
// TODO: Ping gateway and DNS server and log results.
}
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
logValidationProbe(probeTimer.stop(), probeType, httpResponseCode);
return new CaptivePortalProbeResult(httpResponseCode, redirectUrl, url.toString());
}
use of android.net.util.Stopwatch in project platform_frameworks_base by android.
the class NetworkMonitor method sendDnsProbe.
/** Do a DNS resolution of the given server. */
private void sendDnsProbe(String host) {
if (TextUtils.isEmpty(host)) {
return;
}
final String name = ValidationProbeEvent.getProbeName(ValidationProbeEvent.PROBE_DNS);
final Stopwatch watch = new Stopwatch().start();
int result;
String connectInfo;
try {
InetAddress[] addresses = mNetworkAgentInfo.network.getAllByName(host);
result = ValidationProbeEvent.DNS_SUCCESS;
StringBuffer buffer = new StringBuffer(host).append("=");
for (InetAddress address : addresses) {
buffer.append(address.getHostAddress());
if (address != addresses[addresses.length - 1])
buffer.append(",");
}
connectInfo = buffer.toString();
} catch (UnknownHostException e) {
result = ValidationProbeEvent.DNS_FAILURE;
connectInfo = host;
}
final long latency = watch.stop();
String resultString = (ValidationProbeEvent.DNS_SUCCESS == result) ? "OK" : "FAIL";
validationLog(String.format("%s %s %dms, %s", name, resultString, latency, connectInfo));
logValidationProbe(latency, ValidationProbeEvent.PROBE_DNS, result);
}
use of android.net.util.Stopwatch in project android_frameworks_base by DirtyUnicorns.
the class NetworkMonitor method sendHttpProbe.
/**
* Do a URL fetch on a known web server to see if we get the data we expect.
* @return a CaptivePortalProbeResult inferred from the HTTP response.
*/
@VisibleForTesting
protected CaptivePortalProbeResult sendHttpProbe(URL url, int probeType) {
HttpURLConnection urlConnection = null;
int httpResponseCode = 599;
String redirectUrl = null;
final Stopwatch probeTimer = new Stopwatch().start();
try {
urlConnection = (HttpURLConnection) mNetworkAgentInfo.network.openConnection(url);
urlConnection.setInstanceFollowRedirects(probeType == ValidationProbeEvent.PROBE_PAC);
urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
urlConnection.setUseCaches(false);
final String userAgent = getCaptivePortalUserAgent(mContext);
if (userAgent != null) {
urlConnection.setRequestProperty("User-Agent", userAgent);
}
// Time how long it takes to get a response to our request
long requestTimestamp = SystemClock.elapsedRealtime();
httpResponseCode = urlConnection.getResponseCode();
redirectUrl = urlConnection.getHeaderField("location");
// Time how long it takes to get a response to our request
long responseTimestamp = SystemClock.elapsedRealtime();
validationLog(ValidationProbeEvent.getProbeName(probeType) + " " + url + " time=" + (responseTimestamp - requestTimestamp) + "ms" + " ret=" + httpResponseCode + " headers=" + urlConnection.getHeaderFields());
// proxy server.
if (httpResponseCode == 200) {
if (probeType == ValidationProbeEvent.PROBE_PAC) {
validationLog("PAC fetch 200 response interpreted as 204 response.");
httpResponseCode = 204;
} else if (urlConnection.getContentLengthLong() == 0) {
// Consider 200 response with "Content-length=0" to not be a captive portal.
// There's no point in considering this a captive portal as the user cannot
// sign-in to an empty page. Probably the result of a broken transparent proxy.
// See http://b/9972012.
validationLog("200 response with Content-length=0 interpreted as 204 response.");
httpResponseCode = 204;
} else if (urlConnection.getContentLengthLong() == -1) {
// response. Do not use available() as it is unreliable. See http://b/33498325.
if (urlConnection.getInputStream().read() == -1) {
validationLog("Empty 200 response interpreted as 204 response.");
httpResponseCode = 204;
}
}
}
} catch (IOException e) {
validationLog("Probably not a portal: exception " + e);
if (httpResponseCode == 599) {
// TODO: Ping gateway and DNS server and log results.
}
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
logValidationProbe(probeTimer.stop(), probeType, httpResponseCode);
return new CaptivePortalProbeResult(httpResponseCode, redirectUrl, url.toString());
}
use of android.net.util.Stopwatch in project android_frameworks_base by DirtyUnicorns.
the class NetworkMonitor method sendDnsProbe.
/** Do a DNS resolution of the given server. */
private void sendDnsProbe(String host) {
if (TextUtils.isEmpty(host)) {
return;
}
final String name = ValidationProbeEvent.getProbeName(ValidationProbeEvent.PROBE_DNS);
final Stopwatch watch = new Stopwatch().start();
int result;
String connectInfo;
try {
InetAddress[] addresses = mNetworkAgentInfo.network.getAllByName(host);
result = ValidationProbeEvent.DNS_SUCCESS;
StringBuffer buffer = new StringBuffer(host).append("=");
for (InetAddress address : addresses) {
buffer.append(address.getHostAddress());
if (address != addresses[addresses.length - 1])
buffer.append(",");
}
connectInfo = buffer.toString();
} catch (UnknownHostException e) {
result = ValidationProbeEvent.DNS_FAILURE;
connectInfo = host;
}
final long latency = watch.stop();
String resultString = (ValidationProbeEvent.DNS_SUCCESS == result) ? "OK" : "FAIL";
validationLog(String.format("%s %s %dms, %s", name, resultString, latency, connectInfo));
logValidationProbe(latency, ValidationProbeEvent.PROBE_DNS, result);
}
use of android.net.util.Stopwatch in project android_frameworks_base by ResurrectionRemix.
the class NetworkMonitor method sendDnsProbe.
/** Do a DNS resolution of the given server. */
private void sendDnsProbe(String host) {
if (TextUtils.isEmpty(host)) {
return;
}
final String name = ValidationProbeEvent.getProbeName(ValidationProbeEvent.PROBE_DNS);
final Stopwatch watch = new Stopwatch().start();
int result;
String connectInfo;
try {
InetAddress[] addresses = mNetworkAgentInfo.network.getAllByName(host);
result = ValidationProbeEvent.DNS_SUCCESS;
StringBuffer buffer = new StringBuffer(host).append("=");
for (InetAddress address : addresses) {
buffer.append(address.getHostAddress());
if (address != addresses[addresses.length - 1])
buffer.append(",");
}
connectInfo = buffer.toString();
} catch (UnknownHostException e) {
result = ValidationProbeEvent.DNS_FAILURE;
connectInfo = host;
}
final long latency = watch.stop();
String resultString = (ValidationProbeEvent.DNS_SUCCESS == result) ? "OK" : "FAIL";
validationLog(String.format("%s %s %dms, %s", name, resultString, latency, connectInfo));
logValidationProbe(latency, ValidationProbeEvent.PROBE_DNS, result);
}
Aggregations