Skip to content
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ This sets up all future requests to use Basic HTTP authentication with the given
Set a header for all future requests. Takes a header and a value.

cordovaHTTP.setHeader("Header", "Value");

### disableRedirect
If set to `true`, it won't follow redirects automatically. This is a global setting.

cordovaHTTP.disableRedirect(true);


## Async Functions
Expand Down
13 changes: 13 additions & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public abstract class CordovaHttp {
private static AtomicBoolean sslPinning = new AtomicBoolean(false);
private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false);
private static AtomicBoolean validateDomainName = new AtomicBoolean(true);
private static AtomicBoolean disableRedirect = new AtomicBoolean(false);

private String urlString;
private Map<?, ?> params;
Expand Down Expand Up @@ -69,6 +70,10 @@ public static void validateDomainName(boolean accept) {
validateDomainName.set(accept);
}

public static void disableRedirect(boolean disable) {
disableRedirect.set(disable);
}

protected String getUrlString() {
return this.urlString;
}
Expand Down Expand Up @@ -97,6 +102,14 @@ protected HttpRequest setupSecurity(HttpRequest request) {
}
return request;
}

protected HttpRequest setupRedirect(HttpRequest request) {
if (disableRedirect.get()) {
request.followRedirects(false);
}

return request;
}

protected void respondWithError(int status, String msg) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public CordovaHttpDownload(String urlString, Map<?, ?> params, Map<String, Strin
public void run() {
try {
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), true);
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
Expand Down
1 change: 1 addition & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public CordovaHttpGet(String urlString, Map<?, ?> params, Map<String, String> he
public void run() {
try {
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), false);
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
Expand Down
1 change: 1 addition & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpHead.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public CordovaHttpHead(String urlString, Map<?, ?> params, Map<String, String> h
public void run() {
try {
HttpRequest request = HttpRequest.head(this.getUrlString(), this.getParams(), true);
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
Expand Down
4 changes: 4 additions & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
String filePath = args.getString(3);
CordovaHttpDownload download = new CordovaHttpDownload(urlString, paramsMap, headersMap, callbackContext, filePath);
cordova.getThreadPool().execute(download);
} else if (action.equals("disableRedirect")) {
boolean disable = args.getBoolean(0);
CordovaHttp.disableRedirect(disable);
callbackContext.success();
} else {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public CordovaHttpPost(String urlString, Map<?, ?> params, Map<String, String> h
public void run() {
try {
HttpRequest request = HttpRequest.post(this.getUrlString());
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public CordovaHttpUpload(String urlString, Map<?, ?> params, Map<String, String>
public void run() {
try {
HttpRequest request = HttpRequest.post(this.getUrlString());
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeaders());
Expand Down
1 change: 1 addition & 0 deletions src/ios/CordovaHttpPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command;
- (void)acceptAllCerts:(CDVInvokedUrlCommand*)command;
- (void)validateDomainName:(CDVInvokedUrlCommand*)command;
- (void)disableRedirect:(CDVInvokedUrlCommand*)command;
- (void)post:(CDVInvokedUrlCommand*)command;
- (void)get:(CDVInvokedUrlCommand*)command;
- (void)uploadFile:(CDVInvokedUrlCommand*)command;
Expand Down
28 changes: 28 additions & 0 deletions src/ios/CordovaHttpPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ @interface CordovaHttpPlugin()

- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager;
- (void)setResults:(NSMutableDictionary*)dictionary withTask:(NSURLSessionTask*)task;
- (void)setRedirect:(AFHTTPSessionManager*)manager;

@end


@implementation CordovaHttpPlugin {
AFSecurityPolicy *securityPolicy;
bool redirect;
}

- (void)pluginInitialize {
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
redirect = true;
}

- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager {
Expand All @@ -34,6 +37,16 @@ - (void)setResults:(NSMutableDictionary*)dictionary withTask:(NSURLSessionTask*)
}
}

- (void)setRedirect:(AFHTTPSessionManager*)manager {
[manager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
if (redirect) {
return request;
} else {
return nil;
}
}];
}

- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command {
bool enable = [[command.arguments objectAtIndex:0] boolValue];
if (enable) {
Expand Down Expand Up @@ -66,13 +79,24 @@ - (void)validateDomainName:(CDVInvokedUrlCommand*)command {
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)disableRedirect:(CDVInvokedUrlCommand*)command {
CDVPluginResult* pluginResult = nil;
bool disable = [[command.arguments objectAtIndex:0] boolValue];

redirect = !disable;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)post:(CDVInvokedUrlCommand*)command {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy = securityPolicy;
NSString *url = [command.arguments objectAtIndex:0];
NSDictionary *parameters = [command.arguments objectAtIndex:1];
NSDictionary *headers = [command.arguments objectAtIndex:2];
[self setRequestHeaders: headers forManager: manager];
[self setRedirect: manager];

CordovaHttpPlugin* __weak weakSelf = self;
manager.responseSerializer = [TextResponseSerializer serializer];
Expand All @@ -99,6 +123,7 @@ - (void)get:(CDVInvokedUrlCommand*)command {
NSDictionary *parameters = [command.arguments objectAtIndex:1];
NSDictionary *headers = [command.arguments objectAtIndex:2];
[self setRequestHeaders: headers forManager: manager];
[self setRedirect: manager];

CordovaHttpPlugin* __weak weakSelf = self;

Expand Down Expand Up @@ -126,6 +151,7 @@ - (void)head:(CDVInvokedUrlCommand*)command {
NSDictionary *parameters = [command.arguments objectAtIndex:1];
NSDictionary *headers = [command.arguments objectAtIndex:2];
[self setRequestHeaders: headers forManager: manager];
[self setRedirect: manager];

CordovaHttpPlugin* __weak weakSelf = self;

Expand Down Expand Up @@ -158,6 +184,7 @@ - (void)uploadFile:(CDVInvokedUrlCommand*)command {
NSURL *fileURL = [NSURL URLWithString: filePath];

[self setRequestHeaders: headers forManager: manager];
[self setRedirect: manager];

CordovaHttpPlugin* __weak weakSelf = self;
manager.responseSerializer = [TextResponseSerializer serializer];
Expand Down Expand Up @@ -197,6 +224,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
NSString *filePath = [command.arguments objectAtIndex: 3];

[self setRequestHeaders: headers forManager: manager];
[self setRedirect: manager];

if ([filePath hasPrefix:@"file://"]) {
filePath = [filePath substringFromIndex:7];
Expand Down
6 changes: 6 additions & 0 deletions www/cordovaHTTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ var http = {
acceptAllCerts: function(allow, success, failure) {
return exec(success, failure, "CordovaHttpPlugin", "acceptAllCerts", [allow]);
},
disableRedirect: function(disable, success, failure) {
return exec(success, failure, "CordovaHttpPlugin", "disableRedirect", [disable]);
},
validateDomainName: function(validate, success, failure) {
return exec(success, failure, "CordovaHttpPlugin", "validateDomainName", [validate]);
},
Expand Down Expand Up @@ -146,6 +149,9 @@ if (typeof angular !== "undefined") {
enableSSLPinning: function(enable) {
return makePromise(http.enableSSLPinning, [enable]);
},
disableRedirect: function(disable) {
return makePromise(http.disableRedirect, [disable]);
},
acceptAllCerts: function(allow) {
return makePromise(http.acceptAllCerts, [allow]);
},
Expand Down