Skip to content
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

Fix cloudflare redirects #2180

Merged
merged 2 commits into from
Feb 4, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions patches/react-native-webview+11.0.2.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java b/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
index 2190ae7..cd05399 100644
index 2190ae7..c2414ce 100644
--- a/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
+++ b/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
@@ -1,5 +1,27 @@
Expand Down Expand Up @@ -30,18 +30,28 @@ index 2190ae7..cd05399 100644
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.DownloadManager;
@@ -135,6 +157,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -129,13 +151,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
public static final int COMMAND_LOAD_URL = 7;
public static final int COMMAND_FOCUS = 8;

+ protected static final String MIME_UNKNOWN = "application/octet-stream";
+ protected static final String HTML_ENCODING = "UTF-8";
+ protected static final long BYTES_IN_MEGABYTE = 1000000;
+
// android commands
public static final int COMMAND_CLEAR_FORM_DATA = 1000;
public static final int COMMAND_CLEAR_CACHE = 1001;
public static final int COMMAND_CLEAR_HISTORY = 1002;

protected static final String REACT_CLASS = "RNCWebView";
- protected static final String HTML_ENCODING = "UTF-8";
+
+ protected static final String HEADER_CONTENT_TYPE = "content-type";
+ protected static final String UNKNOWN_MIME_TYPE = "application/octet-stream";
+
protected static final String HTML_ENCODING = "UTF-8";
protected static final String HTML_MIME_TYPE = "text/html";
protected static final String JAVASCRIPT_INTERFACE = "ReactNativeWebView";
@@ -150,11 +176,20 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
protected static final String HTTP_METHOD_POST = "POST";
@@ -150,11 +178,20 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
protected @Nullable String mUserAgent = null;
protected @Nullable String mUserAgentWithApplicationName = null;

Expand All @@ -62,15 +72,15 @@ index 2190ae7..cd05399 100644
}

public RNCWebViewManager(WebViewConfig webViewConfig) {
@@ -181,6 +216,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -181,6 +218,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected WebView createViewInstance(ThemedReactContext reactContext) {
RNCWebView webView = createRNCWebViewInstance(reactContext);
+ userAgent = webView.getSettings().getUserAgentString();
setupWebChromeClient(reactContext, webView);
reactContext.addLifecycleEventListener(webView);
mWebViewConfig.configWebView(webView);
@@ -246,9 +282,80 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -246,9 +284,96 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
}
});

Expand Down Expand Up @@ -99,16 +109,32 @@ index 2190ae7..cd05399 100644
+ urlString.contains("|");
+ }
+
+ private Boolean responseRequiresJSInjection(Response response) {
+ // we don't want to inject JS into redirects
+public static Boolean responseRequiresJSInjection(Response response) {
+ if (response.isRedirect()) {
+ return false;
+ }
+ final String contentTypeAndCharset = response.header(HEADER_CONTENT_TYPE, MIME_UNKNOWN);
+ final int responseCode = response.code();
+
+ boolean contentTypeIsHtml = contentTypeAndCharset.startsWith(HTML_MIME_TYPE);
+ boolean responseCodeIsInjectible = responseCode == 200;
+ String responseBody = "";
+
+ if (contentTypeIsHtml && responseCodeIsInjectible) {
+ try {
+ assert response.body() != null;
+ responseBody = response.peekBody(BYTES_IN_MEGABYTE).string();
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+
+ // ...okhttp appends charset to content type sometimes, like "text/html; charset=UTF8"
+ final String contentTypeAndCharset = response.header(HEADER_CONTENT_TYPE, UNKNOWN_MIME_TYPE);
+ // ...and we only want to inject it in to HTML, really
+ return contentTypeAndCharset.startsWith(HTML_MIME_TYPE);
+
+ boolean responseBodyContainsHTMLLikeString = responseBody.matches("[\\S\\s]*<[a-z]+[\\S\\s]*>[\\S\\s]*");
+ return responseBodyContainsHTMLLikeString;
+ } else {
+ return false;
+ }
+ }
+
+ public WebResourceResponse shouldInterceptRequest(WebResourceRequest request, Boolean onlyMainFrame, RNCWebView webView) {
Expand Down Expand Up @@ -151,7 +177,7 @@ index 2190ae7..cd05399 100644
@ReactProp(name = "javaScriptEnabled")
public void setJavaScriptEnabled(WebView view, boolean enabled) {
view.getSettings().setJavaScriptEnabled(enabled);
@@ -778,8 +885,94 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -778,8 +903,94 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
}
}

Expand Down Expand Up @@ -247,7 +273,7 @@ index 2190ae7..cd05399 100644
protected boolean mLastLoadFailed = false;
protected @Nullable
ReadableArray mUrlPrefixesForDefaultIntent;
@@ -808,9 +1001,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -808,9 +1019,6 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
super.onPageStarted(webView, url, favicon);
mLastLoadFailed = false;

Expand All @@ -257,7 +283,7 @@ index 2190ae7..cd05399 100644
dispatchEvent(
webView,
new TopLoadingStartEvent(
@@ -818,6 +1008,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -818,6 +1026,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
createWebViewEvent(webView, url)));
}

Expand All @@ -274,7 +300,7 @@ index 2190ae7..cd05399 100644
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final RNCWebView rncWebView = (RNCWebView) view;
@@ -870,6 +1070,21 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@@ -870,6 +1088,21 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Expand Down