-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject configured OkHttpClient to networking module using MainPackage…
…Config - Most brownfield application will have their OkHttpClient configured with custom headers, interceptors, logging, etc. This will enable them to inject the same http client to react-native networking subsystem. This also allows them to use fetch in javascript.
- Loading branch information
1 parent
1dd7bc1
commit dd205a1
Showing
8 changed files
with
202 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
ReactAndroid/src/main/java/com/facebook/react/modules/network/DefaultOkHttpProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.modules.network; | ||
|
||
import android.os.Build; | ||
|
||
import com.facebook.common.logging.FLog; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import okhttp3.ConnectionSpec; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.TlsVersion; | ||
|
||
public class DefaultOkHttpProvider implements OkHttpClientProvider { | ||
|
||
public OkHttpClient get() { | ||
// No timeouts by default | ||
OkHttpClient.Builder client = new OkHttpClient.Builder() | ||
.connectTimeout(0, TimeUnit.MILLISECONDS) | ||
.readTimeout(0, TimeUnit.MILLISECONDS) | ||
.writeTimeout(0, TimeUnit.MILLISECONDS) | ||
.cookieJar(new ReactCookieJarContainer()); | ||
|
||
return enableTls12OnPreLollipop(client).build(); | ||
} | ||
|
||
/* | ||
On Android 4.1-4.4 (API level 16 to 19) TLS 1.1 and 1.2 are | ||
available but not enabled by default. The following method | ||
enables it. | ||
*/ | ||
public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder client) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { | ||
try { | ||
client.sslSocketFactory(new TLSSocketFactory()); | ||
|
||
ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) | ||
.tlsVersions(TlsVersion.TLS_1_2) | ||
.build(); | ||
|
||
List<ConnectionSpec> specs = new ArrayList<>(); | ||
specs.add(cs); | ||
specs.add(ConnectionSpec.COMPATIBLE_TLS); | ||
specs.add(ConnectionSpec.CLEARTEXT); | ||
|
||
client.connectionSpecs(specs); | ||
} catch (Exception exc) { | ||
FLog.e("OkHttpClientProvider", "Error while enabling TLS 1.2", exc); | ||
} | ||
} | ||
|
||
return client; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.