Skip to content

Commit

Permalink
Fixed ios code to work with AFNetwork 3.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Stephan committed Feb 19, 2016
1 parent 434d8df commit 340e45a
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 92 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
15 changes: 3 additions & 12 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.synconset.cordovaHTTP"
version="0.1.4">
id="cordova-plugin-http"
version="0.2.0">

<name>SSL Pinning</name>

Expand All @@ -17,7 +17,7 @@
<dependency id="cordova-plugin-file" version=">=2.0.0" />

<js-module src="www/cordovaHTTP.js" name="CordovaHttpPlugin">
<clobbers target="plugins.CordovaHttpPlugin" />
<clobbers target="CordovaHttpPlugin" />
</js-module>

<!-- ios -->
Expand All @@ -37,12 +37,6 @@
<header-file src="src/ios/TextResponseSerializer.h" />
<source-file src="src/ios/TextResponseSerializer.m" />

<header-file src="src/ios/AFNetworking/AFHTTPRequestOperation.h" />
<source-file src="src/ios/AFNetworking/AFHTTPRequestOperation.m" />

<header-file src="src/ios/AFNetworking/AFHTTPRequestOperationManager.h" />
<source-file src="src/ios/AFNetworking/AFHTTPRequestOperationManager.m" />

<header-file src="src/ios/AFNetworking/AFHTTPSessionManager.h" />
<source-file src="src/ios/AFNetworking/AFHTTPSessionManager.m" />

Expand All @@ -54,9 +48,6 @@
<header-file src="src/ios/AFNetworking/AFSecurityPolicy.h" />
<source-file src="src/ios/AFNetworking/AFSecurityPolicy.m" />

<header-file src="src/ios/AFNetworking/AFURLConnectionOperation.h" />
<source-file src="src/ios/AFNetworking/AFURLConnectionOperation.m" />

<header-file src="src/ios/AFNetworking/AFURLRequestSerialization.h" />
<source-file src="src/ios/AFNetworking/AFURLRequestSerialization.m" />

Expand Down
165 changes: 87 additions & 78 deletions src/ios/CordovaHttpPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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];
Expand Down Expand Up @@ -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];
Expand All @@ -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
Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions src/ios/HTTPManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
// THE SOFTWARE.
// Modified by Andrew Stephan
#import <Foundation/Foundation.h>
#import "AFHTTPRequestOperationManager.h"
#import "AFHTTPSessionManager.h"

@interface HttpManager : AFHTTPRequestOperationManager
@interface HttpManager : AFHTTPSessionManager

+ (instancetype)sharedClient;

Expand Down
3 changes: 3 additions & 0 deletions www/cordovaHTTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
33 changes: 33 additions & 0 deletions zedconfig.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
}
}
}

0 comments on commit 340e45a

Please sign in to comment.