fixed some issues with responseSerializers and moved some functions out of obj-c into javascript

This commit is contained in:
Andrew Stephan
2016-02-19 17:13:48 -05:00
parent 340e45a381
commit c35af633a4
6 changed files with 88 additions and 177 deletions
+1 -3
View File
@@ -4,11 +4,9 @@
@interface CordovaHttpPlugin : CDVPlugin
- (void)useBasicAuth:(CDVInvokedUrlCommand*)command;
- (void)setHeader:(CDVInvokedUrlCommand*)command;
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command;
- (void)acceptAllCerts:(CDVInvokedUrlCommand*)command;
- (void)acceptAllHosts:(CDVInvokedUrlCommand*)command;
- (void)validateDomainName:(CDVInvokedUrlCommand*)command;
- (void)post:(CDVInvokedUrlCommand*)command;
- (void)get:(CDVInvokedUrlCommand*)command;
- (void)uploadFile:(CDVInvokedUrlCommand*)command;
+47 -94
View File
@@ -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];
-28
View File
@@ -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
-35
View File
@@ -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