From 9f515d01d8bb5dcb7cff82f41cacc50b8614cc1e Mon Sep 17 00:00:00 2001 From: Sefa Ilkimen Date: Thu, 10 Nov 2016 18:46:30 +0100 Subject: [PATCH] defined www interface for configurable parameter serializer --- CHANGELOG.md | 8 ++- package.json | 8 +-- plugin.xml | 12 ++-- www/cordovaHTTP.js | 156 ++++++++++++++++++++++++++------------------- zedconfig.json | 33 ---------- 5 files changed, 108 insertions(+), 109 deletions(-) delete mode 100644 zedconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b848f1..ad21d26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## v1.3.0 + +- forked from "cordova-plugin-http" v1.2.0 (see https://github.com/wymsee/cordova-HTTP) + +# Previous changelog (cordova-plugin-http) + ## v1.2.0 - Added support for TLSv1.1 and TLSv1.2 for android versions 4.1-4.4 (API levels 16-19) @@ -72,4 +78,4 @@ - Reports SSL Handshake errors rather than giving a generic error (Thanks to devgeeks) - Exporting http as a module (Thanks to pvsaikrishna) - Added Limitations section to readme (Thanks to cvillerm) -- Fixed examples (Thanks to hideov) \ No newline at end of file +- Fixed examples (Thanks to hideov) diff --git a/package.json b/package.json index 5004d0f..008c5fb 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "name": "cordova-plugin-http", - "version": "1.2.0", + "name": "cordova-plugin-advanced-http", + "version": "1.3.0", "description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning", "cordova": { - "id": "cordova-plugin-http", + "id": "cordova-plugin-advanced-http", "platforms": [ "ios", "android" @@ -11,7 +11,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/wymsee/cordova-HTTP.git" + "url": "git+https://github.com/silkimen/cordova-plugin-advanced-http.git" }, "keywords": [ "cordova", diff --git a/plugin.xml b/plugin.xml index fc26d9c..0eb7c5f 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,14 +1,14 @@ + id="cordova-plugin-advanced-http" + version="1.3.0"> SSL Pinning - + Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning - + @@ -30,7 +30,7 @@ - + @@ -69,7 +69,7 @@ - + diff --git a/www/cordovaHTTP.js b/www/cordovaHTTP.js index bbcd2e0..e98cf94 100644 --- a/www/cordovaHTTP.js +++ b/www/cordovaHTTP.js @@ -1,14 +1,40 @@ /*global angular*/ +/* + * + * 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 + * Modified by Sefa Ilkimen: + * - added configurable params serializer + * +*/ + /* * An HTTP Plugin for PhoneGap. */ var exec = require('cordova/exec'); +var validSerializers = ['urlencoded', 'json']; // Thanks Mozilla: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_.22Unicode_Problem.22 function b64EncodeUnicode(str) { - return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { + return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) { return String.fromCharCode('0x' + p1); })); } @@ -25,68 +51,65 @@ function mergeHeaders(globalHeaders, localHeaders) { return localHeaders; } +function checkSerializer(serializer) { + serializer = serializer || ''; + serializer = serializer.trim().toLowerCase(); + + if (validSerializers.indexOf(serializer) > -1) { + return serializer; + } + + return serializer[0]; +} + var http = { headers: {}, + paramSerializer: 'urlencoded', sslPinning: false, - getBasicAuthHeader: function(username, password) { + getBasicAuthHeader: function (username, password) { return {'Authorization': 'Basic ' + b64EncodeUnicode(username + ':' + password)}; }, - useBasicAuth: function(username, password) { + useBasicAuth: function (username, password) { this.headers.Authorization = 'Basic ' + b64EncodeUnicode(username + ':' + password); }, - setHeader: function(header, value) { + setHeader: function (header, value) { this.headers[header] = value; }, - enableSSLPinning: function(enable, success, failure) { - return exec(success, failure, "CordovaHttpPlugin", "enableSSLPinning", [enable]); + setParamSerializer: function (serializer) { + this.paramSerializer = checkSerializer(serializer); }, - acceptAllCerts: function(allow, success, failure) { - return exec(success, failure, "CordovaHttpPlugin", "acceptAllCerts", [allow]); + enableSSLPinning: function (enable, success, failure) { + return exec(success, failure, 'CordovaHttpPlugin', 'enableSSLPinning', [enable]); }, - validateDomainName: function(validate, success, failure) { - return exec(success, failure, "CordovaHttpPlugin", "validateDomainName", [validate]); + acceptAllCerts: function (allow, success, failure) { + return exec(success, failure, 'CordovaHttpPlugin', 'acceptAllCerts', [allow]); }, - post: function(url, params, headers, success, failure) { + validateDomainName: function (validate, success, failure) { + return exec(success, failure, 'CordovaHttpPlugin', 'validateDomainName', [validate]); + }, + post: function (url, params, headers, success, failure) { + params = params || {}; + headers = headers || {}; headers = mergeHeaders(this.headers, headers); - return exec(success, failure, "CordovaHttpPlugin", "post", [url, params, headers]); + return exec(success, failure, 'CordovaHttpPlugin', 'post', [url, params, this.serializer, headers]); }, - get: function(url, params, headers, success, failure) { + get: function (url, params, headers, success, failure) { + params = params || {}; + headers = headers || {}; headers = mergeHeaders(this.headers, headers); - return exec(success, failure, "CordovaHttpPlugin", "get", [url, params, headers]); + return exec(success, failure, 'CordovaHttpPlugin', 'get', [url, params, headers]); }, - head: function(url, params, headers, success, failure) { + head: function (url, params, headers, success, failure) { headers = mergeHeaders(this.headers, headers); - return exec(success, failure, "CordovaHttpPlugin", "head", [url, params, headers]); + return exec(success, failure, 'CordovaHttpPlugin', 'head', [url, params, headers]); }, - uploadFile: function(url, params, headers, filePath, name, success, failure) { + uploadFile: function (url, params, headers, filePath, name, success, failure) { headers = mergeHeaders(this.headers, headers); - return exec(success, failure, "CordovaHttpPlugin", "uploadFile", [url, params, headers, filePath, name]); + return exec(success, failure, 'CordovaHttpPlugin', 'uploadFile', [url, params, headers, filePath, name]); }, - downloadFile: function(url, params, headers, 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 - * - */ + downloadFile: function (url, params, headers, filePath, success, failure) { headers = mergeHeaders(this.headers, headers); - var win = function(result) { + var win = function (result) { var entry = new (require('cordova-plugin-file.FileEntry'))(); entry.isDirectory = false; entry.isFile = true; @@ -96,75 +119,78 @@ var http = { entry.nativeURL = result.file.nativeURL; success(entry); }; - return exec(win, failure, "CordovaHttpPlugin", "downloadFile", [url, params, headers, filePath]); + return exec(win, failure, 'CordovaHttpPlugin', 'downloadFile', [url, params, headers, filePath]); } }; module.exports = http; -if (typeof angular !== "undefined") { - angular.module('cordovaHTTP', []).factory('cordovaHTTP', function($timeout, $q) { +if (typeof angular !== 'undefined') { + angular.module('cordovaHTTP', []).factory('cordovaHTTP', function ($timeout, $q) { function makePromise(fn, args, async) { var deferred = $q.defer(); - - var success = function(response) { + + var success = function (response) { if (async) { - $timeout(function() { + $timeout(function () { deferred.resolve(response); }); } else { deferred.resolve(response); } }; - - var fail = function(response) { + + var fail = function (response) { if (async) { - $timeout(function() { + $timeout(function () { deferred.reject(response); }); } else { deferred.reject(response); } }; - + args.push(success); args.push(fail); - + fn.apply(http, args); - + return deferred.promise; } - + var cordovaHTTP = { getBasicAuthHeader: http.getBasicAuthHeader, - useBasicAuth: function(username, password) { + useBasicAuth: function (username, password) { return http.useBasicAuth(username, password); }, - setHeader: function(header, value) { + setHeader: function (header, value) { return http.setHeader(header, value); }, - enableSSLPinning: function(enable) { + setParamSerializer: function (serializer) { + return http.setParamSerializer(serializer); + }, + enableSSLPinning: function (enable) { return makePromise(http.enableSSLPinning, [enable]); }, - acceptAllCerts: function(allow) { + acceptAllCerts: function (allow) { return makePromise(http.acceptAllCerts, [allow]); }, - validateDomainName: function(validate) { + validateDomainName: function (validate) { return makePromise(http.validateDomainName, [validate]); }, - post: function(url, params, headers) { + post: function (url, params, headers) { return makePromise(http.post, [url, params, headers], true); }, - get: function(url, params, headers) { + get: function (url, params, headers) { return makePromise(http.get, [url, params, headers], true); }, - head: function(url, params, headers) { + head: function (url, params, headers) { return makePromise(http.head, [url, params, headers], true); }, - uploadFile: function(url, params, headers, filePath, name) { + uploadFile: function (url, params, headers, filePath, name) { return makePromise(http.uploadFile, [url, params, headers, filePath, name], true); }, - downloadFile: function(url, params, headers, filePath) { + downloadFile: function (url, params, headers, filePath) { return makePromise(http.downloadFile, [url, params, headers, filePath], true); } }; diff --git a/zedconfig.json b/zedconfig.json deleted file mode 100644 index e6a16fa..0000000 --- a/zedconfig.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "preferences": { - "tabSize": 2, - "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