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..6ad0545c4ef02761d6686b03bca0fb52a00d9d9d |
--- /dev/null |
+++ b/components/cronet/android/api/src/org/chromium/net/CronetUtil.java |
@@ -0,0 +1,55 @@ |
+// 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 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 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 |
+ * 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
|
+ * the host name and 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. |
+ * |
+ * @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(); |
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
|
+ } |
+} |