mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Upgrade to OkHttp3
Summary: Update to [OkHttp](https://github.com/square/okhttp) to [OkHttp3](https://publicobject.com/2015/12/12/com-squareup-okhttp3/) We must also update: - Fresco to 0.10.0 - okio to 1.8.0 **Motivation** Reasons for upgrading: * Issue #4021 * "We discovered that RN Android sometimes fails to connect to the latest stable version of NGINX when HTTP/2 is enabled. We aren't seeing errors with other HTTP clients so we think it's specific to RN and OkHttp. Square has fixed several HTTP/2 bugs over the past eight months." - ide * OkHttp3 will be maintained & improved, but OkHttp2 will only receive [security fixes](https://publicobject.com/2016/02/11/okhttp-certificate-pinning-vulnerability/) * Cleaner APIs - "Get and Set prefixes are avoided" * Deprecated/Removed - HttpURLConnection & Apache HTTP * React Native apps are currently being forced to bundle two versions of OkHttp (v2 & v3), if another library uses v3 * Improved WebSocket performance - [CHANGELOG.md](https://github.com/square/okhttp/blob/master Closes https://github.com/facebook/react-native/pull/6113 Reviewed By: andreicoman11, lexs Differential Revision: D3292375 Pulled By: bestander fbshipit-source-id: 7c7043eaa2ea63f95854108b401c4066098d67f7
This commit is contained in:
committed by
Facebook Github Bot 1
parent
5047f6f54c
commit
6bbaff2944
@@ -5,17 +5,16 @@ android_library(
|
||||
manifest = 'AndroidManifest.xml',
|
||||
srcs = glob(['**/*.java']),
|
||||
deps = [
|
||||
react_native_target('res:devsupport'),
|
||||
react_native_dep('libraries/fbcore/src/main/java/com/facebook/common/logging:logging'),
|
||||
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
|
||||
react_native_dep('third-party/java/jsr-305:jsr-305'),
|
||||
react_native_dep('third-party/java/okhttp:okhttp3'),
|
||||
react_native_dep('third-party/java/okio:okio'),
|
||||
react_native_target('java/com/facebook/react/bridge:bridge'),
|
||||
react_native_target('java/com/facebook/react/common:common'),
|
||||
react_native_target('java/com/facebook/react/modules/debug:debug'),
|
||||
react_native_target('java/com/facebook/react/modules/systeminfo:systeminfo'),
|
||||
react_native_dep('libraries/fbcore/src/main/java/com/facebook/common/logging:logging'),
|
||||
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
|
||||
react_native_dep('third-party/java/jsr-305:jsr-305'),
|
||||
react_native_dep('third-party/java/okhttp:okhttp'),
|
||||
react_native_dep('third-party/java/okio:okio'),
|
||||
|
||||
react_native_target('res:devsupport'),
|
||||
],
|
||||
visibility = [
|
||||
'PUBLIC',
|
||||
|
||||
@@ -17,14 +17,8 @@ import com.facebook.common.logging.FLog;
|
||||
import com.facebook.infer.annotation.Assertions;
|
||||
import com.facebook.react.bridge.UiThreadUtil;
|
||||
import com.facebook.react.common.ReactConstants;
|
||||
import com.facebook.react.common.network.OkHttpCallUtil;
|
||||
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;
|
||||
import com.squareup.okhttp.Call;
|
||||
import com.squareup.okhttp.Callback;
|
||||
import com.squareup.okhttp.ConnectionPool;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
import com.squareup.okhttp.ResponseBody;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -33,6 +27,13 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okio.Okio;
|
||||
import okio.Sink;
|
||||
|
||||
@@ -91,12 +92,12 @@ public class DevServerHelper {
|
||||
|
||||
public DevServerHelper(DevInternalSettings settings) {
|
||||
mSettings = settings;
|
||||
mClient = new OkHttpClient();
|
||||
mClient.setConnectTimeout(HTTP_CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
|
||||
mClient = new OkHttpClient.Builder()
|
||||
.connectTimeout(HTTP_CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.writeTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.build();
|
||||
|
||||
// No read or write timeouts by default
|
||||
mClient.setReadTimeout(0, TimeUnit.MILLISECONDS);
|
||||
mClient.setWriteTimeout(0, TimeUnit.MILLISECONDS);
|
||||
mRestartOnChangePollingHandler = new Handler();
|
||||
}
|
||||
|
||||
@@ -176,7 +177,7 @@ public class DevServerHelper {
|
||||
mDownloadBundleFromURLCall = Assertions.assertNotNull(mClient.newCall(request));
|
||||
mDownloadBundleFromURLCall.enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Request request, IOException e) {
|
||||
public void onFailure(Call call, IOException e) {
|
||||
// ignore callback if call was cancelled
|
||||
if (mDownloadBundleFromURLCall == null || mDownloadBundleFromURLCall.isCanceled()) {
|
||||
mDownloadBundleFromURLCall = null;
|
||||
@@ -191,12 +192,12 @@ public class DevServerHelper {
|
||||
.append("\u2022 Ensure that your device/emulator is connected to your machine and has USB debugging enabled - run 'adb devices' to see a list of connected devices\n")
|
||||
.append("\u2022 If you're on a physical device connected to the same machine, run 'adb reverse tcp:8081 tcp:8081' to forward requests from your device\n")
|
||||
.append("\u2022 If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:8081\n\n")
|
||||
.append("URL: ").append(request.urlString());
|
||||
.append("URL: ").append(call.request().url().toString());
|
||||
callback.onFailure(new DebugServerException(sb.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Response response) throws IOException {
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
// ignore callback if call was cancelled
|
||||
if (mDownloadBundleFromURLCall == null || mDownloadBundleFromURLCall.isCanceled()) {
|
||||
mDownloadBundleFromURLCall = null;
|
||||
@@ -213,7 +214,7 @@ public class DevServerHelper {
|
||||
} else {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("The development server returned response error code: ").append(response.code()).append("\n\n")
|
||||
.append("URL: ").append(request.urlString()).append("\n\n")
|
||||
.append("URL: ").append(call.request().url().toString()).append("\n\n")
|
||||
.append("Body:\n")
|
||||
.append(body);
|
||||
callback.onFailure(new DebugServerException(sb.toString()));
|
||||
@@ -251,7 +252,7 @@ public class DevServerHelper {
|
||||
mClient.newCall(request).enqueue(
|
||||
new Callback() {
|
||||
@Override
|
||||
public void onFailure(Request request, IOException e) {
|
||||
public void onFailure(Call call, IOException e) {
|
||||
FLog.w(
|
||||
ReactConstants.TAG,
|
||||
"The packager does not seem to be running as we got an IOException requesting " +
|
||||
@@ -260,7 +261,7 @@ public class DevServerHelper {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Response response) throws IOException {
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
if (!response.isSuccessful()) {
|
||||
FLog.e(
|
||||
ReactConstants.TAG,
|
||||
@@ -297,7 +298,7 @@ public class DevServerHelper {
|
||||
mOnChangePollingEnabled = false;
|
||||
mRestartOnChangePollingHandler.removeCallbacksAndMessages(null);
|
||||
if (mOnChangePollingClient != null) {
|
||||
mOnChangePollingClient.cancel(this);
|
||||
OkHttpCallUtil.cancelTag(mOnChangePollingClient, this);
|
||||
mOnChangePollingClient = null;
|
||||
}
|
||||
mOnServerContentChangeListener = null;
|
||||
@@ -311,10 +312,10 @@ public class DevServerHelper {
|
||||
}
|
||||
mOnChangePollingEnabled = true;
|
||||
mOnServerContentChangeListener = onServerContentChangeListener;
|
||||
mOnChangePollingClient = new OkHttpClient();
|
||||
mOnChangePollingClient
|
||||
.setConnectionPool(new ConnectionPool(1, LONG_POLL_KEEP_ALIVE_DURATION_MS))
|
||||
.setConnectTimeout(HTTP_CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
|
||||
mOnChangePollingClient = new OkHttpClient.Builder()
|
||||
.connectionPool(new ConnectionPool(1, LONG_POLL_KEEP_ALIVE_DURATION_MS, TimeUnit.MINUTES))
|
||||
.connectTimeout(HTTP_CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
|
||||
.build();
|
||||
enqueueOnChangeEndpointLongPolling();
|
||||
}
|
||||
|
||||
@@ -338,7 +339,7 @@ public class DevServerHelper {
|
||||
Request request = new Request.Builder().url(createOnChangeEndpointUrl()).tag(this).build();
|
||||
Assertions.assertNotNull(mOnChangePollingClient).newCall(request).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Request request, IOException e) {
|
||||
public void onFailure(Call call, IOException e) {
|
||||
if (mOnChangePollingEnabled) {
|
||||
// this runnable is used by onchange endpoint poller to delay subsequent requests in case
|
||||
// of a failure, so that we don't flood network queue with frequent requests in case when
|
||||
@@ -356,7 +357,7 @@ public class DevServerHelper {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Response response) throws IOException {
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
handleOnChangePollingResponse(response.code() == 205);
|
||||
}
|
||||
});
|
||||
@@ -376,13 +377,13 @@ public class DevServerHelper {
|
||||
.build();
|
||||
mClient.newCall(request).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Request request, IOException e) {
|
||||
public void onFailure(Call call, IOException e) {
|
||||
// ignore HTTP call response, this is just to open a debugger page and there is no reason
|
||||
// to report failures from here
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Response response) throws IOException {
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
// ignore HTTP call response - see above
|
||||
}
|
||||
});
|
||||
|
||||
@@ -30,10 +30,10 @@ import com.facebook.react.common.MapBuilder;
|
||||
import com.facebook.react.common.ReactConstants;
|
||||
import com.facebook.react.devsupport.StackTraceHelper.StackFrame;
|
||||
|
||||
import com.squareup.okhttp.MediaType;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.RequestBody;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user