Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a4158702 authored by Hugo Benichi's avatar Hugo Benichi
Browse files

Captive portal: regroup hardcoded http response codes.

This patch regroups various hardcoded http codes into well defined
constants. This reduces risk of errors and makes the captive portal
logic clearer.

This patch also fixes the logging when a captive portal detection
probe fails, to take into account https ssl handshake failures: for
well-behaved portals it is expected that the https probe will fail,
however the error message was written before the introduction of the
https probe and had become ambiguous.

Test: built, flashed, tested manually with various portal networks
Bug: 36532213

Change-Id: Ia15f77e268cb414816fc52f92835289f9a9ce92b
parent 0908daaa
Loading
Loading
Loading
Loading
+21 −10
Original line number Diff line number Diff line
@@ -478,7 +478,11 @@ public class NetworkMonitor extends StateMachine {
     */
    @VisibleForTesting
    public static final class CaptivePortalProbeResult {
        static final CaptivePortalProbeResult FAILED = new CaptivePortalProbeResult(599);
        static final int SUCCESS_CODE = 204;
        static final int FAILED_CODE = 599;

        static final CaptivePortalProbeResult FAILED = new CaptivePortalProbeResult(FAILED_CODE);
        static final CaptivePortalProbeResult SUCCESS = new CaptivePortalProbeResult(SUCCESS_CODE);

        private final int mHttpResponseCode;  // HTTP response code returned from Internet probe.
        final String redirectUrl;             // Redirect destination returned from Internet probe.
@@ -496,9 +500,16 @@ public class NetworkMonitor extends StateMachine {
            this(httpResponseCode, null, null);
        }

        boolean isSuccessful() { return mHttpResponseCode == 204; }
        boolean isSuccessful() {
            return mHttpResponseCode == SUCCESS_CODE;
        }

        boolean isPortal() {
            return !isSuccessful() && mHttpResponseCode >= 200 && mHttpResponseCode <= 399;
            return !isSuccessful() && (mHttpResponseCode >= 200) && (mHttpResponseCode <= 399);
        }

        boolean isFailed() {
            return !isSuccessful() && !isPortal();
        }
    }

@@ -712,7 +723,7 @@ public class NetworkMonitor extends StateMachine {
    protected CaptivePortalProbeResult isCaptivePortal() {
        if (!mIsCaptivePortalCheckEnabled) {
            validationLog("Validation disabled.");
            return new CaptivePortalProbeResult(204);
            return CaptivePortalProbeResult.SUCCESS;
        }

        URL pacUrl = null;
@@ -816,7 +827,7 @@ public class NetworkMonitor extends StateMachine {
    @VisibleForTesting
    protected CaptivePortalProbeResult sendHttpProbe(URL url, int probeType) {
        HttpURLConnection urlConnection = null;
        int httpResponseCode = 599;
        int httpResponseCode = CaptivePortalProbeResult.FAILED_CODE;
        String redirectUrl = null;
        final Stopwatch probeTimer = new Stopwatch().start();
        try {
@@ -854,7 +865,7 @@ public class NetworkMonitor extends StateMachine {
                if (probeType == ValidationProbeEvent.PROBE_PAC) {
                    validationLog(
                            probeType, url, "PAC fetch 200 response interpreted as 204 response.");
                    httpResponseCode = 204;
                    httpResponseCode = CaptivePortalProbeResult.SUCCESS_CODE;
                } 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
@@ -862,20 +873,20 @@ public class NetworkMonitor extends StateMachine {
                    // See http://b/9972012.
                    validationLog(probeType, url,
                        "200 response with Content-length=0 interpreted as 204 response.");
                    httpResponseCode = 204;
                    httpResponseCode = CaptivePortalProbeResult.SUCCESS_CODE;
                } else if (urlConnection.getContentLengthLong() == -1) {
                    // When no Content-length (default value == -1), attempt to read a byte from the
                    // response. Do not use available() as it is unreliable. See http://b/33498325.
                    if (urlConnection.getInputStream().read() == -1) {
                        validationLog(
                                probeType, url, "Empty 200 response interpreted as 204 response.");
                        httpResponseCode = 204;
                        httpResponseCode = CaptivePortalProbeResult.SUCCESS_CODE;
                    }
                }
            }
        } catch (IOException e) {
            validationLog(probeType, url, "Probably not a portal: exception " + e);
            if (httpResponseCode == 599) {
            validationLog(probeType, url, "Probe failed with exception " + e);
            if (httpResponseCode == CaptivePortalProbeResult.FAILED_CODE) {
                // TODO: Ping gateway and DNS server and log results.
            }
        } finally {