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 {
" this._flutter_inappbrowser_isAsync = isAsync;" +
" this._flutter_inappbrowser_user = user;" +
" this._flutter_inappbrowser_password = password;" +
" this._flutter_inappbrowser_request_headers = {};" +
" open.call(this, method, url, isAsync, user, password);" +
" };" +
" ajax.prototype.setRequestHeader = function(header, value) {" +
" this._flutter_inappbrowser_request_headers[header] = value;" +
" setRequestHeader.call(this, header, value);" +
" };" +
" function handleEvent(e) {" +
" var self = this;" +
......@@ -290,10 +292,12 @@ final public class InAppWebView extends InputAwareWebView {
" };" +
" for (var header in result.headers) {" +
" var value = result.headers[header];" +
" self._flutter_inappbrowser_request_headers[header] = value;" +
" };" +
" for (var header in self._flutter_inappbrowser_request_headers) {" +
" var value = self._flutter_inappbrowser_request_headers[header];" +
" var flutter_inappbrowser_value = self._flutter_inappbrowser_request_headers[header];" +
" if (flutter_inappbrowser_value == null) {" +
" self._flutter_inappbrowser_request_headers[header] = value;" +
" } else {" +
" self._flutter_inappbrowser_request_headers[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)) {" +
......
......@@ -39,7 +39,7 @@ public class JavaScriptBridgeInterface {
}
@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<>();
if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid);
......@@ -62,10 +62,10 @@ public class JavaScriptBridgeInterface {
return;
}
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 {
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 + "];}");
}
}
......
......@@ -2,7 +2,7 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSLocationAlwaysUsageDescription</key>
<key>NSLocationAlwaysUsageDescription</key>
<string>Need location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Need location</string>
......
......@@ -450,4 +450,4 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
: await getApplicationDocumentsDirectory();
return directory.path;
}
}
}
\ No newline at end of file
......@@ -296,10 +296,12 @@ let interceptAjaxRequestsJS = """
this._flutter_inappbrowser_isAsync = isAsync;
this._flutter_inappbrowser_user = user;
this._flutter_inappbrowser_password = password;
this._flutter_inappbrowser_request_headers = {};
open.call(this, method, url, isAsync, user, password);
};
ajax.prototype.setRequestHeader = function(header, value) {
this._flutter_inappbrowser_request_headers[header] = value;
setRequestHeader.call(this, header, value);
};
function handleEvent(e) {
var self = this;
......@@ -440,10 +442,12 @@ let interceptAjaxRequestsJS = """
};
for (var header in result.headers) {
var value = result.headers[header];
self._flutter_inappbrowser_request_headers[header] = value;
};
for (var header in self._flutter_inappbrowser_request_headers) {
var value = self._flutter_inappbrowser_request_headers[header];
var flutter_inappbrowser_value = self._flutter_inappbrowser_request_headers[header];
if (flutter_inappbrowser_value == null) {
self._flutter_inappbrowser_request_headers[header] = value;
} else {
self._flutter_inappbrowser_request_headers[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)) {
......@@ -2021,7 +2025,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
if let r = result {
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 {
String user = argMap["user"];
String password = argMap["password"];
bool withCredentials = argMap["withCredentials"];
Map<dynamic, dynamic> headers = argMap["headers"];
AjaxRequestHeaders headers = AjaxRequestHeaders(argMap["headers"]);
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);
......@@ -610,7 +610,7 @@ class InAppWebViewController {
String user = argMap["user"];
String password = argMap["password"];
bool withCredentials = argMap["withCredentials"];
Map<dynamic, dynamic> headers = argMap["headers"];
AjaxRequestHeaders headers = AjaxRequestHeaders(argMap["headers"]);
int readyState = argMap["readyState"];
int status = argMap["status"];
String responseURL = argMap["responseURL"];
......@@ -639,7 +639,7 @@ class InAppWebViewController {
String user = argMap["user"];
String password = argMap["password"];
bool withCredentials = argMap["withCredentials"];
Map<dynamic, dynamic> headers = argMap["headers"];
AjaxRequestHeaders headers = AjaxRequestHeaders(argMap["headers"]);
int readyState = argMap["readyState"];
int status = argMap["status"];
String responseURL = argMap["responseURL"];
......
......@@ -1064,6 +1064,31 @@ class AjaxRequestReadyState {
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.
class AjaxRequest {
///Data passed as a parameter to the `XMLHttpRequest.send()` method.
......@@ -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.
bool withCredentials;
///The HTTP request headers.
Map<dynamic, dynamic> headers;
AjaxRequestHeaders headers;
///The state of the `XMLHttpRequest` request.
AjaxRequestReadyState readyState;
///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 {
"user": user,
"password": password,
"withCredentials": withCredentials,
"headers": headers,
"headers": headers?.toMap(),
"readyState": readyState?.toValue(),
"status": status,
"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