diff --git a/plugin.xml b/plugin.xml index 1867e44..f161842 100644 --- a/plugin.xml +++ b/plugin.xml @@ -16,20 +16,20 @@ - - + + - - + + - - + + diff --git a/src/ios/HTTPSPinning.h b/src/ios/CordovaHTTP.h similarity index 95% rename from src/ios/HTTPSPinning.h rename to src/ios/CordovaHTTP.h index 92369bf..e955c82 100644 --- a/src/ios/HTTPSPinning.h +++ b/src/ios/CordovaHTTP.h @@ -3,7 +3,7 @@ #import #import -@interface HTTPSPinning : CDVPlugin +@interface CordovaHTTP : CDVPlugin - (void)setAuthorizationHeaderWithUsernameAndPassword:(CDVInvokedUrlCommand*)command; - (void)setHeader:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/HTTPSPinning.m b/src/ios/CordovaHTTP.m similarity index 96% rename from src/ios/HTTPSPinning.m rename to src/ios/CordovaHTTP.m index fa0dd3d..e747c66 100644 --- a/src/ios/HTTPSPinning.m +++ b/src/ios/CordovaHTTP.m @@ -1,9 +1,9 @@ -#import "HTTPSPinning.h" +#import "CordovaHTTP.h" #import "CDVFile.h" #import "TextResponseSerializer.h" #import "HTTPManager.h" -@implementation HTTPSPinning +@implementation CordovaHTTP - (void) setAuthorizationHeaderWithUsernameAndPassword:(CDVInvokedUrlCommand*)command { NSString *username = [command.arguments objectAtIndex:0]; @@ -92,7 +92,7 @@ NSString *url = [command.arguments objectAtIndex:0]; NSDictionary *parameters = [command.arguments objectAtIndex:1]; - HTTPSPinning* __weak weakSelf = self; + CordovaHTTP* __weak weakSelf = self; [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; @@ -100,14 +100,12 @@ [dictionary setObject:responseObject forKey:@"data"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - NSLog(@"Response: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; [dictionary setObject:[error localizedDescription] forKey:@"error"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - NSLog(@"Error: %@", error); }]; } @@ -116,7 +114,7 @@ NSString *url = [command.arguments objectAtIndex:0]; NSDictionary *parameters = [command.arguments objectAtIndex:1]; - HTTPSPinning* __weak weakSelf = self; + CordovaHTTP* __weak weakSelf = self; [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; @@ -124,14 +122,12 @@ [dictionary setObject:responseObject forKey:@"data"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - NSLog(@"Response: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; [dictionary setObject:[error localizedDescription] forKey:@"error"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - NSLog(@"Error: %@", error); }]; } @@ -144,9 +140,7 @@ NSURL *fileURL = [NSURL fileURLWithPath: filePath]; - NSLog(@"%@", fileURL); - - HTTPSPinning* __weak weakSelf = self; + CordovaHTTP* __weak weakSelf = self; [manager POST:url parameters:parameters constructingBodyWithBlock:^(id formData) { NSError *error; [formData appendPartWithFileURL:fileURL name:name error:&error]; @@ -169,7 +163,6 @@ [dictionary setObject:[error localizedDescription] forKey:@"error"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - NSLog(@"Error: %@", error); }]; } @@ -180,7 +173,7 @@ NSDictionary *parameters = [command.arguments objectAtIndex:1]; NSString *filePath = [command.arguments objectAtIndex: 2]; - HTTPSPinning* __weak weakSelf = self; + CordovaHTTP* __weak weakSelf = self; [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { /* * @@ -243,7 +236,6 @@ [dictionary setObject:[error localizedDescription] forKey:@"error"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - NSLog(@"Error: %@", error); }]; } diff --git a/www/cordovaHTTP.js b/www/cordovaHTTP.js new file mode 100644 index 0000000..964b8df --- /dev/null +++ b/www/cordovaHTTP.js @@ -0,0 +1,159 @@ +/*global angular*/ + +/* + * An HTTP Plugin for PhoneGap. + */ + +var exec = require('cordova/exec'); + +var http = { + SSLPinningMode: { + None: 0, + Certificate: 1, + PublicKey: 2 + }, + setAuthorizationHeaderWithUsernameAndPassword: function(username, password, success, failure) { + return exec(success, failure, "CordovaHTTP", "setAuthorizationHeaderWithUsernameAndPassword", [username, password]); + }, + setHeader: function(header, value, success, failure) { + return exec(success, failure, "CordovaHTTP", "setHeader", [header, value]); + }, + setSSLPinningMode: function(mode, success, failure) { + return exec(success, failure, "CordovaHTTP", "setSSLPinningMode", [mode]); + }, + validateEntireCertificateChain: function(validateChain, success, failure) { + return exec(success, failure, "CordovaHTTP", "validateEntireCertificateChain", [validateChain]); + }, + allowInvalidCertificates: function(allow, success, failure) { + return exec(success, failure, "CordovaHTTP", "allowInvalidCertificates", [allow]); + }, + acceptText: function(success, failure) { + return exec(success, failure, "CordovaHTTP", "acceptText", []); + }, + acceptData: function(success, failure) { + return exec(success, failure, "CordovaHTTP", "acceptData", []); + }, + setAcceptableContentTypes: function(contentTypes, success, failure) { + return exec(success, failure, "CordovaHTTP", "setAcceptableContentTypes", contentTypes); + }, + post: function(url, params, success, failure) { + return exec(success, failure, "CordovaHTTP", "post", [url, params]); + }, + get: function(url, params, success, failure) { + return exec(success, failure, "CordovaHTTP", "get", [url, params]); + }, + uploadFile: function(url, params, filePath, name, success, failure) { + return exec(success, failure, "CordovaHTTP", "uploadFile", [url, params, filePath, name]); + }, + downloadFile: function(url, params, filePath, success, failure) { + /* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * Modified by Andrew Stephan for Sync OnSet + * + */ + var win = function(result) { + var entry = new (require('org.apache.cordova.core.file.FileEntry'))(); + entry.isDirectory = false; + entry.isFile = true; + entry.name = result.file.name; + entry.fullPath = result.file.fullPath; + success(entry); + }; + return exec(win, failure, "CordovaHTTP", "downloadFile", [url, params, filePath]); + } +}; + +if (angular) { + angular.module('cordovaHTTP', []).factory('cordovaHTTP', function($timeout, $q) { + function makePromise(fn, args, async) { + var deferred = $q.defer(); + + var success = function(response) { + if (async) { + $timeout(function() { + deferred.resolve(response); + }); + } else { + deferred.resolve(response); + } + }; + + var fail = function(response) { + if (async) { + $timeout(function() { + deferred.reject(response); + }); + } else { + deferred.reject(response); + } + }; + + args.push(success); + args.push(fail); + + fn.apply(http, args); + + return deferred.promise; + } + + var cordovaHTTP = { + SSLPinningMode: http.SSLPinningMode, + setAuthorizationHeaderWithUsernameAndPassword: function(username, password) { + return makePromise(http.setAuthorizationHeaderWithUsernameAndPassword, [username, password]); + }, + setHeader: function(header, value) { + return makePromise(http.setHeader, [header, value]); + }, + setSSLPinningMode: function(mode) { + return makePromise(http.setSSLPinningMode, [mode]); + }, + validateEntireCertificateChain: function(validateChain) { + return makePromise(http.validateEntireCertificateChain, [validateChain]); + }, + allowInvalidCertificates: function(allow) { + return makePromise(http.allowInvalidCertificates, [allow]); + }, + acceptText: function() { + return makePromise(http.acceptText, []); + }, + acceptData: function() { + return makePromise(http.acceptData, []); + }, + setAcceptableContentTypes: function(contentTypes) { + return makePromise(http.setAcceptableContentTypes, [contentTypes]); + }, + post: function(url, params) { + return makePromise(http.post, [url, params], true); + }, + get: function(url, params) { + return makePromise(http.get, [url, params], true); + }, + uploadFile: function(url, params, filePath, name) { + return makePromise(http.uploadFile, [url, params, filePath, name], true); + }, + downloadFile: function(url, params, filePath) { + return makePromise(http.downloadFile, [url, params, filePath], true); + } + }; + return cordovaHTTP; + }); +} else { + window.cordovaHTTP = http; +} \ No newline at end of file diff --git a/www/httpsp.js b/www/httpsp.js deleted file mode 100644 index 498dc78..0000000 --- a/www/httpsp.js +++ /dev/null @@ -1,83 +0,0 @@ -/* - * An HTTP Plugin for PhoneGap. - */ - -var exec = require('cordova/exec'); - -var http = { - SSLPinningMode: { - None: 0, - Certificate: 1, - PublicKey: 2 - }, - setAuthorizationHeaderWithUsernameAndPassword: function(username, password, success, failure) { - return exec(success, failure, "HTTPSPinning", "setAuthorizationHeaderWithUsernameAndPassword", [username, password]); - }, - setHeader: function(header, value, success, failure) { - return exec(success, failure, "HTTPSPinning", "setHeader", [header, value]); - }, - setSSLPinningMode: function(mode, success, failure) { - return exec(success, failure, "HTTPSPinning", "setSSLPinningMode", [mode]); - }, - validateEntireCertificateChain: function(validateChain, success, failure) { - return exec(success, failure, "HTTPSPinning", "validateEntireCertificateChain", [validateChain]); - }, - allowInvalidCertificates: function(allow, success, failure) { - return exec(success, failure, "HTTPSPinning", "allowInvalidCertificates", [allow]); - }, - acceptText: function(success, failure) { - return exec(success, failure, "HTTPSPinning", "acceptText", []); - }, - acceptData: function(contentTypes, success, failure) { - return exec(success, failure, "HTTPSPinning", "acceptData", []); - }, - setAcceptableContentTypes: function(contentTypes, success, failure) { - return exec(success, failure, "HTTPSPinning", "setAcceptableContentTypes", contentTypes); - }, - post: function(url, params, success, failure) { - return exec(success, failure, "HTTPSPinning", "post", [url, params]); - }, - get: function(url, params, success, failure) { - return exec(success, failure, "HTTPSPinning", "get", [url, params]); - }, - uploadFile: function(url, params, filePath, name, success, failure) { - return exec(success, failure, "HTTPSPinning", "uploadFile", [url, params, filePath, name]); - }, - downloadFile: function(url, params, filePath, success, failure) { - /* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - * Modified by Andrew Stephan for Sync OnSet - * - */ - var win = function(result) { - var entry = new (require('org.apache.cordova.core.file.FileEntry'))(); - entry.isDirectory = false; - entry.isFile = true; - console.log(result); - console.log(JSON.stringify(result)); - entry.name = result.file.name; - entry.fullPath = result.file.fullPath; - success(entry); - }; - return exec(win, failure, "HTTPSPinning", "downloadFile", [url, params, filePath]); - } -}; - -window.http = http; \ No newline at end of file