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 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 valid IPv4 address, which is the sequence of four | |
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 * does not verify the length of the host name labels, the total length of | |
nharper
2015/11/19 23:47:54
documentation nit: RFC 3490 section 4.1 states tha
kapishnikov
2015/11/20 16:38:03
You are right. I have changed the comments and add
| |
27 * the host name and the validity of the top level domain. | |
28 * | |
29 * Note: Currently Cronet doesn't have native implementation of host name va lidation that can | |
30 * be used. There is code that parses a provided URL but doesn't ensur e its correctness. | |
31 * The implementation relies on {@code getaddrinfo} function. | |
32 * | |
33 * @param hostName host name to check. | |
34 * @return true if the string is a valid host name. | |
35 */ | |
36 static boolean isValidHostName(String hostName) { | |
37 try { | |
38 IDN.toASCII(hostName, IDN.USE_STD3_ASCII_RULES); | |
39 } catch (IllegalArgumentException ex) { | |
40 // The hostname is illegal according to RFC 1122 and RFC 1123. | |
41 return false; | |
42 } | |
43 return true; | |
44 } | |
45 | |
46 /** | |
47 * Checks whether a given string that represents an IPv4 address is valid. | |
48 * | |
49 * @param addr IPv4 address to check. | |
50 * @return true if the string is a valid IPv4 address. | |
51 */ | |
52 static boolean isValidIPv4(String addr) { | |
53 return VALID_IP_PATTERN.matcher(addr).matches(); | |
nharper
2015/11/19 23:47:54
Chrome interprets plenty of things that don't matc
kapishnikov
2015/11/20 16:38:03
To validate the host name, isValidHostName() metho
nharper
2015/11/20 19:02:47
(This comment really belongs on isValidHostNameFor
kapishnikov
2015/11/20 20:40:09
It is a good point. I agree that we should make th
kapishnikov
2015/11/23 16:48:45
Done with corresponding tests in PkpTest.java. I h
| |
54 } | |
55 } | |
OLD | NEW |