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