Move shared Proxy method and constants to shared lib

The static validate() method an PROXY_* constants in Proxy are
used by both module and Settings. After Proxy is moving into
connectivity module, Setting will not able to access them.
Thus, move them to ProxyUtils in net shared lib.

Bug: 172183305
Test: atest FrameworksNetTests
Test: atest CtsNetTestCases:ProxyInfoTest
Change-Id: I02c88aeaeb53d62b87a42a2d8ead9b140b054650
This commit is contained in:
Chiachang Wang
2021-02-05 17:33:53 +08:00
parent e004fd3a44
commit ab9f017af4
2 changed files with 3 additions and 61 deletions

View File

@@ -30,8 +30,6 @@ import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A convenience class for accessing the user and default proxy
@@ -64,40 +62,9 @@ public final class Proxy {
@Deprecated
public static final String EXTRA_PROXY_INFO = "android.intent.extra.PROXY_INFO";
/** @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
// Matches blank input, ips, and domain names
private static final String NAME_IP_REGEX =
"[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*)*";
private static final String HOSTNAME_REGEXP = "^$|^" + NAME_IP_REGEX + "$";
private static final Pattern HOSTNAME_PATTERN;
private static final String EXCL_REGEX =
"[a-zA-Z0-9*]+(\\-[a-zA-Z0-9*]+)*(\\.[a-zA-Z0-9*]+(\\-[a-zA-Z0-9*]+)*)*";
private static final String EXCLLIST_REGEXP = "^$|^" + EXCL_REGEX + "(," + EXCL_REGEX + ")*$";
private static final Pattern EXCLLIST_PATTERN;
static {
HOSTNAME_PATTERN = Pattern.compile(HOSTNAME_REGEXP);
EXCLLIST_PATTERN = Pattern.compile(EXCLLIST_REGEXP);
sDefaultProxySelector = ProxySelector.getDefault();
}
@@ -216,33 +183,6 @@ public final class Proxy {
return false;
}
/**
* Validate syntax of hostname, port and exclusion list entries
* {@hide}
*/
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()) return PROXY_HOSTNAME_INVALID;
if (!listMatch.matches()) return PROXY_EXCLLIST_INVALID;
if (hostname.length() > 0 && port.length() == 0) return PROXY_PORT_EMPTY;
if (port.length() > 0) {
if (hostname.length() == 0) return PROXY_HOSTNAME_EMPTY;
int portVal = -1;
try {
portVal = Integer.parseInt(port);
} catch (NumberFormatException ex) {
return PROXY_PORT_INVALID;
}
if (portVal <= 0 || portVal > 0xFFFF) return PROXY_PORT_INVALID;
}
return PROXY_VALID;
}
/** @hide */
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
public static final void setHttpProxySystemProperty(ProxyInfo p) {

View File

@@ -23,6 +23,8 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import com.android.net.module.util.ProxyUtils;
import java.net.InetSocketAddress;
import java.net.URLConnection;
import java.util.List;
@@ -233,7 +235,7 @@ public class ProxyInfo implements Parcelable {
*/
public boolean isValid() {
if (!Uri.EMPTY.equals(mPacFileUrl)) return true;
return Proxy.PROXY_VALID == Proxy.validate(mHost == null ? "" : mHost,
return ProxyUtils.PROXY_VALID == ProxyUtils.validate(mHost == null ? "" : mHost,
mPort == 0 ? "" : Integer.toString(mPort),
mExclusionList == null ? "" : mExclusionList);
}