Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(529)

Side by Side Diff: components/cronet/android/api/src/org/chromium/net/CronetUtil.java

Issue 1407263010: [Cronet] Public key pinning for Java API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Shutdown CronetEngine between HPKP tests Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698