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

Commit 9070484e authored by Yuhao Zheng's avatar Yuhao Zheng
Browse files

Return detailed reason of invalid proxy settings

Make Proxy.validate() return valid/invalid int code, instead of throwing
exceptions. If invalid, detailed reason code is returned (currently for
Settings UI use).

bug: 13248097
Change-Id: Ic68d03f666f1cd63667afc311de7dc370d233901
parent 9f6a372c
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -1155,7 +1155,9 @@ public class DevicePolicyManager {
                        }
                        exclSpec = listBuilder.toString();
                    }
                    android.net.Proxy.validate(hostName, Integer.toString(port), exclSpec);
                    if (android.net.Proxy.validate(hostName, Integer.toString(port), exclSpec)
                            != android.net.Proxy.PROXY_VALID)
                        throw new IllegalArgumentException();
                }
                return mService.setGlobalProxy(admin, hostSpec, exclSpec, UserHandle.myUserId());
            } catch (RemoteException e) {
+21 −17
Original line number Diff line number Diff line
@@ -66,6 +66,19 @@ public final class Proxy {
    /** {@hide} **/
    public static final String EXTRA_PROXY_INFO = "proxy";

    /** @hide */
    public static final int PROXY_VALID             = 0;
    /** @hide */
    public static final int PROXY_HOSTNAME_EMPTY    = 1;
    /** @hide */
    public static final int PROXY_HOSTNAME_INVALID  = 2;
    /** @hide */
    public static final int PROXY_PORT_EMPTY        = 3;
    /** @hide */
    public static final int PROXY_PORT_INVALID      = 4;
    /** @hide */
    public static final int PROXY_EXCLLIST_INVALID  = 5;

    private static ConnectivityManager sConnectivityManager = null;

    // Hostname / IP REGEX validation
@@ -236,36 +249,27 @@ public final class Proxy {
     * Validate syntax of hostname, port and exclusion list entries
     * {@hide}
     */
    public static void validate(String hostname, String port, String exclList) {
    public static int validate(String hostname, String port, String exclList) {
        Matcher match = HOSTNAME_PATTERN.matcher(hostname);
        Matcher listMatch = EXCLLIST_PATTERN.matcher(exclList);

        if (!match.matches()) {
            throw new IllegalArgumentException();
        }
        if (!match.matches()) return PROXY_HOSTNAME_INVALID;

        if (!listMatch.matches()) {
            throw new IllegalArgumentException();
        }
        if (!listMatch.matches()) return PROXY_EXCLLIST_INVALID;

        if (hostname.length() > 0 && port.length() == 0) {
            throw new IllegalArgumentException();
        }
        if (hostname.length() > 0 && port.length() == 0) return PROXY_PORT_EMPTY;

        if (port.length() > 0) {
            if (hostname.length() == 0) {
                throw new IllegalArgumentException();
            }
            if (hostname.length() == 0) return PROXY_HOSTNAME_EMPTY;
            int portVal = -1;
            try {
                portVal = Integer.parseInt(port);
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException();
            }
            if (portVal <= 0 || portVal > 0xFFFF) {
                throw new IllegalArgumentException();
                return PROXY_PORT_INVALID;
            }
            if (portVal <= 0 || portVal > 0xFFFF) return PROXY_PORT_INVALID;
        }
        return PROXY_VALID;
    }

    static class AndroidProxySelectorRoutePlanner
+3 −7
Original line number Diff line number Diff line
@@ -140,13 +140,9 @@ public class ProxyProperties implements Parcelable {

    public boolean isValid() {
        if (!TextUtils.isEmpty(mPacFileUrl)) return true;
        try {
            Proxy.validate(mHost == null ? "" : mHost, mPort == 0 ? "" : Integer.toString(mPort),
        return Proxy.PROXY_VALID == Proxy.validate(mHost == null ? "" : mHost,
                                                mPort == 0 ? "" : Integer.toString(mPort),
                                                mExclusionList == null ? "" : mExclusionList);
        } catch (IllegalArgumentException e) {
            return false;
        }
        return true;
    }

    public java.net.Proxy makeProxy() {