Commit fed2da63 authored by Lorenzo Pichilli's avatar Lorenzo Pichilli

fixed issue with headers in javascript code for intercepting ajax requests

parent a11936c1
This diff is collapsed.
...@@ -146,10 +146,12 @@ final public class InAppWebView extends InputAwareWebView { ...@@ -146,10 +146,12 @@ final public class InAppWebView extends InputAwareWebView {
" this._flutter_inappbrowser_isAsync = isAsync;" + " this._flutter_inappbrowser_isAsync = isAsync;" +
" this._flutter_inappbrowser_user = user;" + " this._flutter_inappbrowser_user = user;" +
" this._flutter_inappbrowser_password = password;" + " this._flutter_inappbrowser_password = password;" +
" this._flutter_inappbrowser_request_headers = {};" +
" open.call(this, method, url, isAsync, user, password);" + " open.call(this, method, url, isAsync, user, password);" +
" };" + " };" +
" ajax.prototype.setRequestHeader = function(header, value) {" + " ajax.prototype.setRequestHeader = function(header, value) {" +
" this._flutter_inappbrowser_request_headers[header] = value;" + " this._flutter_inappbrowser_request_headers[header] = value;" +
" setRequestHeader.call(this, header, value);" +
" };" + " };" +
" function handleEvent(e) {" + " function handleEvent(e) {" +
" var self = this;" + " var self = this;" +
...@@ -290,10 +292,12 @@ final public class InAppWebView extends InputAwareWebView { ...@@ -290,10 +292,12 @@ final public class InAppWebView extends InputAwareWebView {
" };" + " };" +
" for (var header in result.headers) {" + " for (var header in result.headers) {" +
" var value = result.headers[header];" + " var value = result.headers[header];" +
" var flutter_inappbrowser_value = self._flutter_inappbrowser_request_headers[header];" +
" if (flutter_inappbrowser_value == null) {" +
" self._flutter_inappbrowser_request_headers[header] = value;" + " self._flutter_inappbrowser_request_headers[header] = value;" +
" };" + " } else {" +
" for (var header in self._flutter_inappbrowser_request_headers) {" + " self._flutter_inappbrowser_request_headers[header] += ', ' + value;" +
" var value = self._flutter_inappbrowser_request_headers[header];" + " }" +
" setRequestHeader.call(self, header, value);" + " setRequestHeader.call(self, header, value);" +
" };" + " };" +
" if ((self._flutter_inappbrowser_method != result.method && result.method != null) || (self._flutter_inappbrowser_url != result.url && result.url != null)) {" + " if ((self._flutter_inappbrowser_method != result.method && result.method != null) || (self._flutter_inappbrowser_url != result.url && result.url != null)) {" +
......
...@@ -39,7 +39,7 @@ public class JavaScriptBridgeInterface { ...@@ -39,7 +39,7 @@ public class JavaScriptBridgeInterface {
} }
@JavascriptInterface @JavascriptInterface
public void _callHandler(String handlerName, final String _callHandlerID, String args) { public void _callHandler(final String handlerName, final String _callHandlerID, String args) {
final Map<String, Object> obj = new HashMap<>(); final Map<String, Object> obj = new HashMap<>();
if (inAppBrowserActivity != null) if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid); obj.put("uuid", inAppBrowserActivity.uuid);
...@@ -62,10 +62,10 @@ public class JavaScriptBridgeInterface { ...@@ -62,10 +62,10 @@ public class JavaScriptBridgeInterface {
return; return;
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("window." + name + "[" + _callHandlerID + "](" + json + "); delete window." + name + "[" + _callHandlerID + "];", (ValueCallback<String>) null); webView.evaluateJavascript("if(window." + name + "[" + _callHandlerID + "] != null) {window." + name + "[" + _callHandlerID + "](" + json + "); delete window." + name + "[" + _callHandlerID + "];}", (ValueCallback<String>) null);
} }
else { else {
webView.loadUrl("javascript:window." + name + "[" + _callHandlerID + "](" + json + "); delete window." + name + "[" + _callHandlerID + "];"); webView.loadUrl("javascript:if(window." + name + "[" + _callHandlerID + "] != null) {window." + name + "[" + _callHandlerID + "](" + json + "); delete window." + name + "[" + _callHandlerID + "];}");
} }
} }
......
...@@ -296,10 +296,12 @@ let interceptAjaxRequestsJS = """ ...@@ -296,10 +296,12 @@ let interceptAjaxRequestsJS = """
this._flutter_inappbrowser_isAsync = isAsync; this._flutter_inappbrowser_isAsync = isAsync;
this._flutter_inappbrowser_user = user; this._flutter_inappbrowser_user = user;
this._flutter_inappbrowser_password = password; this._flutter_inappbrowser_password = password;
this._flutter_inappbrowser_request_headers = {};
open.call(this, method, url, isAsync, user, password); open.call(this, method, url, isAsync, user, password);
}; };
ajax.prototype.setRequestHeader = function(header, value) { ajax.prototype.setRequestHeader = function(header, value) {
this._flutter_inappbrowser_request_headers[header] = value; this._flutter_inappbrowser_request_headers[header] = value;
setRequestHeader.call(this, header, value);
}; };
function handleEvent(e) { function handleEvent(e) {
var self = this; var self = this;
...@@ -440,10 +442,12 @@ let interceptAjaxRequestsJS = """ ...@@ -440,10 +442,12 @@ let interceptAjaxRequestsJS = """
}; };
for (var header in result.headers) { for (var header in result.headers) {
var value = result.headers[header]; var value = result.headers[header];
var flutter_inappbrowser_value = self._flutter_inappbrowser_request_headers[header];
if (flutter_inappbrowser_value == null) {
self._flutter_inappbrowser_request_headers[header] = value; self._flutter_inappbrowser_request_headers[header] = value;
}; } else {
for (var header in self._flutter_inappbrowser_request_headers) { self._flutter_inappbrowser_request_headers[header] += ', ' + value;
var value = self._flutter_inappbrowser_request_headers[header]; }
setRequestHeader.call(self, header, value); setRequestHeader.call(self, header, value);
}; };
if ((self._flutter_inappbrowser_method != result.method && result.method != null) || (self._flutter_inappbrowser_url != result.url && result.url != null)) { if ((self._flutter_inappbrowser_method != result.method && result.method != null) || (self._flutter_inappbrowser_url != result.url && result.url != null)) {
...@@ -2021,7 +2025,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi ...@@ -2021,7 +2025,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
if let r = result { if let r = result {
json = r as! String json = r as! String
} }
self.evaluateJavaScript("window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)](\(json)); delete window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)];", completionHandler: nil) self.evaluateJavaScript("if(window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)] != null) {window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)](\(json)); delete window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)];}", completionHandler: nil)
} }
}) })
} }
......
...@@ -591,7 +591,7 @@ class InAppWebViewController { ...@@ -591,7 +591,7 @@ class InAppWebViewController {
String user = argMap["user"]; String user = argMap["user"];
String password = argMap["password"]; String password = argMap["password"];
bool withCredentials = argMap["withCredentials"]; bool withCredentials = argMap["withCredentials"];
Map<dynamic, dynamic> headers = argMap["headers"]; AjaxRequestHeaders headers = AjaxRequestHeaders(argMap["headers"]);
String responseType = argMap["responseType"]; String responseType = argMap["responseType"];
var request = new AjaxRequest(data: data, method: method, url: url, isAsync: isAsync, user: user, password: password, withCredentials: withCredentials, headers: headers, responseType: responseType); var request = new AjaxRequest(data: data, method: method, url: url, isAsync: isAsync, user: user, password: password, withCredentials: withCredentials, headers: headers, responseType: responseType);
...@@ -610,7 +610,7 @@ class InAppWebViewController { ...@@ -610,7 +610,7 @@ class InAppWebViewController {
String user = argMap["user"]; String user = argMap["user"];
String password = argMap["password"]; String password = argMap["password"];
bool withCredentials = argMap["withCredentials"]; bool withCredentials = argMap["withCredentials"];
Map<dynamic, dynamic> headers = argMap["headers"]; AjaxRequestHeaders headers = AjaxRequestHeaders(argMap["headers"]);
int readyState = argMap["readyState"]; int readyState = argMap["readyState"];
int status = argMap["status"]; int status = argMap["status"];
String responseURL = argMap["responseURL"]; String responseURL = argMap["responseURL"];
...@@ -639,7 +639,7 @@ class InAppWebViewController { ...@@ -639,7 +639,7 @@ class InAppWebViewController {
String user = argMap["user"]; String user = argMap["user"];
String password = argMap["password"]; String password = argMap["password"];
bool withCredentials = argMap["withCredentials"]; bool withCredentials = argMap["withCredentials"];
Map<dynamic, dynamic> headers = argMap["headers"]; AjaxRequestHeaders headers = AjaxRequestHeaders(argMap["headers"]);
int readyState = argMap["readyState"]; int readyState = argMap["readyState"];
int status = argMap["status"]; int status = argMap["status"];
String responseURL = argMap["responseURL"]; String responseURL = argMap["responseURL"];
......
...@@ -1064,6 +1064,31 @@ class AjaxRequestReadyState { ...@@ -1064,6 +1064,31 @@ class AjaxRequestReadyState {
int get hashCode => _value.hashCode; int get hashCode => _value.hashCode;
} }
///AjaxRequestHeaders class represents the HTTP headers of an [AjaxRequest].
class AjaxRequestHeaders {
Map<dynamic, dynamic> _headers;
Map<String, dynamic> _newHeaders = {};
AjaxRequestHeaders(this._headers);
///Gets the HTTP headers of the [AjaxRequest].
Map<dynamic, dynamic> getHeaders() {
return this._headers;
}
///Sets/updates an HTTP header of the [AjaxRequest]. If there is already an existing [header] with the same name, the values are merged into one single request header.
///For security reasons, some headers can only be controlled by the user agent.
///These headers include the [forbidden header names](https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name)
///and [forbidden response header names](https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_response_header_name).
void setRequestHeader(String header, String value) {
_newHeaders[header] = value;
}
Map<String, dynamic> toMap() {
return _newHeaders;
}
}
///AjaxRequest class represents a JavaScript [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) object. ///AjaxRequest class represents a JavaScript [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) object.
class AjaxRequest { class AjaxRequest {
///Data passed as a parameter to the `XMLHttpRequest.send()` method. ///Data passed as a parameter to the `XMLHttpRequest.send()` method.
...@@ -1084,7 +1109,7 @@ class AjaxRequest { ...@@ -1084,7 +1109,7 @@ class AjaxRequest {
///In addition, this flag is also used to indicate when cookies are to be ignored in the response. The default is false. ///In addition, this flag is also used to indicate when cookies are to be ignored in the response. The default is false.
bool withCredentials; bool withCredentials;
///The HTTP request headers. ///The HTTP request headers.
Map<dynamic, dynamic> headers; AjaxRequestHeaders headers;
///The state of the `XMLHttpRequest` request. ///The state of the `XMLHttpRequest` request.
AjaxRequestReadyState readyState; AjaxRequestReadyState readyState;
///The numerical HTTP [status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) of the `XMLHttpRequest`'s response. ///The numerical HTTP [status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) of the `XMLHttpRequest`'s response.
...@@ -1128,7 +1153,7 @@ class AjaxRequest { ...@@ -1128,7 +1153,7 @@ class AjaxRequest {
"user": user, "user": user,
"password": password, "password": password,
"withCredentials": withCredentials, "withCredentials": withCredentials,
"headers": headers, "headers": headers?.toMap(),
"readyState": readyState?.toValue(), "readyState": readyState?.toValue(),
"status": status, "status": status,
"responseURL": responseURL, "responseURL": responseURL,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment