Skip to content

Commit

Permalink
Fixed ajax and fetch interceptor when the data/body sent is not a str…
Browse files Browse the repository at this point in the history
…ing, fix #724, updated nodejs test server
  • Loading branch information
pichillilorenzo committed Mar 18, 2021
1 parent bcc5fc6 commit 6f356be
Show file tree
Hide file tree
Showing 14 changed files with 1,364 additions and 395 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,12 @@
- Fixed wrong mapping of `NavigationAction` class on Android for `androidHasGesture` and `androidIsRedirect` properties
- Fixed "Pull to refresh creating problem in some webpages on Android" [#719](https://github.com/pichillilorenzo/flutter_inappwebview/issues/719)
- Fixed iOS sometimes `scrollView.contentSize` doesn't fit all the `frame.size` available
- Fixed ajax and fetch interceptor when the data/body sent is not a string
- Fixed "InAppLocalhostServer - Error: type 'List<dynamic>' is not a subtype of type 'List<int>' in type cast" [#724](https://github.com/pichillilorenzo/flutter_inappwebview/issues/724)

### BREAKING CHANGES

- `FetchRequest.body` is a dynamic type now

## 5.1.0+4

Expand Down
Expand Up @@ -274,4 +274,17 @@ public static boolean objEquals(@Nullable Object a, @Nullable Object b) {
public static String replaceAll(String s, String oldString, String newString) {
return TextUtils.join(newString, s.split(Pattern.quote(oldString)));
}

public static void log(String tag, String message) {
// Split by line, then ensure each line can fit into Log's maximum length.
for (int i = 0, length = message.length(); i < length; i++) {
int newline = message.indexOf('\n', i);
newline = newline != -1 ? newline : length;
do {
int end = Math.min(newline, i + 4000);
Log.d(tag, message.substring(i, end));
i = end;
} while (i < newline);
}
}
}
Expand Up @@ -183,46 +183,63 @@ public class InterceptAjaxRequestJS {
" this.addEventListener('error', handleEvent);" +
" this.addEventListener('abort', handleEvent);" +
" this.addEventListener('timeout', handleEvent);" +
" var ajaxRequest = {" +
" data: data," +
" method: this._flutter_inappwebview_method," +
" url: this._flutter_inappwebview_url," +
" isAsync: this._flutter_inappwebview_isAsync," +
" user: this._flutter_inappwebview_user," +
" password: this._flutter_inappwebview_password," +
" withCredentials: this.withCredentials," +
" headers: this._flutter_inappwebview_request_headers," +
" responseType: this.responseType" +
" };" +
" window." + JavaScriptBridgeJS.JAVASCRIPT_BRIDGE_NAME + ".callHandler('shouldInterceptAjaxRequest', ajaxRequest).then(function(result) {" +
" if (result != null) {" +
" switch (result.action) {" +
" case 0:" +
" " + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".convertBodyRequest(data).then(function(data) {" +
" var ajaxRequest = {" +
" data: data," +
" method: self._flutter_inappwebview_method," +
" url: self._flutter_inappwebview_url," +
" isAsync: self._flutter_inappwebview_isAsync," +
" user: self._flutter_inappwebview_user," +
" password: self._flutter_inappwebview_password," +
" withCredentials: self.withCredentials," +
" headers: self._flutter_inappwebview_request_headers," +
" responseType: self.responseType" +
" };" +
" window." + JavaScriptBridgeJS.JAVASCRIPT_BRIDGE_NAME + ".callHandler('shouldInterceptAjaxRequest', ajaxRequest).then(function(result) {" +
" if (result != null) {" +
" switch (result.action) {" +
" case 0:" +
" self.abort();" +
" return;" +
" };" +
" if (result.data != null && !" + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".isString(result.data) && result.data.length > 0) {" +
" var bodyString = " + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".arrayBufferToString(result.data);" +
" if (" + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".isBodyFormData(bodyString)) {" +
" var formDataContentType = " + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".getFormDataContentType(bodyString);" +
" if (result.headers != null) {" +
" result.headers['Content-Type'] = result.headers['Content-Type'] == null ? formDataContentType : result.headers['Content-Type'];" +
" } else {" +
" result.headers = { 'Content-Type': formDataContentType };" +
" }" +
" }" +
" }" +
" if (" + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".isString(result.data) || result.data == null) {" +
" data = result.data;" +
" } else if (result.data.length > 0) {" +
" data = new Uint8Array(result.data);" +
" }" +
" self.withCredentials = result.withCredentials;" +
" if (result.responseType != null) {" +
" self.responseType = result.responseType;" +
" };" +
" for (var header in result.headers) {" +
" var value = result.headers[header];" +
" var flutter_inappwebview_value = self._flutter_inappwebview_request_headers[header];" +
" if (flutter_inappwebview_value == null) {" +
" self._flutter_inappwebview_request_headers[header] = value;" +
" } else {" +
" self._flutter_inappwebview_request_headers[header] += ', ' + value;" +
" }" +
" setRequestHeader.call(self, header, value);" +
" };" +
" if ((self._flutter_inappwebview_method != result.method && result.method != null) || (self._flutter_inappwebview_url != result.url && result.url != null)) {" +
" self.abort();" +
" self.open(result.method, result.url, result.isAsync, result.user, result.password);" +
" return;" +
" };" +
" data = result.data;" +
" self.withCredentials = result.withCredentials;" +
" if (result.responseType != null) {" +
" self.responseType = result.responseType;" +
" };" +
" for (var header in result.headers) {" +
" var value = result.headers[header];" +
" var flutter_inappwebview_value = self._flutter_inappwebview_request_headers[header];" +
" if (flutter_inappwebview_value == null) {" +
" self._flutter_inappwebview_request_headers[header] = value;" +
" } else {" +
" self._flutter_inappwebview_request_headers[header] += ', ' + value;" +
" }" +
" setRequestHeader.call(self, header, value);" +
" };" +
" if ((self._flutter_inappwebview_method != result.method && result.method != null) || (self._flutter_inappwebview_url != result.url && result.url != null)) {" +
" self.abort();" +
" self.open(result.method, result.url, result.isAsync, result.user, result.password);" +
" return;" +
" }" +
" }" +
" send.call(self, data);" +
" send.call(self, data);" +
" });" +
" });" +
" } else {" +
" send.call(this, data);" +
Expand Down
Expand Up @@ -21,68 +21,6 @@ public class InterceptFetchRequestJS {
" if (fetch == null) {" +
" return;" +
" }" +
" function convertHeadersToJson(headers) {" +
" var headersObj = {};" +
" for (var header of headers.keys()) {" +
" var value = headers.get(header);" +
" headersObj[header] = value;" +
" }" +
" return headersObj;" +
" }" +
" function convertJsonToHeaders(headersJson) {" +
" return new Headers(headersJson);" +
" }" +
" function convertBodyToArray(body) {" +
" return new Response(body).arrayBuffer().then(function(arrayBuffer) {" +
" var arr = Array.from(new Uint8Array(arrayBuffer));" +
" return arr;" +
" })" +
" }" +
" function convertArrayIntBodyToUint8Array(arrayIntBody) {" +
" return new Uint8Array(arrayIntBody);" +
" }" +
" function convertCredentialsToJson(credentials) {" +
" var credentialsObj = {};" +
" if (window.FederatedCredential != null && credentials instanceof FederatedCredential) {" +
" credentialsObj.type = credentials.type;" +
" credentialsObj.id = credentials.id;" +
" credentialsObj.name = credentials.name;" +
" credentialsObj.protocol = credentials.protocol;" +
" credentialsObj.provider = credentials.provider;" +
" credentialsObj.iconURL = credentials.iconURL;" +
" } else if (window.PasswordCredential != null && credentials instanceof PasswordCredential) {" +
" credentialsObj.type = credentials.type;" +
" credentialsObj.id = credentials.id;" +
" credentialsObj.name = credentials.name;" +
" credentialsObj.password = credentials.password;" +
" credentialsObj.iconURL = credentials.iconURL;" +
" } else {" +
" credentialsObj.type = 'default';" +
" credentialsObj.value = credentials;" +
" }" +
" }" +
" function convertJsonToCredential(credentialsJson) {" +
" var credentials;" +
" if (window.FederatedCredential != null && credentialsJson.type === 'federated') {" +
" credentials = new FederatedCredential({" +
" id: credentialsJson.id," +
" name: credentialsJson.name," +
" protocol: credentialsJson.protocol," +
" provider: credentialsJson.provider," +
" iconURL: credentialsJson.iconURL" +
" });" +
" } else if (window.PasswordCredential != null && credentialsJson.type === 'password') {" +
" credentials = new PasswordCredential({" +
" id: credentialsJson.id," +
" name: credentialsJson.name," +
" password: credentialsJson.password," +
" iconURL: credentialsJson.iconURL" +
" });" +
" } else {" +
" credentials = credentialsJson;" +
" }" +
" return credentials;" +
" }" +
" window.fetch = async function(resource, init) {" +
" var w = (window.top == null || window.top === window) ? window : window.top;" +
" if (w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_FETCH_REQUEST_JS_SOURCE + " == null || w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_FETCH_REQUEST_JS_SOURCE + " == true) {" +
Expand Down Expand Up @@ -114,7 +52,7 @@ public class InterceptFetchRequestJS {
" fetchRequest.integrity = resource.integrity;" +
" fetchRequest.keepalive = resource.keepalive;" +
" } else {" +
" fetchRequest.url = resource;" +
" fetchRequest.url = resource != null ? resource.toString() : null;" +
" if (init != null) {" +
" fetchRequest.method = init.method;" +
" fetchRequest.headers = init.headers;" +
Expand All @@ -130,10 +68,10 @@ public class InterceptFetchRequestJS {
" }" +
" }" +
" if (fetchRequest.headers instanceof Headers) {" +
" fetchRequest.headers = convertHeadersToJson(fetchRequest.headers);" +
" fetchRequest.headers = " + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".convertHeadersToJson(fetchRequest.headers);" +
" }" +
" fetchRequest.credentials = convertCredentialsToJson(fetchRequest.credentials);" +
" return convertBodyToArray(fetchRequest.body).then(function(body) {" +
" fetchRequest.credentials = " + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".convertCredentialsToJson(fetchRequest.credentials);" +
" return " + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".convertBodyRequest(fetchRequest.body).then(function(body) {" +
" fetchRequest.body = body;" +
" return window." + JavaScriptBridgeJS.JAVASCRIPT_BRIDGE_NAME + ".callHandler('shouldInterceptFetchRequest', fetchRequest).then(function(result) {" +
" if (result != null) {" +
Expand All @@ -150,24 +88,37 @@ public class InterceptFetchRequestJS {
" controller.abort();" +
" break;" +
" }" +
" resource = (result.url != null) ? result.url : resource;" +
" if (result.body != null && !" + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".isString(result.body) && result.body.length > 0) {" +
" var bodyString = " + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".arrayBufferToString(result.body);" +
" if (" + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".isBodyFormData(bodyString)) {" +
" var formDataContentType = " + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".getFormDataContentType(bodyString);" +
" if (result.headers != null) {" +
" result.headers['Content-Type'] = result.headers['Content-Type'] == null ? formDataContentType : result.headers['Content-Type'];" +
" } else {" +
" result.headers = { 'Content-Type': formDataContentType };" +
" }" +
" }" +
" }" +
" resource = result.url;" +
" if (init == null) {" +
" init = {};" +
" }" +
" if (result.method != null && result.method.length > 0) {" +
" init.method = result.method;" +
" }" +
" if (result.headers != null && Object.keys(result.headers).length > 0) {" +
" init.headers = convertJsonToHeaders(result.headers);" +
" init.headers = " + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".convertJsonToHeaders(result.headers);" +
" }" +
" if (result.body != null && result.body.length > 0) {" +
" init.body = convertArrayIntBodyToUint8Array(result.body);" +
" if (" + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".isString(result.body) || result.body == null) {" +
" init.body = result.body;" +
" } else if (result.body.length > 0) {" +
" init.body = new Uint8Array(result.body);" +
" }" +
" if (result.mode != null && result.mode.length > 0) {" +
" init.mode = result.mode;" +
" }" +
" if (result.credentials != null) {" +
" init.credentials = convertJsonToCredential(result.credentials);" +
" init.credentials = " + JavaScriptBridgeJS.JAVASCRIPT_UTIL_VAR_NAME + ".convertJsonToCredential(result.credentials);" +
" }" +
" if (result.cache != null && result.cache.length > 0) {" +
" init.cache = result.cache;" +
Expand Down

0 comments on commit 6f356be

Please sign in to comment.