mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-04-24 00:00:03 +08:00
fixed some issues with responseSerializers and moved some functions out of obj-c into javascript
This commit is contained in:
Vendored
+40
-14
@@ -6,12 +6,36 @@
|
||||
|
||||
var exec = require('cordova/exec');
|
||||
|
||||
// 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 String.fromCharCode('0x' + p1);
|
||||
}));
|
||||
}
|
||||
|
||||
function mergeHeaders(globalHeaders, localHeaders) {
|
||||
var globalKeys = Object.keys(globalHeaders);
|
||||
var key;
|
||||
for (var i = 0; i < globalKeys.length; i++) {
|
||||
key = globalKeys[i];
|
||||
if (!localHeaders.hasOwnProperty(key)) {
|
||||
localHeaders[key] = globalHeaders[key];
|
||||
}
|
||||
}
|
||||
return localHeaders;
|
||||
}
|
||||
|
||||
var http = {
|
||||
useBasicAuth: function(username, password, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "useBasicAuth", [username, password]);
|
||||
headers: {},
|
||||
sslPinning: false,
|
||||
getBasicAuthHeader: function(username, password) {
|
||||
return {'Authorization': 'Basic ' + b64EncodeUnicode(username + ':' + password)};
|
||||
},
|
||||
setHeader: function(header, value, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "setHeader", [header, value]);
|
||||
useBasicAuth: function(username, password) {
|
||||
this.headers.Authorization = 'Basic ' + b64EncodeUnicode(username + ':' + password);
|
||||
},
|
||||
setHeader: function(header, value) {
|
||||
this.headers[header] = value;
|
||||
},
|
||||
enableSSLPinning: function(enable, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "enableSSLPinning", [enable]);
|
||||
@@ -19,19 +43,23 @@ var http = {
|
||||
acceptAllCerts: function(allow, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "acceptAllCerts", [allow]);
|
||||
},
|
||||
acceptAllHosts: function(allow, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "acceptAllHosts", [allow]);
|
||||
validateDomainName: function(validate, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "validateDomainName", [validate]);
|
||||
},
|
||||
post: function(url, params, headers, success, failure) {
|
||||
headers = mergeHeaders(this.headers, headers);
|
||||
return exec(success, failure, "CordovaHttpPlugin", "post", [url, params, headers]);
|
||||
},
|
||||
get: function(url, params, headers, success, failure) {
|
||||
headers = mergeHeaders(this.headers, headers);
|
||||
return exec(success, failure, "CordovaHttpPlugin", "get", [url, params, headers]);
|
||||
},
|
||||
head: function(url, params, headers, success, failure) {
|
||||
headers = mergeHeaders(this.headers, headers);
|
||||
return exec(success, failure, "CordovaHttpPlugin", "head", [url, params, headers]);
|
||||
},
|
||||
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]);
|
||||
},
|
||||
downloadFile: function(url, params, headers, filePath, success, failure) {
|
||||
@@ -57,6 +85,7 @@ var http = {
|
||||
* Modified by Andrew Stephan for Sync OnSet
|
||||
*
|
||||
*/
|
||||
headers = mergeHeaders(this.headers, headers);
|
||||
var win = function(result) {
|
||||
var entry = new (require('cordova-plugin-file.FileEntry'))();
|
||||
entry.isDirectory = false;
|
||||
@@ -107,20 +136,17 @@ if (typeof angular !== "undefined") {
|
||||
}
|
||||
|
||||
var cordovaHTTP = {
|
||||
useBasicAuth: function(username, password) {
|
||||
return makePromise(http.useBasicAuth, [username, password]);
|
||||
},
|
||||
setHeader: function(header, value) {
|
||||
return makePromise(http.setHeader, [header, value]);
|
||||
},
|
||||
getBasicAuthHeader: http.getBasicAuthHeader,
|
||||
useBasicAuth: http.useBasicAuth,
|
||||
setHeader: http.setHeader,
|
||||
enableSSLPinning: function(enable) {
|
||||
return makePromise(http.enableSSLPinning, [enable]);
|
||||
},
|
||||
acceptAllCerts: function(allow) {
|
||||
return makePromise(http.acceptAllCerts, [allow]);
|
||||
},
|
||||
acceptAllHosts: function(allow) {
|
||||
return makePromise(http.acceptAllHosts, [allow]);
|
||||
validateDomainName: function(validate) {
|
||||
return makePromise(http.validateDomainName, [validate]);
|
||||
},
|
||||
post: function(url, params, headers) {
|
||||
return makePromise(http.post, [url, params, headers], true);
|
||||
|
||||
Reference in New Issue
Block a user