| 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..42794807e4028c8303ba9137c33a89bf62c51de3
|
| --- /dev/null
|
| +++ b/components/cronet/android/api/src/org/chromium/net/CronetUtil.java
|
| @@ -0,0 +1,58 @@
|
| +// 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 {
|
| + // Expression that defines a 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 a 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);
|
| +
|
| + private CronetUtil() {}
|
| +
|
| + /**
|
| + * Checks whether a given string that represents a host name is valid. The method
|
| + * verifies the length of the host name labels (max 63 characters) and the total length of
|
| + * the host name (max 255 characters). The method does not verify 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) {
|
| + try {
|
| + IDN.toASCII(hostName, IDN.USE_STD3_ASCII_RULES);
|
| + } catch (IllegalArgumentException ex) {
|
| + // The hostname is illegal according to RFC 1122 and RFC 1123.
|
| + return false;
|
| + }
|
| + return true;
|
| + }
|
| +
|
| + /**
|
| + * Checks whether a given string that represents an IPv4 address is valid.
|
| + * The format of the string should be in the decimal xxx.xxx.xxx.xxx format
|
| + * with no leading zeroes.
|
| + *
|
| + * @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();
|
| + }
|
| +}
|
|
|