Search in sources :

Example 1 with Stopwatch

use of android.net.util.Stopwatch in project android_frameworks_base by AOSPA.

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);
}
Also used : UnknownHostException(java.net.UnknownHostException) Stopwatch(android.net.util.Stopwatch) InetAddress(java.net.InetAddress)

Example 2 with Stopwatch

use of android.net.util.Stopwatch in project android_frameworks_base by AOSPA.

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());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Stopwatch(android.net.util.Stopwatch) IOException(java.io.IOException) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 3 with Stopwatch

use of android.net.util.Stopwatch in project android_frameworks_base by ResurrectionRemix.

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());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Stopwatch(android.net.util.Stopwatch) IOException(java.io.IOException) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 4 with Stopwatch

use of android.net.util.Stopwatch in project android_frameworks_base by crdroidandroid.

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());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Stopwatch(android.net.util.Stopwatch) IOException(java.io.IOException) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 5 with Stopwatch

use of android.net.util.Stopwatch in project android_frameworks_base by crdroidandroid.

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);
}
Also used : UnknownHostException(java.net.UnknownHostException) Stopwatch(android.net.util.Stopwatch) InetAddress(java.net.InetAddress)

Aggregations

Stopwatch (android.net.util.Stopwatch)10 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)5 IOException (java.io.IOException)5 HttpURLConnection (java.net.HttpURLConnection)5 InetAddress (java.net.InetAddress)5 UnknownHostException (java.net.UnknownHostException)5