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 private static final String VALID_IP_EXPR = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2 [0-4][0-9]|25[0-5])" | |
estark
2015/11/10 19:04:16
Where are these regexes from? It's difficult for m
kapishnikov
2015/11/10 22:33:33
I took it somewhere from stackoverflow.com. There
| |
14 + "\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"; | |
15 private static final String VALID_HOST_EXPR = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a- zA-Z0-9\\-]*" | |
16 + "[a-zA-Z0-9])\\.)*([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0- 9])$"; | |
17 private static final Pattern VALID_IP_PATTERN = Pattern.compile(VALID_IP_EXP R); | |
18 private static final Pattern VALID_HOST_PATTERN = Pattern.compile(VALID_HOST _EXPR); | |
19 | |
20 private CronetUtil() {} | |
21 | |
22 /** | |
23 * Checks whether a given string that represents a host name is valid. | |
24 * | |
25 * @param hostName host name to check. | |
26 * @return true if the string is a valid host name. | |
27 */ | |
28 static boolean isValidHostName(String hostName) { | |
29 String ascii = IDN.toASCII(hostName); | |
mef
2015/11/10 22:19:03
maybe IDN.toASCII(src, IDN.USE_STD3_ASCII_RULES) t
kapishnikov
2015/11/10 22:33:33
I will add it.
| |
30 return VALID_HOST_PATTERN.matcher(ascii).matches(); | |
31 } | |
32 | |
33 /** | |
34 * Checks whether a given string that represents an IPv4 address is valid. | |
35 * | |
36 * @param addr IPv4 address to check. | |
37 * @return true if the string is a valid IPv4 address. | |
38 */ | |
39 static boolean isValidIPv4(String addr) { | |
40 return VALID_IP_PATTERN.matcher(addr).matches(); | |
41 } | |
42 } | |
OLD | NEW |