Skip to content

feat(android,ios): Support for passing in request headers #115

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 34 additions & 4 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

@SuppressLint("SetJavaScriptEnabled")
Expand Down Expand Up @@ -94,6 +95,7 @@ public class InAppBrowser extends CordovaPlugin {
private boolean clearAllCache = false;
private boolean clearSessionCache = false;
private boolean hadwareBackButton = true;
private Map <String, String> extraHeaders;

/**
* Executes the request and returns PluginResult.
Expand All @@ -113,6 +115,8 @@ public boolean execute(String action, CordovaArgs args, final CallbackContext ca
}
final String target = t;
final HashMap<String, Boolean> features = parseFeature(args.optString(2));

extraHeaders = parseHeaders(args.optString(3));

Log.d(LOG_TAG, "target = " + target);

Expand Down Expand Up @@ -328,7 +332,33 @@ private HashMap<String, Boolean> parseFeature(String optString) {
return map;
}
}


/**
* Put the list of headers into a map
*
* @param headerString
* @return
*/
private Map<String, String> parseHeaders(String headerString) {
if (headerString.equals(NULL)) {
Map<String, String> map = new HashMap<String, String>();
return map;
} else {
Map<String, String> map = new HashMap<String, String>();
StringTokenizer features = new StringTokenizer(headerString, ",");
StringTokenizer option;
while(features.hasMoreElements()) {
option = new StringTokenizer(features.nextToken(), ":");
if (option.hasMoreElements()) {
String key = option.nextToken();
String value = option.nextToken();
map.put(key, value);
}
}
return map;
}
}

/**
* Display a new browser with the specified URL.
*
Expand Down Expand Up @@ -438,9 +468,9 @@ private void navigate(String url) {
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

if (!url.startsWith("http") && !url.startsWith("file:")) {
this.inAppWebView.loadUrl("http://" + url);
this.inAppWebView.loadUrl("http://" + url,extraHeaders);
} else {
this.inAppWebView.loadUrl(url);
this.inAppWebView.loadUrl(url,extraHeaders);
}
this.inAppWebView.requestFocus();
}
Expand Down Expand Up @@ -666,7 +696,7 @@ public void onClick(View v) {
CookieManager.getInstance().removeSessionCookie();
}

inAppWebView.loadUrl(url);
inAppWebView.loadUrl(url,extraHeaders);
inAppWebView.setId(6);
inAppWebView.getSettings().setLoadWithOverviewMode(true);
inAppWebView.getSettings().setUseWideViewPort(true);
Expand Down
43 changes: 38 additions & 5 deletions src/ios/CDVInAppBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ - (void)open:(CDVInvokedUrlCommand*)command
NSString* url = [command argumentAtIndex:0];
NSString* target = [command argumentAtIndex:1 withDefault:kInAppBrowserTargetSelf];
NSString* options = [command argumentAtIndex:2 withDefault:@"" andClass:[NSString class]];
NSString* headers = [command argumentAtIndex:3 withDefault:@"" andClass:[NSString class]];

self.callbackId = command.callbackId;

Expand All @@ -98,7 +99,7 @@ - (void)open:(CDVInvokedUrlCommand*)command
} else if ([target isEqualToString:kInAppBrowserTargetSystem]) {
[self openInSystem:absoluteUrl];
} else { // _blank or anything else
[self openInInAppBrowser:absoluteUrl withOptions:options];
[self openInInAppBrowser:absoluteUrl withOptions:options withHeaders:headers];
}

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
Expand All @@ -110,7 +111,7 @@ - (void)open:(CDVInvokedUrlCommand*)command
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
- (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options withHeaders:(NSString*)headers
{
CDVInAppBrowserOptions* browserOptions = [CDVInAppBrowserOptions parseOptions:options];

Expand Down Expand Up @@ -195,7 +196,8 @@ - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
self.inAppBrowserViewController.webView.suppressesIncrementalRendering = browserOptions.suppressesincrementalrendering;
}

[self.inAppBrowserViewController navigateTo:url];
//[self.inAppBrowserViewController navigateTo:url];
[self.inAppBrowserViewController navigateToNew:url headers:headers];
if (!browserOptions.hidden) {
[self show:nil];
}
Expand Down Expand Up @@ -236,7 +238,7 @@ - (void)openInCordovaWebView:(NSURL*)url withOptions:(NSString*)options
[self.webView loadRequest:request];
#endif
} else { // this assumes the InAppBrowser can be excepted from the white-list
[self openInInAppBrowser:url withOptions:options];
[self openInInAppBrowser:url withOptions:options withHeaders:@""];
}
}

Expand Down Expand Up @@ -760,7 +762,38 @@ - (void)close

- (void)navigateTo:(NSURL*)url
{
NSURLRequest* request = [NSURLRequest requestWithURL:url];
//NSURLRequest* request = [NSURLRequest requestWithURL:url];

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];

[request setValue:@"1" forHTTPHeaderField:@"horror"];

if (_userAgentLockToken != 0) {
[self.webView loadRequest:request];
} else {
[CDVUserAgentUtil acquireLock:^(NSInteger lockToken) {
_userAgentLockToken = lockToken;
[CDVUserAgentUtil setUserAgent:_userAgent lockToken:lockToken];
[self.webView loadRequest:request];
}];
}
}

- (void)navigateToNew:(NSURL*)url headers:(NSString*)headers
{
//NSURLRequest* request = [NSURLRequest requestWithURL:url];

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];

//[request setValue:@"1" forHTTPHeaderField:@"horror"];
NSArray* pairs = [headers componentsSeparatedByString:@","];

for (NSString* pair in pairs) {
NSArray* keyvalue = [pair componentsSeparatedByString:@":"];
NSString* key = [[keyvalue objectAtIndex:0] lowercaseString];
NSString* value = [keyvalue objectAtIndex:1];
[request setValue:value forHTTPHeaderField:key];
}

if (_userAgentLockToken != 0) {
[self.webView loadRequest:request];
Expand Down
5 changes: 3 additions & 2 deletions www/inappbrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ InAppBrowser.prototype = {
}
};

module.exports = function(strUrl, strWindowName, strWindowFeatures, callbacks) {
module.exports = function(strUrl, strWindowName, strWindowFeatures, strWindowHeaders, callbacks) {
// Don't catch calls that write to existing frames (e.g. named iframes).
if (window.frames && window.frames[strWindowName]) {
var origOpenFunc = modulemapper.getOriginalSymbol(window, 'open');
Expand All @@ -97,8 +97,9 @@ module.exports = function(strUrl, strWindowName, strWindowFeatures, callbacks) {
};

strWindowFeatures = strWindowFeatures || "";
strWindowHeaders = strWindowHeaders || "";

exec(cb, cb, "InAppBrowser", "open", [strUrl, strWindowName, strWindowFeatures]);
exec(cb, cb, "InAppBrowser", "open", [strUrl, strWindowName, strWindowFeatures, strWindowHeaders]);
return iab;
};