OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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.impl; |
| 5 |
| 6 import android.content.Context; |
| 7 import android.support.annotation.IntDef; |
| 8 |
| 9 import org.chromium.base.Log; |
| 10 import org.chromium.base.VisibleForTesting; |
| 11 import org.chromium.net.CronetEngine; |
| 12 import org.chromium.net.HttpCacheType; |
| 13 import org.chromium.net.ICronetEngineBuilder; |
| 14 |
| 15 import java.io.File; |
| 16 import java.lang.annotation.Retention; |
| 17 import java.lang.annotation.RetentionPolicy; |
| 18 import java.net.IDN; |
| 19 import java.util.Date; |
| 20 import java.util.HashSet; |
| 21 import java.util.LinkedList; |
| 22 import java.util.List; |
| 23 import java.util.Set; |
| 24 import java.util.regex.Pattern; |
| 25 |
| 26 /** |
| 27 * Implementation of {@link ICronetEngineBuilder}. |
| 28 */ |
| 29 public class CronetEngineBuilderImpl implements ICronetEngineBuilder { |
| 30 /** |
| 31 * A hint that a host supports QUIC. |
| 32 */ |
| 33 public static class QuicHint { |
| 34 // The host. |
| 35 final String mHost; |
| 36 // Port of the server that supports QUIC. |
| 37 final int mPort; |
| 38 // Alternate protocol port. |
| 39 final int mAlternatePort; |
| 40 |
| 41 QuicHint(String host, int port, int alternatePort) { |
| 42 mHost = host; |
| 43 mPort = port; |
| 44 mAlternatePort = alternatePort; |
| 45 } |
| 46 } |
| 47 |
| 48 /** |
| 49 * A public key pin. |
| 50 */ |
| 51 public static class Pkp { |
| 52 // Host to pin for. |
| 53 final String mHost; |
| 54 // Array of SHA-256 hashes of keys. |
| 55 final byte[][] mHashes; |
| 56 // Should pin apply to subdomains? |
| 57 final boolean mIncludeSubdomains; |
| 58 // When the pin expires. |
| 59 final Date mExpirationDate; |
| 60 |
| 61 Pkp(String host, byte[][] hashes, boolean includeSubdomains, Date expira
tionDate) { |
| 62 mHost = host; |
| 63 mHashes = hashes; |
| 64 mIncludeSubdomains = includeSubdomains; |
| 65 mExpirationDate = expirationDate; |
| 66 } |
| 67 } |
| 68 |
| 69 private static final String TAG = "CronetEngineBuilder"; |
| 70 private static final Pattern INVALID_PKP_HOST_NAME = Pattern.compile("^[0-9\
\.]*$"); |
| 71 |
| 72 // Private fields are simply storage of configuration for the resulting Cron
etEngine. |
| 73 // See setters below for verbose descriptions. |
| 74 private final Context mContext; |
| 75 private final List<QuicHint> mQuicHints = new LinkedList<>(); |
| 76 private final List<Pkp> mPkps = new LinkedList<>(); |
| 77 private boolean mPublicKeyPinningBypassForLocalTrustAnchorsEnabled; |
| 78 private String mUserAgent; |
| 79 private String mStoragePath; |
| 80 private boolean mLegacyModeEnabled; |
| 81 private CronetEngine.Builder.LibraryLoader mLibraryLoader; |
| 82 private String mLibraryName; |
| 83 private boolean mQuicEnabled; |
| 84 private boolean mHttp2Enabled; |
| 85 private boolean mSdchEnabled; |
| 86 private String mDataReductionProxyKey; |
| 87 private String mDataReductionProxyPrimaryProxy; |
| 88 private String mDataReductionProxyFallbackProxy; |
| 89 private String mDataReductionProxySecureProxyCheckUrl; |
| 90 private boolean mDisableCache; |
| 91 private int mHttpCacheMode; |
| 92 private long mHttpCacheMaxSize; |
| 93 private String mExperimentalOptions; |
| 94 private long mMockCertVerifier; |
| 95 private boolean mNetworkQualityEstimatorEnabled; |
| 96 private String mCertVerifierData; |
| 97 |
| 98 /** |
| 99 * Default config enables SPDY, disables QUIC, SDCH and HTTP cache. |
| 100 * @param context Android {@link Context} for engine to use. |
| 101 */ |
| 102 public CronetEngineBuilderImpl(Context context) { |
| 103 mContext = context; |
| 104 setLibraryName("cronet"); |
| 105 enableLegacyMode(false); |
| 106 enableQuic(false); |
| 107 enableHttp2(true); |
| 108 enableSdch(false); |
| 109 enableHttpCache(HTTP_CACHE_DISABLED, 0); |
| 110 enableNetworkQualityEstimator(false); |
| 111 enablePublicKeyPinningBypassForLocalTrustAnchors(true); |
| 112 } |
| 113 |
| 114 /** |
| 115 * Constructs a User-Agent string including application name and version, |
| 116 * system build version, model and id, and Cronet version. |
| 117 * |
| 118 * @return User-Agent string. |
| 119 */ |
| 120 public String getDefaultUserAgent() { |
| 121 return UserAgent.from(mContext); |
| 122 } |
| 123 |
| 124 /** |
| 125 * Overrides the User-Agent header for all requests. An explicitly |
| 126 * set User-Agent header (set using |
| 127 * {@link org.chromium.net.UrlRequest.Builder#addHeader(String, String)}) |
| 128 * will override a value set using this function. |
| 129 * |
| 130 * @param userAgent the User-Agent string to use for all requests. |
| 131 * @return the builder to facilitate chaining. |
| 132 */ |
| 133 public CronetEngineBuilderImpl setUserAgent(String userAgent) { |
| 134 mUserAgent = userAgent; |
| 135 return this; |
| 136 } |
| 137 |
| 138 String getUserAgent() { |
| 139 return mUserAgent; |
| 140 } |
| 141 |
| 142 /** |
| 143 * Sets directory for HTTP Cache and Cookie Storage. The directory must |
| 144 * exist. |
| 145 * <p> |
| 146 * <b>NOTE:</b> Do not use the same storage directory with more than one |
| 147 * {@code CronetEngine} at a time. Access to the storage directory does |
| 148 * not support concurrent access by multiple {@code CronetEngine}s. |
| 149 * |
| 150 * @param value path to existing directory. |
| 151 * @return the builder to facilitate chaining. |
| 152 */ |
| 153 public CronetEngineBuilderImpl setStoragePath(String value) { |
| 154 if (!new File(value).isDirectory()) { |
| 155 throw new IllegalArgumentException("Storage path must be set to exis
ting directory"); |
| 156 } |
| 157 mStoragePath = value; |
| 158 return this; |
| 159 } |
| 160 |
| 161 String storagePath() { |
| 162 return mStoragePath; |
| 163 } |
| 164 |
| 165 /** |
| 166 * Sets whether the resulting {@link CronetEngine} uses an |
| 167 * implementation based on the system's |
| 168 * {@link java.net.HttpURLConnection} implementation, or if this is |
| 169 * only done as a backup if the native implementation fails to load. |
| 170 * Defaults to disabled. |
| 171 * @param value {@code true} makes the resulting {@link CronetEngine} |
| 172 * use an implementation based on the system's |
| 173 * {@link java.net.HttpURLConnection} implementation |
| 174 * without trying to load the native implementation. |
| 175 * {@code false} makes the resulting {@code CronetEngine} |
| 176 * use the native implementation, or if that fails to load, |
| 177 * falls back to an implementation based on the system's |
| 178 * {@link java.net.HttpURLConnection} implementation. |
| 179 * @return the builder to facilitate chaining. |
| 180 */ |
| 181 public CronetEngineBuilderImpl enableLegacyMode(boolean value) { |
| 182 mLegacyModeEnabled = value; |
| 183 return this; |
| 184 } |
| 185 |
| 186 private boolean legacyMode() { |
| 187 return mLegacyModeEnabled; |
| 188 } |
| 189 |
| 190 /** |
| 191 * Overrides the name of the native library backing Cronet. |
| 192 * @param libName the name of the native library backing Cronet. |
| 193 * @return the builder to facilitate chaining. |
| 194 */ |
| 195 public CronetEngineBuilderImpl setLibraryName(String libName) { |
| 196 mLibraryName = libName; |
| 197 return this; |
| 198 } |
| 199 |
| 200 String libraryName() { |
| 201 return mLibraryName; |
| 202 } |
| 203 |
| 204 /** |
| 205 * Sets a {@link CronetEngine.Builder.LibraryLoader} to be used to load the
native library. |
| 206 * If not set, the library will be loaded using {@link System#loadLibrary}. |
| 207 * @param loader {@code LibraryLoader} to be used to load the native library
. |
| 208 * @return the builder to facilitate chaining. |
| 209 */ |
| 210 public CronetEngineBuilderImpl setLibraryLoader(CronetEngine.Builder.Library
Loader loader) { |
| 211 mLibraryLoader = loader; |
| 212 return this; |
| 213 } |
| 214 |
| 215 CronetEngine.Builder.LibraryLoader libraryLoader() { |
| 216 return mLibraryLoader; |
| 217 } |
| 218 |
| 219 /** |
| 220 * Sets whether <a href="https://www.chromium.org/quic">QUIC</a> protocol |
| 221 * is enabled. Defaults to disabled. If QUIC is enabled, then QUIC User Agen
t Id |
| 222 * containing application name and Cronet version is sent to the server. |
| 223 * @param value {@code true} to enable QUIC, {@code false} to disable. |
| 224 * @return the builder to facilitate chaining. |
| 225 */ |
| 226 public CronetEngineBuilderImpl enableQuic(boolean value) { |
| 227 mQuicEnabled = value; |
| 228 return this; |
| 229 } |
| 230 |
| 231 boolean quicEnabled() { |
| 232 return mQuicEnabled; |
| 233 } |
| 234 |
| 235 /** |
| 236 * Constructs default QUIC User Agent Id string including application name |
| 237 * and Cronet version. Returns empty string if QUIC is not enabled. |
| 238 * |
| 239 * @param context Android {@link Context} to get package name from. |
| 240 * @return QUIC User Agent ID string. |
| 241 */ |
| 242 // TODO(mef): remove |context| parameter when legacy ChromiumUrlRequestConte
xt is removed. |
| 243 String getDefaultQuicUserAgentId(Context context) { |
| 244 return mQuicEnabled ? UserAgent.getQuicUserAgentIdFrom(context) : ""; |
| 245 } |
| 246 |
| 247 /** |
| 248 * Sets whether <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> |
| 249 * protocol is enabled. Defaults to enabled. |
| 250 * @param value {@code true} to enable HTTP/2, {@code false} to disable. |
| 251 * @return the builder to facilitate chaining. |
| 252 */ |
| 253 public CronetEngineBuilderImpl enableHttp2(boolean value) { |
| 254 mHttp2Enabled = value; |
| 255 return this; |
| 256 } |
| 257 |
| 258 boolean http2Enabled() { |
| 259 return mHttp2Enabled; |
| 260 } |
| 261 |
| 262 /** |
| 263 * Sets whether |
| 264 * <a |
| 265 * href="https://lists.w3.org/Archives/Public/ietf-http-wg/2008JulSep/att-04
41/Shared_Dictionary_Compression_over_HTTP.pdf"> |
| 266 * SDCH</a> compression is enabled. Defaults to disabled. |
| 267 * @param value {@code true} to enable SDCH, {@code false} to disable. |
| 268 * @return the builder to facilitate chaining. |
| 269 */ |
| 270 public CronetEngineBuilderImpl enableSdch(boolean value) { |
| 271 mSdchEnabled = value; |
| 272 return this; |
| 273 } |
| 274 |
| 275 boolean sdchEnabled() { |
| 276 return mSdchEnabled; |
| 277 } |
| 278 |
| 279 /** |
| 280 * Enables |
| 281 * <a href="https://developer.chrome.com/multidevice/data-compression">Data |
| 282 * Reduction Proxy</a>. Defaults to disabled. |
| 283 * @param key key to use when authenticating with the proxy. |
| 284 * @return the builder to facilitate chaining. |
| 285 */ |
| 286 public CronetEngineBuilderImpl enableDataReductionProxy(String key) { |
| 287 mDataReductionProxyKey = key; |
| 288 return this; |
| 289 } |
| 290 |
| 291 String dataReductionProxyKey() { |
| 292 return mDataReductionProxyKey; |
| 293 } |
| 294 |
| 295 /** |
| 296 * Overrides |
| 297 * <a href="https://developer.chrome.com/multidevice/data-compression"> |
| 298 * Data Reduction Proxy</a> configuration parameters with a primary |
| 299 * proxy name, fallback proxy name, and a secure proxy check URL. Proxies |
| 300 * are specified as [scheme://]host[:port]. Used for testing. |
| 301 * @param primaryProxy the primary data reduction proxy to use. |
| 302 * @param fallbackProxy a fallback data reduction proxy to use. |
| 303 * @param secureProxyCheckUrl a URL to fetch to determine if using a secure |
| 304 * proxy is allowed. |
| 305 * @return the builder to facilitate chaining. |
| 306 */ |
| 307 public CronetEngineBuilderImpl setDataReductionProxyOptions( |
| 308 String primaryProxy, String fallbackProxy, String secureProxyCheckUr
l) { |
| 309 if (primaryProxy.isEmpty() || fallbackProxy.isEmpty() || secureProxyChec
kUrl.isEmpty()) { |
| 310 throw new IllegalArgumentException( |
| 311 "Primary and fallback proxies and check url must be set"); |
| 312 } |
| 313 mDataReductionProxyPrimaryProxy = primaryProxy; |
| 314 mDataReductionProxyFallbackProxy = fallbackProxy; |
| 315 mDataReductionProxySecureProxyCheckUrl = secureProxyCheckUrl; |
| 316 return this; |
| 317 } |
| 318 |
| 319 String dataReductionProxyPrimaryProxy() { |
| 320 return mDataReductionProxyPrimaryProxy; |
| 321 } |
| 322 |
| 323 String dataReductionProxyFallbackProxy() { |
| 324 return mDataReductionProxyFallbackProxy; |
| 325 } |
| 326 |
| 327 String dataReductionProxySecureProxyCheckUrl() { |
| 328 return mDataReductionProxySecureProxyCheckUrl; |
| 329 } |
| 330 |
| 331 @IntDef({ |
| 332 HTTP_CACHE_DISABLED, HTTP_CACHE_IN_MEMORY, HTTP_CACHE_DISK_NO_HTTP,
HTTP_CACHE_DISK, |
| 333 }) |
| 334 @Retention(RetentionPolicy.SOURCE) |
| 335 public @interface HttpCacheSetting {} |
| 336 |
| 337 /** |
| 338 * Setting to disable HTTP cache. Some data may still be temporarily stored
in memory. |
| 339 * Passed to {@link #enableHttpCache}. |
| 340 */ |
| 341 static final int HTTP_CACHE_DISABLED = 0; |
| 342 |
| 343 /** |
| 344 * Setting to enable in-memory HTTP cache, including HTTP data. |
| 345 * Passed to {@link #enableHttpCache}. |
| 346 */ |
| 347 public static final int HTTP_CACHE_IN_MEMORY = 1; |
| 348 |
| 349 /** |
| 350 * Setting to enable on-disk cache, excluding HTTP data. |
| 351 * {@link #setStoragePath} must be called prior to passing this constant to |
| 352 * {@link #enableHttpCache}. |
| 353 */ |
| 354 static final int HTTP_CACHE_DISK_NO_HTTP = 2; |
| 355 |
| 356 /** |
| 357 * Setting to enable on-disk cache, including HTTP data. |
| 358 * {@link #setStoragePath} must be called prior to passing this constant to |
| 359 * {@link #enableHttpCache}. |
| 360 */ |
| 361 static final int HTTP_CACHE_DISK = 3; |
| 362 |
| 363 /** |
| 364 * Enables or disables caching of HTTP data and other information like QUIC |
| 365 * server information. |
| 366 * @param cacheMode control location and type of cached data. Must be one of |
| 367 * {@link #HTTP_CACHE_DISABLED HTTP_CACHE_*}. |
| 368 * @param maxSize maximum size in bytes used to cache data (advisory and may
be |
| 369 * exceeded at times). |
| 370 * @return the builder to facilitate chaining. |
| 371 */ |
| 372 public CronetEngineBuilderImpl enableHttpCache(@HttpCacheSetting int cacheMo
de, long maxSize) { |
| 373 if (cacheMode == HTTP_CACHE_DISK || cacheMode == HTTP_CACHE_DISK_NO_HTTP
) { |
| 374 if (storagePath() == null) { |
| 375 throw new IllegalArgumentException("Storage path must be set"); |
| 376 } |
| 377 } else { |
| 378 if (storagePath() != null) { |
| 379 throw new IllegalArgumentException("Storage path must not be set
"); |
| 380 } |
| 381 } |
| 382 mDisableCache = (cacheMode == HTTP_CACHE_DISABLED || cacheMode == HTTP_C
ACHE_DISK_NO_HTTP); |
| 383 mHttpCacheMaxSize = maxSize; |
| 384 |
| 385 switch (cacheMode) { |
| 386 case HTTP_CACHE_DISABLED: |
| 387 mHttpCacheMode = HttpCacheType.DISABLED; |
| 388 break; |
| 389 case HTTP_CACHE_DISK_NO_HTTP: |
| 390 case HTTP_CACHE_DISK: |
| 391 mHttpCacheMode = HttpCacheType.DISK; |
| 392 break; |
| 393 case HTTP_CACHE_IN_MEMORY: |
| 394 mHttpCacheMode = HttpCacheType.MEMORY; |
| 395 break; |
| 396 default: |
| 397 throw new IllegalArgumentException("Unknown cache mode"); |
| 398 } |
| 399 return this; |
| 400 } |
| 401 |
| 402 boolean cacheDisabled() { |
| 403 return mDisableCache; |
| 404 } |
| 405 |
| 406 long httpCacheMaxSize() { |
| 407 return mHttpCacheMaxSize; |
| 408 } |
| 409 |
| 410 int httpCacheMode() { |
| 411 return mHttpCacheMode; |
| 412 } |
| 413 |
| 414 /** |
| 415 * Adds hint that {@code host} supports QUIC. |
| 416 * Note that {@link #enableHttpCache enableHttpCache} |
| 417 * ({@link #HTTP_CACHE_DISK}) is needed to take advantage of 0-RTT |
| 418 * connection establishment between sessions. |
| 419 * |
| 420 * @param host hostname of the server that supports QUIC. |
| 421 * @param port host of the server that supports QUIC. |
| 422 * @param alternatePort alternate port to use for QUIC. |
| 423 * @return the builder to facilitate chaining. |
| 424 */ |
| 425 public CronetEngineBuilderImpl addQuicHint(String host, int port, int altern
atePort) { |
| 426 if (host.contains("/")) { |
| 427 throw new IllegalArgumentException("Illegal QUIC Hint Host: " + host
); |
| 428 } |
| 429 mQuicHints.add(new QuicHint(host, port, alternatePort)); |
| 430 return this; |
| 431 } |
| 432 |
| 433 List<QuicHint> quicHints() { |
| 434 return mQuicHints; |
| 435 } |
| 436 |
| 437 /** |
| 438 * <p> |
| 439 * Pins a set of public keys for a given host. By pinning a set of public ke
ys, |
| 440 * {@code pinsSha256}, communication with {@code hostName} is required to |
| 441 * authenticate with a certificate with a public key from the set of pinned
ones. |
| 442 * An app can pin the public key of the root certificate, any of the interme
diate |
| 443 * certificates or the end-entry certificate. Authentication will fail and s
ecure |
| 444 * communication will not be established if none of the public keys is prese
nt in the |
| 445 * host's certificate chain, even if the host attempts to authenticate with
a |
| 446 * certificate allowed by the device's trusted store of certificates. |
| 447 * </p> |
| 448 * <p> |
| 449 * Calling this method multiple times with the same host name overrides the
previously |
| 450 * set pins for the host. |
| 451 * </p> |
| 452 * <p> |
| 453 * More information about the public key pinning can be found in |
| 454 * <a href="https://tools.ietf.org/html/rfc7469">RFC 7469</a>. |
| 455 * </p> |
| 456 * |
| 457 * @param hostName name of the host to which the public keys should be pinne
d. A host that |
| 458 * consists only of digits and the dot character is treated
as invalid. |
| 459 * @param pinsSha256 a set of pins. Each pin is the SHA-256 cryptographic |
| 460 * hash of the DER-encoded ASN.1 representation of the Sub
ject Public |
| 461 * Key Info (SPKI) of the host's X.509 certificate. Use |
| 462 * {@link java.security.cert.Certificate#getPublicKey() |
| 463 * Certificate.getPublicKey()} and |
| 464 * {@link java.security.Key#getEncoded() Key.getEncoded()} |
| 465 * to obtain DER-encoded ASN.1 representation of the SPKI. |
| 466 * Although, the method does not mandate the presence of t
he backup pin |
| 467 * that can be used if the control of the primary private
key has been |
| 468 * lost, it is highly recommended to supply one. |
| 469 * @param includeSubdomains indicates whether the pinning policy should be a
pplied to |
| 470 * subdomains of {@code hostName}. |
| 471 * @param expirationDate specifies the expiration date for the pins. |
| 472 * @return the builder to facilitate chaining. |
| 473 * @throws NullPointerException if any of the input parameters are {@code nu
ll}. |
| 474 * @throws IllegalArgumentException if the given host name is invalid or {@c
ode pinsSha256} |
| 475 * contains a byte array that does not repr
esent a valid |
| 476 * SHA-256 hash. |
| 477 */ |
| 478 public CronetEngineBuilderImpl addPublicKeyPins(String hostName, Set<byte[]>
pinsSha256, |
| 479 boolean includeSubdomains, Date expirationDate) { |
| 480 if (hostName == null) { |
| 481 throw new NullPointerException("The hostname cannot be null"); |
| 482 } |
| 483 if (pinsSha256 == null) { |
| 484 throw new NullPointerException("The set of SHA256 pins cannot be nul
l"); |
| 485 } |
| 486 if (expirationDate == null) { |
| 487 throw new NullPointerException("The pin expiration date cannot be nu
ll"); |
| 488 } |
| 489 String idnHostName = validateHostNameForPinningAndConvert(hostName); |
| 490 // Convert the pin to BASE64 encoding. The hash set will eliminate dupli
cations. |
| 491 Set<byte[]> hashes = new HashSet<>(pinsSha256.size()); |
| 492 for (byte[] pinSha256 : pinsSha256) { |
| 493 if (pinSha256 == null || pinSha256.length != 32) { |
| 494 throw new IllegalArgumentException("Public key pin is invalid"); |
| 495 } |
| 496 hashes.add(pinSha256); |
| 497 } |
| 498 // Add new element to PKP list. |
| 499 mPkps.add(new Pkp(idnHostName, hashes.toArray(new byte[hashes.size()][])
, includeSubdomains, |
| 500 expirationDate)); |
| 501 return this; |
| 502 } |
| 503 |
| 504 /** |
| 505 * Returns list of public key pins. |
| 506 * @return list of public key pins. |
| 507 */ |
| 508 List<Pkp> publicKeyPins() { |
| 509 return mPkps; |
| 510 } |
| 511 |
| 512 /** |
| 513 * Enables or disables public key pinning bypass for local trust anchors. Di
sabling the |
| 514 * bypass for local trust anchors is highly discouraged since it may prohibi
t the app |
| 515 * from communicating with the pinned hosts. E.g., a user may want to send a
ll traffic |
| 516 * through an SSL enabled proxy by changing the device proxy settings and ad
ding the |
| 517 * proxy certificate to the list of local trust anchor. Disabling the bypass
will most |
| 518 * likly prevent the app from sending any traffic to the pinned hosts. For m
ore |
| 519 * information see 'How does key pinning interact with local proxies and fil
ters?' at |
| 520 * https://www.chromium.org/Home/chromium-security/security-faq |
| 521 * |
| 522 * @param value {@code true} to enable the bypass, {@code false} to disable. |
| 523 * @return the builder to facilitate chaining. |
| 524 */ |
| 525 public CronetEngineBuilderImpl enablePublicKeyPinningBypassForLocalTrustAnch
ors(boolean value) { |
| 526 mPublicKeyPinningBypassForLocalTrustAnchorsEnabled = value; |
| 527 return this; |
| 528 } |
| 529 |
| 530 boolean publicKeyPinningBypassForLocalTrustAnchorsEnabled() { |
| 531 return mPublicKeyPinningBypassForLocalTrustAnchorsEnabled; |
| 532 } |
| 533 |
| 534 /** |
| 535 * Checks whether a given string represents a valid host name for PKP and co
nverts it |
| 536 * to ASCII Compatible Encoding representation according to RFC 1122, RFC 11
23 and |
| 537 * RFC 3490. This method is more restrictive than required by RFC 7469. Thus
, a host |
| 538 * that contains digits and the dot character only is considered invalid. |
| 539 * |
| 540 * Note: Currently Cronet doesn't have native implementation of host name va
lidation that |
| 541 * can be used. There is code that parses a provided URL but doesn't e
nsure its |
| 542 * correctness. The implementation relies on {@code getaddrinfo} funct
ion. |
| 543 * |
| 544 * @param hostName host name to check and convert. |
| 545 * @return true if the string is a valid host name. |
| 546 * @throws IllegalArgumentException if the the given string does not represe
nt a valid |
| 547 * hostname. |
| 548 */ |
| 549 private static String validateHostNameForPinningAndConvert(String hostName) |
| 550 throws IllegalArgumentException { |
| 551 if (INVALID_PKP_HOST_NAME.matcher(hostName).matches()) { |
| 552 throw new IllegalArgumentException("Hostname " + hostName + " is ill
egal." |
| 553 + " A hostname should not consist of digits and/or dots only
."); |
| 554 } |
| 555 // Workaround for crash, see crbug.com/634914 |
| 556 if (hostName.length() > 255) { |
| 557 throw new IllegalArgumentException("Hostname " + hostName + " is too
long." |
| 558 + " The name of the host does not comply with RFC 1122 and R
FC 1123."); |
| 559 } |
| 560 try { |
| 561 return IDN.toASCII(hostName, IDN.USE_STD3_ASCII_RULES); |
| 562 } catch (IllegalArgumentException ex) { |
| 563 throw new IllegalArgumentException("Hostname " + hostName + " is ill
egal." |
| 564 + " The name of the host does not comply with RFC 1122 and R
FC 1123."); |
| 565 } |
| 566 } |
| 567 |
| 568 /** |
| 569 * Sets experimental options to be used in Cronet. |
| 570 * |
| 571 * @param options JSON formatted experimental options. |
| 572 * @return the builder to facilitate chaining. |
| 573 */ |
| 574 public CronetEngineBuilderImpl setExperimentalOptions(String options) { |
| 575 mExperimentalOptions = options; |
| 576 return this; |
| 577 } |
| 578 |
| 579 public String experimentalOptions() { |
| 580 return mExperimentalOptions; |
| 581 } |
| 582 |
| 583 /** |
| 584 * Sets a native MockCertVerifier for testing. See |
| 585 * {@code MockCertVerifier.createMockCertVerifier} for a method that |
| 586 * can be used to create a MockCertVerifier. |
| 587 * @param mockCertVerifier pointer to native MockCertVerifier. |
| 588 * @return the builder to facilitate chaining. |
| 589 */ |
| 590 @VisibleForTesting |
| 591 public CronetEngineBuilderImpl setMockCertVerifierForTesting(long mockCertVe
rifier) { |
| 592 mMockCertVerifier = mockCertVerifier; |
| 593 return this; |
| 594 } |
| 595 |
| 596 long mockCertVerifier() { |
| 597 return mMockCertVerifier; |
| 598 } |
| 599 |
| 600 /** |
| 601 * @return true if the network quality estimator has been enabled for |
| 602 * this builder. |
| 603 */ |
| 604 boolean networkQualityEstimatorEnabled() { |
| 605 return mNetworkQualityEstimatorEnabled; |
| 606 } |
| 607 |
| 608 /** |
| 609 * Initializes CachingCertVerifier's cache with certVerifierData which has |
| 610 * the results of certificate verification. |
| 611 * @param certVerifierData a serialized representation of certificate |
| 612 * verification results. |
| 613 * @return the builder to facilitate chaining. |
| 614 */ |
| 615 public CronetEngineBuilderImpl setCertVerifierData(String certVerifierData)
{ |
| 616 mCertVerifierData = certVerifierData; |
| 617 return this; |
| 618 } |
| 619 |
| 620 /** |
| 621 * Enables the network quality estimator, which collects and reports |
| 622 * measurements of round trip time (RTT) and downstream throughput at |
| 623 * various layers of the network stack. After enabling the estimator, |
| 624 * listeners of RTT and throughput can be added with |
| 625 * {@link org.chromium.net.ExperimentalCronetEngine#addRttListener} and |
| 626 * {@link org.chromium.net.ExperimentalCronetEngine#addThroughputListener} a
nd |
| 627 * removed with {@link org.chromium.net.ExperimentalCronetEngine#removeRttLi
stener} and |
| 628 * {@link org.chromium.net.ExperimentalCronetEngine#removeThroughputListener
}. |
| 629 * The estimator uses memory and CPU only when enabled. |
| 630 * @param value {@code true} to enable network quality estimator, |
| 631 * {@code false} to disable. |
| 632 * @return the builder to facilitate chaining. |
| 633 */ |
| 634 @Override |
| 635 public CronetEngineBuilderImpl enableNetworkQualityEstimator(boolean value)
{ |
| 636 mNetworkQualityEstimatorEnabled = value; |
| 637 return this; |
| 638 } |
| 639 |
| 640 String certVerifierData() { |
| 641 return mCertVerifierData; |
| 642 } |
| 643 |
| 644 /** |
| 645 * Returns {@link Context} for builder. |
| 646 * |
| 647 * @return {@link Context} for builder. |
| 648 */ |
| 649 public Context getContext() { |
| 650 return mContext; |
| 651 } |
| 652 |
| 653 /** |
| 654 * Build a {@link CronetEngine} using this builder's configuration. |
| 655 * @return constructed {@link CronetEngine}. |
| 656 */ |
| 657 public CronetEngine build() { |
| 658 if (getUserAgent() == null) { |
| 659 setUserAgent(getDefaultUserAgent()); |
| 660 } |
| 661 CronetEngine cronetEngine = null; |
| 662 if (!legacyMode()) { |
| 663 cronetEngine = CronetEngineBase.createCronetEngine(this); |
| 664 } |
| 665 if (cronetEngine == null) { |
| 666 cronetEngine = new JavaCronetEngine(getUserAgent()); |
| 667 } |
| 668 Log.i(TAG, "Using network stack: " + cronetEngine.getVersionString()); |
| 669 // Clear MOCK_CERT_VERIFIER reference if there is any, since |
| 670 // the ownership has been transferred to the engine. |
| 671 mMockCertVerifier = 0; |
| 672 return cronetEngine; |
| 673 } |
| 674 } |
OLD | NEW |