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 // IPv4 validation related constants. |
| 14 |
| 15 // Expression that defines valid IPv4 decimal number in range [0, 255]. |
| 16 private static final String VALID_IP_NUMBER = |
| 17 "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"; |
| 18 // Expression that defines valid IPv4 address, which is the sequence of four |
| 19 // |VALID_IP_NUMBER| numbers separated by '.'. |
| 20 private static final String VALID_IP_EXPR = |
| 21 "^(" + VALID_IP_NUMBER + "\\.){3}" + VALID_IP_NUMBER + "$"; |
| 22 private static final Pattern VALID_IP_PATTERN = Pattern.compile(VALID_IP_EXP
R); |
| 23 |
| 24 // Hostname validation related constants. |
| 25 |
| 26 // Component hostname labels may contain only the ASCII letters 'a' through
'z' |
| 27 // (in a case-insensitive manner), the digits '0' through '9', and the hyphe
n ('-'). |
| 28 // Labels cannot start with a hyphen, and must not end with a hyphen. |
| 29 // No other symbols, punctuation characters, or white space are permitted. |
| 30 private static final String VALID_HOST_LABEL = |
| 31 "([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])"; |
| 32 // Defines a valid host name expression, which is the sequence of |VALID_HOS
T_LABEL| labels |
| 33 // separated by '.'. |
| 34 private static final String VALID_HOST_EXPR = |
| 35 "^(" + VALID_HOST_LABEL + "\\.)*" + VALID_HOST_LABEL + "$"; |
| 36 private static final Pattern VALID_HOST_PATTERN = Pattern.compile(VALID_HOST
_EXPR); |
| 37 |
| 38 private CronetUtil() {} |
| 39 |
| 40 /** |
| 41 * Checks whether a given string that represents a host name is valid. The m
ethod |
| 42 * does not verify the length of the host name labels, the total length of |
| 43 * the host name and the validity of the top level domain. |
| 44 * |
| 45 * Note: Currently Cronet doesn't have native implementation of host name va
lidation that can |
| 46 * be used. There is code that parses a provided URL but doesn't ensur
e its correctness. |
| 47 * The implementation relies on {@code getaddrinfo} function. |
| 48 * |
| 49 * @param hostName host name to check. |
| 50 * @return true if the string is a valid host name. |
| 51 */ |
| 52 static boolean isValidHostName(String hostName) { |
| 53 String ascii = IDN.toASCII(hostName); |
| 54 return VALID_HOST_PATTERN.matcher(ascii).matches(); |
| 55 } |
| 56 |
| 57 /** |
| 58 * Checks whether a given string that represents an IPv4 address is valid. |
| 59 * |
| 60 * @param addr IPv4 address to check. |
| 61 * @return true if the string is a valid IPv4 address. |
| 62 */ |
| 63 static boolean isValidIPv4(String addr) { |
| 64 return VALID_IP_PATTERN.matcher(addr).matches(); |
| 65 } |
| 66 } |
OLD | NEW |