-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inject configured OkHttpClient to networking module using MainPackageConfig #14068
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
/** | ||
* 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 com.facebook.react.common.ReactConstants; | ||
|
||
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. This method enables it. | ||
*/ | ||
private 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 e) { | ||
FLog.e(ReactConstants.TAG, "Error while enabling TLS 1.2", e); | ||
} | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,16 +9,6 @@ | |
|
||
package com.facebook.react.modules.network; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import android.util.Base64; | ||
|
||
import com.facebook.react.bridge.Arguments; | ||
|
@@ -33,6 +23,16 @@ | |
import com.facebook.react.module.annotations.ReactModule; | ||
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import okhttp3.Call; | ||
import okhttp3.Callback; | ||
import okhttp3.CookieJar; | ||
|
@@ -76,43 +76,58 @@ public final class NetworkingModule extends ReactContextBaseJavaModule { | |
/* package */ NetworkingModule( | ||
ReactApplicationContext reactContext, | ||
@Nullable String defaultUserAgent, | ||
OkHttpClient client, | ||
@Nullable OkHttpClientProvider httpClientProvider, | ||
@Nullable List<NetworkInterceptorCreator> networkInterceptorCreators) { | ||
super(reactContext); | ||
|
||
mClient = createOkHttpClient(httpClientProvider, networkInterceptorCreators); | ||
mCookieHandler = new ForwardingCookieHandler(reactContext); | ||
mCookieJarContainer = (CookieJarContainer) mClient.cookieJar(); | ||
mShuttingDown = false; | ||
mDefaultUserAgent = defaultUserAgent; | ||
mRequestIds = new HashSet<>(); | ||
} | ||
|
||
private OkHttpClient createOkHttpClient( | ||
@Nullable OkHttpClientProvider httpClientProvider, | ||
@Nullable List<NetworkInterceptorCreator> networkInterceptorCreators) { | ||
OkHttpClient client = httpClientProvider == null ? new DefaultOkHttpProvider().get() : httpClientProvider.get(); | ||
if (networkInterceptorCreators != null) { | ||
OkHttpClient.Builder clientBuilder = client.newBuilder(); | ||
for (NetworkInterceptorCreator networkInterceptorCreator : networkInterceptorCreators) { | ||
clientBuilder.addNetworkInterceptor(networkInterceptorCreator.create()); | ||
} | ||
client = clientBuilder.build(); | ||
} | ||
mClient = client; | ||
mCookieHandler = new ForwardingCookieHandler(reactContext); | ||
mCookieJarContainer = (CookieJarContainer) mClient.cookieJar(); | ||
mShuttingDown = false; | ||
mDefaultUserAgent = defaultUserAgent; | ||
mRequestIds = new HashSet<>(); | ||
return client; | ||
} | ||
|
||
/** | ||
* @param context the ReactContext of the application | ||
* @param defaultUserAgent the User-Agent header that will be set for all requests where the | ||
* caller does not provide one explicitly | ||
* @param client the {@link OkHttpClient} to be used for networking | ||
* @param httpClientProvider {@link OkHttpClientProvider} to provider {@link OkHttpClient} | ||
*/ | ||
/* package */ NetworkingModule( | ||
ReactApplicationContext context, | ||
@Nullable String defaultUserAgent, | ||
OkHttpClient client) { | ||
this(context, defaultUserAgent, client, null); | ||
@Nullable OkHttpClientProvider httpClientProvider) { | ||
this(context, defaultUserAgent, httpClientProvider, null); | ||
} | ||
|
||
/** | ||
* @param context the ReactContext of the application | ||
*/ | ||
public NetworkingModule(final ReactApplicationContext context) { | ||
this(context, null, OkHttpClientProvider.createClient(), null); | ||
this(context, null, null, null); | ||
} | ||
|
||
/** | ||
* @param context the ReactContext of the application | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit |
||
* @param httpClientProvider {@link OkHttpClientProvider} to provide {@link OkHttpClient} | ||
*/ | ||
public NetworkingModule(final ReactApplicationContext context, @Nullable OkHttpClientProvider httpClientProvider) { | ||
this(context, null, httpClientProvider, null); | ||
} | ||
|
||
/** | ||
|
@@ -123,7 +138,7 @@ public NetworkingModule(final ReactApplicationContext context) { | |
public NetworkingModule( | ||
ReactApplicationContext context, | ||
List<NetworkInterceptorCreator> networkInterceptorCreators) { | ||
this(context, null, OkHttpClientProvider.createClient(), networkInterceptorCreators); | ||
this(context, null, null, networkInterceptorCreators); | ||
} | ||
|
||
/** | ||
|
@@ -132,7 +147,7 @@ public NetworkingModule( | |
* caller does not provide one explicitly | ||
*/ | ||
public NetworkingModule(ReactApplicationContext context, String defaultUserAgent) { | ||
this(context, defaultUserAgent, OkHttpClientProvider.createClient(), null); | ||
this(context, defaultUserAgent, null, null); | ||
} | ||
|
||
@Override | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If fetching images should not be affected by the HTTP provider from
MainPackageConfig
we should document theMainPackageConfig
only affects thefetch
API.It's probably easier, however, to just use the HTTP client configured in
MainPackageConfig
also here (for fetching images) to avoid confusion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't looked in to the details of how Fresco is using this client. But I guess, Fresco might need its own customised client configuration.
For now, I will add a comment in
MainPackageConfig
. Will take a look at this later. Would be great if someone with Fresco context can share some light.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK sounds good, someone else can do that separately and we can keep this pull request simpler.