OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 package org.chromium.net; |
| 5 |
| 6 import java.net.IDN; |
| 7 import java.util.regex.Pattern; |
| 8 |
| 9 /** |
| 10 * A set of generic utility methods. |
| 11 */ |
| 12 class CronetUtil { |
| 13 // Expression that defines a valid IPv4 decimal number in range [0, 255]. |
| 14 private static final String VALID_IP_NUMBER = |
| 15 "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"; |
| 16 // Expression that defines a valid IPv4 address, which is the sequence of fo
ur |
| 17 // |VALID_IP_NUMBER| numbers separated by '.'. |
| 18 private static final String VALID_IP_EXPR = |
| 19 "^(" + VALID_IP_NUMBER + "\\.){3}" + VALID_IP_NUMBER + "$"; |
| 20 private static final Pattern VALID_IP_PATTERN = Pattern.compile(VALID_IP_EXP
R); |
| 21 |
| 22 private CronetUtil() {} |
| 23 |
| 24 /** |
| 25 * Checks whether a given string that represents a host name is valid. The m
ethod |
| 26 * verifies the length of the host name labels (max 63 characters) and the t
otal length of |
| 27 * the host name (max 255 characters). The method does not verify the validi
ty of the top |
| 28 * level domain. |
| 29 * |
| 30 * Note: Currently Cronet doesn't have native implementation of host name va
lidation that can |
| 31 * be used. There is code that parses a provided URL but doesn't ensur
e its correctness. |
| 32 * The implementation relies on {@code getaddrinfo} function. |
| 33 * |
| 34 * @param hostName host name to check. |
| 35 * @return true if the string is a valid host name. |
| 36 */ |
| 37 static boolean isValidHostName(String hostName) { |
| 38 try { |
| 39 IDN.toASCII(hostName, IDN.USE_STD3_ASCII_RULES); |
| 40 } catch (IllegalArgumentException ex) { |
| 41 // The hostname is illegal according to RFC 1122 and RFC 1123. |
| 42 return false; |
| 43 } |
| 44 return true; |
| 45 } |
| 46 |
| 47 /** |
| 48 * Checks whether a given string that represents an IPv4 address is valid. |
| 49 * The format of the string should be in the decimal xxx.xxx.xxx.xxx format |
| 50 * with no leading zeroes. |
| 51 * |
| 52 * @param addr IPv4 address to check. |
| 53 * @return true if the string is a valid IPv4 address. |
| 54 */ |
| 55 static boolean isValidIPv4(String addr) { |
| 56 return VALID_IP_PATTERN.matcher(addr).matches(); |
| 57 } |
| 58 } |
OLD | NEW |