mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-04-24 00:00:03 +08:00
fixed some issues with responseSerializers and moved some functions out of obj-c into javascript
This commit is contained in:
@@ -31,9 +31,6 @@
|
|||||||
<header-file src="src/ios/CordovaHttpPlugin.h" />
|
<header-file src="src/ios/CordovaHttpPlugin.h" />
|
||||||
<source-file src="src/ios/CordovaHttpPlugin.m" />
|
<source-file src="src/ios/CordovaHttpPlugin.m" />
|
||||||
|
|
||||||
<header-file src="src/ios/HTTPManager.h" />
|
|
||||||
<source-file src="src/ios/HTTPManager.m" />
|
|
||||||
|
|
||||||
<header-file src="src/ios/TextResponseSerializer.h" />
|
<header-file src="src/ios/TextResponseSerializer.h" />
|
||||||
<source-file src="src/ios/TextResponseSerializer.m" />
|
<source-file src="src/ios/TextResponseSerializer.m" />
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,9 @@
|
|||||||
|
|
||||||
@interface CordovaHttpPlugin : CDVPlugin
|
@interface CordovaHttpPlugin : CDVPlugin
|
||||||
|
|
||||||
- (void)useBasicAuth:(CDVInvokedUrlCommand*)command;
|
|
||||||
- (void)setHeader:(CDVInvokedUrlCommand*)command;
|
|
||||||
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command;
|
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command;
|
||||||
- (void)acceptAllCerts:(CDVInvokedUrlCommand*)command;
|
- (void)acceptAllCerts:(CDVInvokedUrlCommand*)command;
|
||||||
- (void)acceptAllHosts:(CDVInvokedUrlCommand*)command;
|
- (void)validateDomainName:(CDVInvokedUrlCommand*)command;
|
||||||
- (void)post:(CDVInvokedUrlCommand*)command;
|
- (void)post:(CDVInvokedUrlCommand*)command;
|
||||||
- (void)get:(CDVInvokedUrlCommand*)command;
|
- (void)get:(CDVInvokedUrlCommand*)command;
|
||||||
- (void)uploadFile:(CDVInvokedUrlCommand*)command;
|
- (void)uploadFile:(CDVInvokedUrlCommand*)command;
|
||||||
|
|||||||
+46
-93
@@ -1,59 +1,45 @@
|
|||||||
#import "CordovaHttpPlugin.h"
|
#import "CordovaHttpPlugin.h"
|
||||||
#import "CDVFile.h"
|
#import "CDVFile.h"
|
||||||
#import "TextResponseSerializer.h"
|
#import "TextResponseSerializer.h"
|
||||||
#import "HttpManager.h"
|
#import "AFHTTPSessionManager.h"
|
||||||
|
|
||||||
@interface CordovaHttpPlugin()
|
@interface CordovaHttpPlugin()
|
||||||
|
|
||||||
- (void)setRequestHeaders:(NSDictionary*)headers;
|
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager;
|
||||||
|
- (void)setResults:(NSMutableDictionary*)dictionary withTask:(NSURLSessionTask*)task;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
@implementation CordovaHttpPlugin {
|
@implementation CordovaHttpPlugin {
|
||||||
AFHTTPRequestSerializer *requestSerializer;
|
AFSecurityPolicy *securityPolicy;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)pluginInitialize {
|
- (void)pluginInitialize {
|
||||||
requestSerializer = [AFHTTPRequestSerializer serializer];
|
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setRequestHeaders:(NSDictionary*)headers {
|
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager {
|
||||||
[HttpManager sharedClient].requestSerializer = [AFHTTPRequestSerializer serializer];
|
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
|
||||||
[requestSerializer.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
|
||||||
[[HttpManager sharedClient].requestSerializer setValue:obj forHTTPHeaderField:key];
|
|
||||||
}];
|
|
||||||
[headers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
[headers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
||||||
[[HttpManager sharedClient].requestSerializer setValue:obj forHTTPHeaderField:key];
|
[manager.requestSerializer setValue:obj forHTTPHeaderField:key];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)useBasicAuth:(CDVInvokedUrlCommand*)command {
|
- (void)setResults:(NSMutableDictionary*)dictionary withTask:(NSURLSessionTask*)task {
|
||||||
NSString *username = [command.arguments objectAtIndex:0];
|
if (task.response != nil) {
|
||||||
NSString *password = [command.arguments objectAtIndex:1];
|
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||||
|
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||||
[requestSerializer setAuthorizationHeaderFieldWithUsername:username password:password];
|
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||||
|
|
||||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setHeader:(CDVInvokedUrlCommand*)command {
|
|
||||||
NSString *header = [command.arguments objectAtIndex:0];
|
|
||||||
NSString *value = [command.arguments objectAtIndex:1];
|
|
||||||
|
|
||||||
[requestSerializer setValue:value forHTTPHeaderField: header];
|
|
||||||
|
|
||||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command {
|
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command {
|
||||||
bool enable = [[command.arguments objectAtIndex:0] boolValue];
|
bool enable = [[command.arguments objectAtIndex:0] boolValue];
|
||||||
if (enable) {
|
if (enable) {
|
||||||
[HttpManager sharedClient].securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
|
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
|
||||||
} else {
|
} else {
|
||||||
[HttpManager sharedClient].securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
||||||
}
|
}
|
||||||
|
|
||||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||||
@@ -64,46 +50,41 @@
|
|||||||
CDVPluginResult* pluginResult = nil;
|
CDVPluginResult* pluginResult = nil;
|
||||||
bool allow = [[command.arguments objectAtIndex:0] boolValue];
|
bool allow = [[command.arguments objectAtIndex:0] boolValue];
|
||||||
|
|
||||||
[HttpManager sharedClient].securityPolicy.allowInvalidCertificates = allow;
|
securityPolicy.allowInvalidCertificates = allow;
|
||||||
|
|
||||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)acceptAllHosts:(CDVInvokedUrlCommand*)command {
|
- (void)validateDomainName:(CDVInvokedUrlCommand*)command {
|
||||||
CDVPluginResult* pluginResult = nil;
|
CDVPluginResult* pluginResult = nil;
|
||||||
bool allow = [[command.arguments objectAtIndex:0] boolValue];
|
bool validate = [[command.arguments objectAtIndex:0] boolValue];
|
||||||
[HttpManager sharedClient].securityPolicy.validatesDomainName = !allow;
|
|
||||||
|
securityPolicy.validatesDomainName = validate;
|
||||||
|
|
||||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)post:(CDVInvokedUrlCommand*)command {
|
- (void)post:(CDVInvokedUrlCommand*)command {
|
||||||
HttpManager *manager = [HttpManager sharedClient];
|
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||||
|
manager.securityPolicy = securityPolicy;
|
||||||
NSString *url = [command.arguments objectAtIndex:0];
|
NSString *url = [command.arguments objectAtIndex:0];
|
||||||
NSDictionary *parameters = [command.arguments objectAtIndex:1];
|
NSDictionary *parameters = [command.arguments objectAtIndex:1];
|
||||||
NSDictionary *headers = [command.arguments objectAtIndex:2];
|
NSDictionary *headers = [command.arguments objectAtIndex:2];
|
||||||
[self setRequestHeaders: headers];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
manager.responseSerializer = [TextResponseSerializer serializer];
|
||||||
[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||||
if (task.response != nil) {
|
[self setResults: dictionary withTask: task];
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
|
||||||
}
|
|
||||||
[dictionary setObject:responseObject forKey:@"data"];
|
[dictionary setObject:responseObject forKey:@"data"];
|
||||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||||
if (task.response != nil) {
|
[self setResults: dictionary withTask: task];
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
|
||||||
}
|
|
||||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
@@ -111,32 +92,25 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)get:(CDVInvokedUrlCommand*)command {
|
- (void)get:(CDVInvokedUrlCommand*)command {
|
||||||
HttpManager *manager = [HttpManager sharedClient];
|
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||||
|
manager.securityPolicy = securityPolicy;
|
||||||
NSString *url = [command.arguments objectAtIndex:0];
|
NSString *url = [command.arguments objectAtIndex:0];
|
||||||
NSDictionary *parameters = [command.arguments objectAtIndex:1];
|
NSDictionary *parameters = [command.arguments objectAtIndex:1];
|
||||||
NSDictionary *headers = [command.arguments objectAtIndex:2];
|
NSDictionary *headers = [command.arguments objectAtIndex:2];
|
||||||
[self setRequestHeaders: headers];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
|
|
||||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
manager.responseSerializer = [TextResponseSerializer serializer];
|
||||||
[manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
[manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||||
if (task.response != nil) {
|
[self setResults: dictionary withTask: task];
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
|
||||||
}
|
|
||||||
[dictionary setObject:responseObject forKey:@"data"];
|
[dictionary setObject:responseObject forKey:@"data"];
|
||||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||||
if (task.response != nil) {
|
[self setResults: dictionary withTask: task];
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
|
||||||
}
|
|
||||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
@@ -144,32 +118,25 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)head:(CDVInvokedUrlCommand*)command {
|
- (void)head:(CDVInvokedUrlCommand*)command {
|
||||||
HttpManager *manager = [HttpManager sharedClient];
|
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||||
|
manager.securityPolicy = securityPolicy;
|
||||||
NSString *url = [command.arguments objectAtIndex:0];
|
NSString *url = [command.arguments objectAtIndex:0];
|
||||||
NSDictionary *parameters = [command.arguments objectAtIndex:1];
|
NSDictionary *parameters = [command.arguments objectAtIndex:1];
|
||||||
NSDictionary *headers = [command.arguments objectAtIndex:2];
|
NSDictionary *headers = [command.arguments objectAtIndex:2];
|
||||||
[self setRequestHeaders: headers];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
|
|
||||||
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
|
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
|
||||||
[manager HEAD:url parameters:parameters success:^(NSURLSessionTask *task) {
|
[manager HEAD:url parameters:parameters success:^(NSURLSessionTask *task) {
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||||
if (task.response != nil) {
|
[self setResults: dictionary withTask: task];
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
|
||||||
}
|
|
||||||
// no 'body' for HEAD request, omitting 'data'
|
// no 'body' for HEAD request, omitting 'data'
|
||||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||||
if (task.response != nil) {
|
[self setResults: dictionary withTask: task];
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
|
||||||
}
|
|
||||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
@@ -177,7 +144,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)uploadFile:(CDVInvokedUrlCommand*)command {
|
- (void)uploadFile:(CDVInvokedUrlCommand*)command {
|
||||||
HttpManager *manager = [HttpManager sharedClient];
|
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||||
|
manager.securityPolicy = securityPolicy;
|
||||||
NSString *url = [command.arguments objectAtIndex:0];
|
NSString *url = [command.arguments objectAtIndex:0];
|
||||||
NSDictionary *parameters = [command.arguments objectAtIndex:1];
|
NSDictionary *parameters = [command.arguments objectAtIndex:1];
|
||||||
NSDictionary *headers = [command.arguments objectAtIndex:2];
|
NSDictionary *headers = [command.arguments objectAtIndex:2];
|
||||||
@@ -186,7 +154,7 @@
|
|||||||
|
|
||||||
NSURL *fileURL = [NSURL fileURLWithPath: filePath];
|
NSURL *fileURL = [NSURL fileURLWithPath: filePath];
|
||||||
|
|
||||||
[self setRequestHeaders: headers];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
manager.responseSerializer = [TextResponseSerializer serializer];
|
||||||
@@ -203,20 +171,12 @@
|
|||||||
}
|
}
|
||||||
} progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
} progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||||
if (task.response != nil) {
|
[self setResults: dictionary withTask: task];
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
|
||||||
}
|
|
||||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||||
if (task.response != nil) {
|
[self setResults: dictionary withTask: task];
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
|
||||||
}
|
|
||||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
@@ -225,13 +185,14 @@
|
|||||||
|
|
||||||
|
|
||||||
- (void)downloadFile:(CDVInvokedUrlCommand*)command {
|
- (void)downloadFile:(CDVInvokedUrlCommand*)command {
|
||||||
HttpManager *manager = [HttpManager sharedClient];
|
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||||
|
manager.securityPolicy = securityPolicy;
|
||||||
NSString *url = [command.arguments objectAtIndex:0];
|
NSString *url = [command.arguments objectAtIndex:0];
|
||||||
NSDictionary *parameters = [command.arguments objectAtIndex:1];
|
NSDictionary *parameters = [command.arguments objectAtIndex:1];
|
||||||
NSDictionary *headers = [command.arguments objectAtIndex:2];
|
NSDictionary *headers = [command.arguments objectAtIndex:2];
|
||||||
NSString *filePath = [command.arguments objectAtIndex: 3];
|
NSString *filePath = [command.arguments objectAtIndex: 3];
|
||||||
|
|
||||||
[self setRequestHeaders: headers];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
|
|
||||||
if ([filePath hasPrefix:@"file://"]) {
|
if ([filePath hasPrefix:@"file://"]) {
|
||||||
filePath = [filePath substringFromIndex:7];
|
filePath = [filePath substringFromIndex:7];
|
||||||
@@ -291,21 +252,13 @@
|
|||||||
|
|
||||||
id filePlugin = [self.commandDelegate getCommandInstance:@"File"];
|
id filePlugin = [self.commandDelegate getCommandInstance:@"File"];
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||||
if (task.response != nil) {
|
[self setResults: dictionary withTask: task];
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
|
||||||
}
|
|
||||||
[dictionary setObject:[filePlugin getDirectoryEntry:filePath isDirectory:NO] forKey:@"file"];
|
[dictionary setObject:[filePlugin getDirectoryEntry:filePath isDirectory:NO] forKey:@"file"];
|
||||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||||
if (task.response != nil) {
|
[self setResults: dictionary withTask: task];
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
|
||||||
}
|
|
||||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
// Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
// THE SOFTWARE.
|
|
||||||
// Modified by Andrew Stephan
|
|
||||||
#import <Foundation/Foundation.h>
|
|
||||||
#import "AFHTTPSessionManager.h"
|
|
||||||
|
|
||||||
@interface HttpManager : AFHTTPSessionManager
|
|
||||||
|
|
||||||
+ (instancetype)sharedClient;
|
|
||||||
|
|
||||||
@end
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
// Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
// THE SOFTWARE.
|
|
||||||
// Modified by Andrew Stephan
|
|
||||||
#import "HttpManager.h"
|
|
||||||
|
|
||||||
@implementation HttpManager
|
|
||||||
|
|
||||||
+ (instancetype)sharedClient {
|
|
||||||
static HttpManager *_sharedClient = nil;
|
|
||||||
static dispatch_once_t onceToken;
|
|
||||||
dispatch_once(&onceToken, ^{
|
|
||||||
_sharedClient = [HttpManager manager];
|
|
||||||
});
|
|
||||||
|
|
||||||
return _sharedClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
Vendored
+40
-14
@@ -6,12 +6,36 @@
|
|||||||
|
|
||||||
var exec = require('cordova/exec');
|
var exec = require('cordova/exec');
|
||||||
|
|
||||||
|
// Thanks Mozilla: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_.22Unicode_Problem.22
|
||||||
|
function b64EncodeUnicode(str) {
|
||||||
|
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
|
||||||
|
return String.fromCharCode('0x' + p1);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeHeaders(globalHeaders, localHeaders) {
|
||||||
|
var globalKeys = Object.keys(globalHeaders);
|
||||||
|
var key;
|
||||||
|
for (var i = 0; i < globalKeys.length; i++) {
|
||||||
|
key = globalKeys[i];
|
||||||
|
if (!localHeaders.hasOwnProperty(key)) {
|
||||||
|
localHeaders[key] = globalHeaders[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return localHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
var http = {
|
var http = {
|
||||||
useBasicAuth: function(username, password, success, failure) {
|
headers: {},
|
||||||
return exec(success, failure, "CordovaHttpPlugin", "useBasicAuth", [username, password]);
|
sslPinning: false,
|
||||||
|
getBasicAuthHeader: function(username, password) {
|
||||||
|
return {'Authorization': 'Basic ' + b64EncodeUnicode(username + ':' + password)};
|
||||||
},
|
},
|
||||||
setHeader: function(header, value, success, failure) {
|
useBasicAuth: function(username, password) {
|
||||||
return exec(success, failure, "CordovaHttpPlugin", "setHeader", [header, value]);
|
this.headers.Authorization = 'Basic ' + b64EncodeUnicode(username + ':' + password);
|
||||||
|
},
|
||||||
|
setHeader: function(header, value) {
|
||||||
|
this.headers[header] = value;
|
||||||
},
|
},
|
||||||
enableSSLPinning: function(enable, success, failure) {
|
enableSSLPinning: function(enable, success, failure) {
|
||||||
return exec(success, failure, "CordovaHttpPlugin", "enableSSLPinning", [enable]);
|
return exec(success, failure, "CordovaHttpPlugin", "enableSSLPinning", [enable]);
|
||||||
@@ -19,19 +43,23 @@ var http = {
|
|||||||
acceptAllCerts: function(allow, success, failure) {
|
acceptAllCerts: function(allow, success, failure) {
|
||||||
return exec(success, failure, "CordovaHttpPlugin", "acceptAllCerts", [allow]);
|
return exec(success, failure, "CordovaHttpPlugin", "acceptAllCerts", [allow]);
|
||||||
},
|
},
|
||||||
acceptAllHosts: function(allow, success, failure) {
|
validateDomainName: function(validate, success, failure) {
|
||||||
return exec(success, failure, "CordovaHttpPlugin", "acceptAllHosts", [allow]);
|
return exec(success, failure, "CordovaHttpPlugin", "validateDomainName", [validate]);
|
||||||
},
|
},
|
||||||
post: function(url, params, headers, success, failure) {
|
post: function(url, params, headers, success, failure) {
|
||||||
|
headers = mergeHeaders(this.headers, headers);
|
||||||
return exec(success, failure, "CordovaHttpPlugin", "post", [url, params, headers]);
|
return exec(success, failure, "CordovaHttpPlugin", "post", [url, params, headers]);
|
||||||
},
|
},
|
||||||
get: function(url, params, headers, success, failure) {
|
get: function(url, params, headers, success, failure) {
|
||||||
|
headers = mergeHeaders(this.headers, headers);
|
||||||
return exec(success, failure, "CordovaHttpPlugin", "get", [url, params, headers]);
|
return exec(success, failure, "CordovaHttpPlugin", "get", [url, params, headers]);
|
||||||
},
|
},
|
||||||
head: function(url, params, headers, success, failure) {
|
head: function(url, params, headers, success, failure) {
|
||||||
|
headers = mergeHeaders(this.headers, headers);
|
||||||
return exec(success, failure, "CordovaHttpPlugin", "head", [url, params, headers]);
|
return exec(success, failure, "CordovaHttpPlugin", "head", [url, params, headers]);
|
||||||
},
|
},
|
||||||
uploadFile: function(url, params, headers, filePath, name, success, failure) {
|
uploadFile: function(url, params, headers, filePath, name, success, failure) {
|
||||||
|
headers = mergeHeaders(this.headers, headers);
|
||||||
return exec(success, failure, "CordovaHttpPlugin", "uploadFile", [url, params, headers, filePath, name]);
|
return exec(success, failure, "CordovaHttpPlugin", "uploadFile", [url, params, headers, filePath, name]);
|
||||||
},
|
},
|
||||||
downloadFile: function(url, params, headers, filePath, success, failure) {
|
downloadFile: function(url, params, headers, filePath, success, failure) {
|
||||||
@@ -57,6 +85,7 @@ var http = {
|
|||||||
* Modified by Andrew Stephan for Sync OnSet
|
* Modified by Andrew Stephan for Sync OnSet
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
headers = mergeHeaders(this.headers, headers);
|
||||||
var win = function(result) {
|
var win = function(result) {
|
||||||
var entry = new (require('cordova-plugin-file.FileEntry'))();
|
var entry = new (require('cordova-plugin-file.FileEntry'))();
|
||||||
entry.isDirectory = false;
|
entry.isDirectory = false;
|
||||||
@@ -107,20 +136,17 @@ if (typeof angular !== "undefined") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var cordovaHTTP = {
|
var cordovaHTTP = {
|
||||||
useBasicAuth: function(username, password) {
|
getBasicAuthHeader: http.getBasicAuthHeader,
|
||||||
return makePromise(http.useBasicAuth, [username, password]);
|
useBasicAuth: http.useBasicAuth,
|
||||||
},
|
setHeader: http.setHeader,
|
||||||
setHeader: function(header, value) {
|
|
||||||
return makePromise(http.setHeader, [header, value]);
|
|
||||||
},
|
|
||||||
enableSSLPinning: function(enable) {
|
enableSSLPinning: function(enable) {
|
||||||
return makePromise(http.enableSSLPinning, [enable]);
|
return makePromise(http.enableSSLPinning, [enable]);
|
||||||
},
|
},
|
||||||
acceptAllCerts: function(allow) {
|
acceptAllCerts: function(allow) {
|
||||||
return makePromise(http.acceptAllCerts, [allow]);
|
return makePromise(http.acceptAllCerts, [allow]);
|
||||||
},
|
},
|
||||||
acceptAllHosts: function(allow) {
|
validateDomainName: function(validate) {
|
||||||
return makePromise(http.acceptAllHosts, [allow]);
|
return makePromise(http.validateDomainName, [validate]);
|
||||||
},
|
},
|
||||||
post: function(url, params, headers) {
|
post: function(url, params, headers) {
|
||||||
return makePromise(http.post, [url, params, headers], true);
|
return makePromise(http.post, [url, params, headers], true);
|
||||||
|
|||||||
Reference in New Issue
Block a user