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:
+47
-94
@@ -1,59 +1,45 @@
|
||||
#import "CordovaHttpPlugin.h"
|
||||
#import "CDVFile.h"
|
||||
#import "TextResponseSerializer.h"
|
||||
#import "HttpManager.h"
|
||||
#import "AFHTTPSessionManager.h"
|
||||
|
||||
@interface CordovaHttpPlugin()
|
||||
|
||||
- (void)setRequestHeaders:(NSDictionary*)headers;
|
||||
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager;
|
||||
- (void)setResults:(NSMutableDictionary*)dictionary withTask:(NSURLSessionTask*)task;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation CordovaHttpPlugin {
|
||||
AFHTTPRequestSerializer *requestSerializer;
|
||||
AFSecurityPolicy *securityPolicy;
|
||||
}
|
||||
|
||||
- (void)pluginInitialize {
|
||||
requestSerializer = [AFHTTPRequestSerializer serializer];
|
||||
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
||||
}
|
||||
|
||||
- (void)setRequestHeaders:(NSDictionary*)headers {
|
||||
[HttpManager sharedClient].requestSerializer = [AFHTTPRequestSerializer serializer];
|
||||
[requestSerializer.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
||||
[[HttpManager sharedClient].requestSerializer setValue:obj forHTTPHeaderField:key];
|
||||
}];
|
||||
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager {
|
||||
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
|
||||
[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 {
|
||||
NSString *username = [command.arguments objectAtIndex:0];
|
||||
NSString *password = [command.arguments objectAtIndex:1];
|
||||
|
||||
[requestSerializer setAuthorizationHeaderFieldWithUsername:username password:password];
|
||||
|
||||
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)setResults:(NSMutableDictionary*)dictionary withTask:(NSURLSessionTask*)task {
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command {
|
||||
bool enable = [[command.arguments objectAtIndex:0] boolValue];
|
||||
if (enable) {
|
||||
[HttpManager sharedClient].securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
|
||||
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
|
||||
} else {
|
||||
[HttpManager sharedClient].securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
||||
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
||||
}
|
||||
|
||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||
@@ -64,46 +50,41 @@
|
||||
CDVPluginResult* pluginResult = nil;
|
||||
bool allow = [[command.arguments objectAtIndex:0] boolValue];
|
||||
|
||||
[HttpManager sharedClient].securityPolicy.allowInvalidCertificates = allow;
|
||||
securityPolicy.allowInvalidCertificates = allow;
|
||||
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
}
|
||||
|
||||
- (void)acceptAllHosts:(CDVInvokedUrlCommand*)command {
|
||||
- (void)validateDomainName:(CDVInvokedUrlCommand*)command {
|
||||
CDVPluginResult* pluginResult = nil;
|
||||
bool allow = [[command.arguments objectAtIndex:0] boolValue];
|
||||
[HttpManager sharedClient].securityPolicy.validatesDomainName = !allow;
|
||||
bool validate = [[command.arguments objectAtIndex:0] boolValue];
|
||||
|
||||
securityPolicy.validatesDomainName = validate;
|
||||
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
}
|
||||
|
||||
- (void)post:(CDVInvokedUrlCommand*)command {
|
||||
HttpManager *manager = [HttpManager sharedClient];
|
||||
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];
|
||||
[self setRequestHeaders: headers forManager: manager];
|
||||
|
||||
CordovaHttpPlugin* __weak weakSelf = self;
|
||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
||||
[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
[self setResults: dictionary withTask: task];
|
||||
[dictionary setObject:responseObject forKey:@"data"];
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
[self setResults: dictionary withTask: task];
|
||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
@@ -111,32 +92,25 @@
|
||||
}
|
||||
|
||||
- (void)get:(CDVInvokedUrlCommand*)command {
|
||||
HttpManager *manager = [HttpManager sharedClient];
|
||||
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];
|
||||
[self setRequestHeaders: headers forManager: manager];
|
||||
|
||||
CordovaHttpPlugin* __weak weakSelf = self;
|
||||
|
||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
||||
[manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
[self setResults: dictionary withTask: task];
|
||||
[dictionary setObject:responseObject forKey:@"data"];
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
[self setResults: dictionary withTask: task];
|
||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
@@ -144,32 +118,25 @@
|
||||
}
|
||||
|
||||
- (void)head:(CDVInvokedUrlCommand*)command {
|
||||
HttpManager *manager = [HttpManager sharedClient];
|
||||
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];
|
||||
[self setRequestHeaders: headers forManager: manager];
|
||||
|
||||
CordovaHttpPlugin* __weak weakSelf = self;
|
||||
|
||||
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
|
||||
[manager HEAD:url parameters:parameters success:^(NSURLSessionTask *task) {
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
[self setResults: dictionary withTask: task];
|
||||
// no 'body' for HEAD request, omitting 'data'
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
[self setResults: dictionary withTask: task];
|
||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
@@ -177,7 +144,8 @@
|
||||
}
|
||||
|
||||
- (void)uploadFile:(CDVInvokedUrlCommand*)command {
|
||||
HttpManager *manager = [HttpManager sharedClient];
|
||||
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];
|
||||
@@ -186,7 +154,7 @@
|
||||
|
||||
NSURL *fileURL = [NSURL fileURLWithPath: filePath];
|
||||
|
||||
[self setRequestHeaders: headers];
|
||||
[self setRequestHeaders: headers forManager: manager];
|
||||
|
||||
CordovaHttpPlugin* __weak weakSelf = self;
|
||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
||||
@@ -203,20 +171,12 @@
|
||||
}
|
||||
} progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
[self setResults: dictionary withTask: task];
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
[self setResults: dictionary withTask: task];
|
||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
@@ -225,13 +185,14 @@
|
||||
|
||||
|
||||
- (void)downloadFile:(CDVInvokedUrlCommand*)command {
|
||||
HttpManager *manager = [HttpManager sharedClient];
|
||||
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];
|
||||
NSString *filePath = [command.arguments objectAtIndex: 3];
|
||||
|
||||
[self setRequestHeaders: headers];
|
||||
[self setRequestHeaders: headers forManager: manager];
|
||||
|
||||
if ([filePath hasPrefix:@"file://"]) {
|
||||
filePath = [filePath substringFromIndex:7];
|
||||
@@ -291,21 +252,13 @@
|
||||
|
||||
id filePlugin = [self.commandDelegate getCommandInstance:@"File"];
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
[self setResults: dictionary withTask: task];
|
||||
[dictionary setObject:[filePlugin getDirectoryEntry:filePath isDirectory:NO] forKey:@"file"];
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
if (task.response != nil) {
|
||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
||||
}
|
||||
[self setResults: dictionary withTask: task];
|
||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
|
||||
Reference in New Issue
Block a user