| Index: components/cronet/android/api/src/org/chromium/net/CronetUtil.java
|
| diff --git a/components/cronet/android/api/src/org/chromium/net/CronetUtil.java b/components/cronet/android/api/src/org/chromium/net/CronetUtil.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..1a0d559723d00e3f3a44d31f82e7f8b2f7c09713
|
| --- /dev/null
|
| +++ b/components/cronet/android/api/src/org/chromium/net/CronetUtil.java
|
| @@ -0,0 +1,66 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +package org.chromium.net;
|
| +
|
| +import java.net.IDN;
|
| +import java.util.regex.Pattern;
|
| +
|
| +/**
|
| + * A set of generic utility methods.
|
| + */
|
| +class CronetUtil {
|
| + // IPv4 validation related constants.
|
| +
|
| + // Expression that defines valid IPv4 decimal number in range [0, 255].
|
| + private static final String VALID_IP_NUMBER =
|
| + "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])";
|
| + // Expression that defines valid IPv4 address, which is the sequence of four
|
| + // |VALID_IP_NUMBER| numbers separated by '.'.
|
| + private static final String VALID_IP_EXPR =
|
| + "^(" + VALID_IP_NUMBER + "\\.){3}" + VALID_IP_NUMBER + "$";
|
| + private static final Pattern VALID_IP_PATTERN = Pattern.compile(VALID_IP_EXPR);
|
| +
|
| + // Hostname validation related constants.
|
| +
|
| + // Component hostname labels may contain only the ASCII letters 'a' through 'z'
|
| + // (in a case-insensitive manner), the digits '0' through '9', and the hyphen ('-').
|
| + // Labels cannot start with a hyphen, and must not end with a hyphen.
|
| + // No other symbols, punctuation characters, or white space are permitted.
|
| + private static final String VALID_HOST_LABEL =
|
| + "([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])";
|
| + // Defines a valid host name expression, which is the sequence of |VALID_HOST_LABEL| labels
|
| + // separated by '.'.
|
| + private static final String VALID_HOST_EXPR =
|
| + "^(" + VALID_HOST_LABEL + "\\.)*" + VALID_HOST_LABEL + "$";
|
| + private static final Pattern VALID_HOST_PATTERN = Pattern.compile(VALID_HOST_EXPR);
|
| +
|
| + private CronetUtil() {}
|
| +
|
| + /**
|
| + * Checks whether a given string that represents a host name is valid. The method
|
| + * does not verify the length of the host name labels, the total length of
|
| + * the host name and the validity of the top level domain.
|
| + *
|
| + * Note: Currently Cronet doesn't have native implementation of host name validation that can
|
| + * be used. There is code that parses a provided URL but doesn't ensure its correctness.
|
| + * The implementation relies on {@code getaddrinfo} function.
|
| + *
|
| + * @param hostName host name to check.
|
| + * @return true if the string is a valid host name.
|
| + */
|
| + static boolean isValidHostName(String hostName) {
|
| + String ascii = IDN.toASCII(hostName);
|
| + return VALID_HOST_PATTERN.matcher(ascii).matches();
|
| + }
|
| +
|
| + /**
|
| + * Checks whether a given string that represents an IPv4 address is valid.
|
| + *
|
| + * @param addr IPv4 address to check.
|
| + * @return true if the string is a valid IPv4 address.
|
| + */
|
| + static boolean isValidIPv4(String addr) {
|
| + return VALID_IP_PATTERN.matcher(addr).matches();
|
| + }
|
| +}
|
|
|