From 340e45a3811debc56682ff05a251fad68b5a3f09 Mon Sep 17 00:00:00 2001 From: Andrew Stephan Date: Thu, 18 Feb 2016 20:31:21 -0500 Subject: [PATCH] Fixed ios code to work with AFNetwork 3.0.x --- .editorconfig | 22 +++++ plugin.xml | 15 +--- src/ios/CordovaHttpPlugin.m | 165 +++++++++++++++++++----------------- src/ios/HTTPManager.h | 4 +- www/cordovaHTTP.js | 3 + zedconfig.json | 33 ++++++++ 6 files changed, 150 insertions(+), 92 deletions(-) create mode 100644 .editorconfig create mode 100644 zedconfig.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..e4f81142 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,22 @@ + +# EditorConfig helps developers define and maintain consistent +# coding styles between different editors and IDEs +# editorconfig.org + +root = true + + +[*] + +# Change these settings to your own preference +indent_style = space +indent_size = 4 + +# We recommend you to keep these unchanged +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false \ No newline at end of file diff --git a/plugin.xml b/plugin.xml index 481df5d0..c3f01cf5 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,8 +1,8 @@ + id="cordova-plugin-http" + version="0.2.0"> SSL Pinning @@ -17,7 +17,7 @@ - + @@ -37,12 +37,6 @@ - - - - - - @@ -54,9 +48,6 @@ - - - diff --git a/src/ios/CordovaHttpPlugin.m b/src/ios/CordovaHttpPlugin.m index 97763fe5..eefaa456 100644 --- a/src/ios/CordovaHttpPlugin.m +++ b/src/ios/CordovaHttpPlugin.m @@ -52,7 +52,6 @@ - (void)enableSSLPinning:(CDVInvokedUrlCommand*)command { bool enable = [[command.arguments objectAtIndex:0] boolValue]; if (enable) { [HttpManager sharedClient].securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate]; - [HttpManager sharedClient].securityPolicy.validatesCertificateChain = NO; } else { [HttpManager sharedClient].securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; } @@ -80,64 +79,68 @@ - (void)acceptAllHosts:(CDVInvokedUrlCommand*)command { } - (void)post:(CDVInvokedUrlCommand*)command { - HttpManager *manager = [HttpManager sharedClient]; - NSString *url = [command.arguments objectAtIndex:0]; - NSDictionary *parameters = [command.arguments objectAtIndex:1]; - NSDictionary *headers = [command.arguments objectAtIndex:2]; - [self setRequestHeaders: headers]; + HttpManager *manager = [HttpManager sharedClient]; + NSString *url = [command.arguments objectAtIndex:0]; + NSDictionary *parameters = [command.arguments objectAtIndex:1]; + NSDictionary *headers = [command.arguments objectAtIndex:2]; + [self setRequestHeaders: headers]; - CordovaHttpPlugin* __weak weakSelf = self; - manager.responseSerializer = [TextResponseSerializer serializer]; - [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - if (operation.response != nil) { - [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; - } - [dictionary setObject:responseObject forKey:@"data"]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - if (operation.response != nil) { - [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; - } - [dictionary setObject:[error localizedDescription] forKey:@"error"]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + 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"]; + } + [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"]; + } + [dictionary setObject:[error localizedDescription] forKey:@"error"]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; } - (void)get:(CDVInvokedUrlCommand*)command { - HttpManager *manager = [HttpManager sharedClient]; - NSString *url = [command.arguments objectAtIndex:0]; - NSDictionary *parameters = [command.arguments objectAtIndex:1]; - NSDictionary *headers = [command.arguments objectAtIndex:2]; - [self setRequestHeaders: headers]; + HttpManager *manager = [HttpManager sharedClient]; + NSString *url = [command.arguments objectAtIndex:0]; + NSDictionary *parameters = [command.arguments objectAtIndex:1]; + NSDictionary *headers = [command.arguments objectAtIndex:2]; + [self setRequestHeaders: headers]; - CordovaHttpPlugin* __weak weakSelf = self; + CordovaHttpPlugin* __weak weakSelf = self; - manager.responseSerializer = [TextResponseSerializer serializer]; - [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - if (operation.response != nil) { - [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; - } - [dictionary setObject:responseObject forKey:@"data"]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - if (operation.response != nil) { - [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; - } - [dictionary setObject:[error localizedDescription] forKey:@"error"]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + 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"]; + } + [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"]; + } + [dictionary setObject:[error localizedDescription] forKey:@"error"]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; } - (void)head:(CDVInvokedUrlCommand*)command { @@ -150,20 +153,22 @@ - (void)head:(CDVInvokedUrlCommand*)command { CordovaHttpPlugin* __weak weakSelf = self; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; - [manager HEAD:url parameters:parameters success:^(AFHTTPRequestOperation *operation) { + [manager HEAD:url parameters:parameters success:^(NSURLSessionTask *task) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - if (operation.response != nil) { - [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + if (task.response != nil) { + 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' CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + } failure:^(NSURLSessionTask *task, NSError *error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - if (operation.response != nil) { - [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + if (task.response != nil) { + 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"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; @@ -191,24 +196,26 @@ - (void)uploadFile:(CDVInvokedUrlCommand*)command { if (error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:500] forKey:@"status"]; - [dictionary setObject:@"Could not add image to post body." forKey:@"error"]; + [dictionary setObject:@"Could not add file to post body." forKey:@"error"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; return; } - } success:^(AFHTTPRequestOperation *operation, id responseObject) { + } progress:nil success:^(NSURLSessionTask *task, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - if (operation.response != nil) { - [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + if (task.response != nil) { + 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]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + } failure:^(NSURLSessionTask *task, NSError *error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - if (operation.response != nil) { - [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + if (task.response != nil) { + 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"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; @@ -232,7 +239,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command { CordovaHttpPlugin* __weak weakSelf = self; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; - [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { + [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { /* * * Licensed to the Apache Software Foundation (ASF) under one @@ -284,18 +291,20 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command { id filePlugin = [self.commandDelegate getCommandInstance:@"File"]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - if (operation.response != nil) { - [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + if (task.response != nil) { + 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"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + } failure:^(NSURLSessionTask *task, NSError *error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - if (operation.response != nil) { - [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + if (task.response != nil) { + 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"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; diff --git a/src/ios/HTTPManager.h b/src/ios/HTTPManager.h index e4633d2f..9367bbe7 100644 --- a/src/ios/HTTPManager.h +++ b/src/ios/HTTPManager.h @@ -19,9 +19,9 @@ // THE SOFTWARE. // Modified by Andrew Stephan #import -#import "AFHTTPRequestOperationManager.h" +#import "AFHTTPSessionManager.h" -@interface HttpManager : AFHTTPRequestOperationManager +@interface HttpManager : AFHTTPSessionManager + (instancetype)sharedClient; diff --git a/www/cordovaHTTP.js b/www/cordovaHTTP.js index 9613f3e5..8c3db92f 100644 --- a/www/cordovaHTTP.js +++ b/www/cordovaHTTP.js @@ -119,6 +119,9 @@ if (typeof angular !== "undefined") { acceptAllCerts: function(allow) { return makePromise(http.acceptAllCerts, [allow]); }, + acceptAllHosts: function(allow) { + return makePromise(http.acceptAllHosts, [allow]); + }, post: function(url, params, headers) { return makePromise(http.post, [url, params, headers], true); }, diff --git a/zedconfig.json b/zedconfig.json new file mode 100644 index 00000000..3f52390a --- /dev/null +++ b/zedconfig.json @@ -0,0 +1,33 @@ +{ + "preferences": { + "tabSize": 4, + "wordWrap": true, + "useSoftTabs": true, + "gotoExclude": [] + }, + "packages": [ + "gh:wymsee/zed-tools/mobile" + ], + "modes": { + "javascript": { + "commands": { + "Tools:Check": { + "options": { + "globals": { + "angular": true, + "$": true, + "device": true, + "persistence": true, + "moment": true, + "sos": true, + "LocalFileSystem": true, + "Hammer": true, + "AnimationFrame": true, + "Bloodhound": true + } + } + } + } + } + } +} \ No newline at end of file