mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-07-26 00:00:14 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14eef1ca66 | ||
|
|
cf5a684d51 | ||
|
|
c153378b4b | ||
|
|
8092e8b025 | ||
|
|
afb4ea6199 |
@@ -1,5 +1,22 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## v1.5.6
|
||||||
|
|
||||||
|
- All response header keys are converted to lowercase (iOS only)
|
||||||
|
|
||||||
|
## v1.5.5
|
||||||
|
|
||||||
|
- added a function to remove all cookies for a URL
|
||||||
|
|
||||||
|
## v1.5.4
|
||||||
|
|
||||||
|
- fixed an error if the response has no "headers" field
|
||||||
|
|
||||||
|
## v1.5.3
|
||||||
|
|
||||||
|
- handles cookies correctly on non-success response from server
|
||||||
|
- throws error when a callback function is missing
|
||||||
|
|
||||||
## v1.5.2
|
## v1.5.2
|
||||||
|
|
||||||
- fixed missing file "umd-tough-cookie.js“ (caused by missing file ".npmignore")
|
- fixed missing file "umd-tough-cookie.js“ (caused by missing file ".npmignore")
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "cordova-plugin-advanced-http",
|
"name": "cordova-plugin-advanced-http",
|
||||||
"version": "1.5.2",
|
"version": "1.5.6",
|
||||||
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
|
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "cp node_modules/umd-tough-cookie/lib/umd-tough-cookie.js www/umd-tough-cookie.js",
|
"build": "cp node_modules/umd-tough-cookie/lib/umd-tough-cookie.js www/umd-tough-cookie.js",
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
|
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
id="cordova-plugin-advanced-http"
|
id="cordova-plugin-advanced-http"
|
||||||
version="1.5.2">
|
version="1.5.6">
|
||||||
|
|
||||||
<name>Advanced HTTP plugin</name>
|
<name>Advanced HTTP plugin</name>
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager;
|
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager;
|
||||||
- (void)setResults:(NSMutableDictionary*)dictionary withTask:(NSURLSessionTask*)task;
|
- (void)setResults:(NSMutableDictionary*)dictionary withTask:(NSURLSessionTask*)task;
|
||||||
|
- (NSMutableDictionary*)copyHeaderFields:(NSDictionary*)headerFields;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@@ -37,10 +38,22 @@
|
|||||||
if (task.response != nil) {
|
if (task.response != nil) {
|
||||||
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
|
||||||
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
|
||||||
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
|
[dictionary setObject:[self copyHeaderFields:response.allHeaderFields] forKey:@"headers"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (NSMutableDictionary*)copyHeaderFields:(NSDictionary *)headerFields {
|
||||||
|
NSMutableDictionary *headerFieldsCopy = [[NSMutableDictionary alloc] initWithCapacity:headerFields.count];
|
||||||
|
NSString *headerKeyCopy;
|
||||||
|
|
||||||
|
for (NSString *headerKey in headerFields.allKeys) {
|
||||||
|
headerKeyCopy = [[headerKey mutableCopy] lowercaseString];
|
||||||
|
[headerFieldsCopy setValue:[headerFields objectForKey:headerKey] forKey:headerKeyCopy];
|
||||||
|
}
|
||||||
|
|
||||||
|
return headerFieldsCopy;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command {
|
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command {
|
||||||
bool enable = [[command.arguments objectAtIndex:0] boolValue];
|
bool enable = [[command.arguments objectAtIndex:0] boolValue];
|
||||||
if (enable) {
|
if (enable) {
|
||||||
|
|||||||
+84
-19
@@ -22,6 +22,9 @@
|
|||||||
* Modified by Andrew Stephan for Sync OnSet
|
* Modified by Andrew Stephan for Sync OnSet
|
||||||
* Modified by Sefa Ilkimen:
|
* Modified by Sefa Ilkimen:
|
||||||
* - added configurable params serializer
|
* - added configurable params serializer
|
||||||
|
* - added put and delete methods
|
||||||
|
* - using cordova www module pattern
|
||||||
|
* - some minor improvements
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -70,7 +73,7 @@ function checkSerializer(serializer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function resolveCookieString(headers) {
|
function resolveCookieString(headers) {
|
||||||
var keys = Object.keys(headers);
|
var keys = Object.keys(headers || {});
|
||||||
|
|
||||||
for (var i = 0; i < keys.length; ++i) {
|
for (var i = 0; i < keys.length; ++i) {
|
||||||
if (keys[i].match(/^set-cookie$/i)) {
|
if (keys[i].match(/^set-cookie$/i)) {
|
||||||
@@ -81,17 +84,46 @@ function resolveCookieString(headers) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSuccessHandler(url, cb) {
|
function createFileEntry(rawEntry) {
|
||||||
|
var entry = new (require('cordova-plugin-file.FileEntry'))();
|
||||||
|
|
||||||
|
entry.isDirectory = rawEntry.isDirectory;
|
||||||
|
entry.isFile = rawEntry.isFile;
|
||||||
|
entry.name = rawEntry.name;
|
||||||
|
entry.fullPath = rawEntry.fullPath;
|
||||||
|
entry.filesystem = new FileSystem(rawEntry.filesystemName || (rawEntry.filesystem == window.PERSISTENT ? 'persistent' : 'temporary'));
|
||||||
|
entry.nativeURL = rawEntry.nativeURL;
|
||||||
|
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
function injectCookieHandler(url, cb) {
|
||||||
return function(response) {
|
return function(response) {
|
||||||
cookieHandler.setCookieFromString(url, resolveCookieString(response.headers));
|
cookieHandler.setCookieFromString(url, resolveCookieString(response.headers));
|
||||||
cb(response);
|
cb(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function injectFileEntryHandler(cb) {
|
||||||
|
return function(response) {
|
||||||
|
cb(createFileEntry(response.file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getCookieHeader(url) {
|
function getCookieHeader(url) {
|
||||||
return { Cookie: cookieHandler.getCookieString(url) };
|
return { Cookie: cookieHandler.getCookieString(url) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleMissingCallbacks(successFn, failFn) {
|
||||||
|
if (Object.prototype.toString.call(successFn) !== '[object Function]') {
|
||||||
|
throw new Error('advanced-http: missing mandatory "onSuccess" callback function');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.prototype.toString.call(failFn) !== '[object Function]') {
|
||||||
|
throw new Error('advanced-http: missing mandatory "onFail" callback function');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var http = {
|
var http = {
|
||||||
headers: {},
|
headers: {},
|
||||||
dataSerializer: 'urlencoded',
|
dataSerializer: 'urlencoded',
|
||||||
@@ -111,6 +143,9 @@ var http = {
|
|||||||
clearCookies: function () {
|
clearCookies: function () {
|
||||||
return cookieHandler.clearCookies();
|
return cookieHandler.clearCookies();
|
||||||
},
|
},
|
||||||
|
removeCookies: function (url, callback) {
|
||||||
|
cookieHandler.removeCookies(url, callback);
|
||||||
|
},
|
||||||
enableSSLPinning: function (enable, success, failure) {
|
enableSSLPinning: function (enable, success, failure) {
|
||||||
return exec(success, failure, 'CordovaHttpPlugin', 'enableSSLPinning', [enable]);
|
return exec(success, failure, 'CordovaHttpPlugin', 'enableSSLPinning', [enable]);
|
||||||
},
|
},
|
||||||
@@ -121,65 +156,95 @@ var http = {
|
|||||||
return exec(success, failure, 'CordovaHttpPlugin', 'validateDomainName', [validate]);
|
return exec(success, failure, 'CordovaHttpPlugin', 'validateDomainName', [validate]);
|
||||||
},
|
},
|
||||||
post: function (url, data, headers, success, failure) {
|
post: function (url, data, headers, success, failure) {
|
||||||
|
handleMissingCallbacks(success, failure);
|
||||||
|
|
||||||
data = data || {};
|
data = data || {};
|
||||||
headers = headers || {};
|
headers = headers || {};
|
||||||
headers = mergeHeaders(this.headers, headers);
|
headers = mergeHeaders(this.headers, headers);
|
||||||
headers = mergeHeaders(getCookieHeader(url), headers);
|
headers = mergeHeaders(getCookieHeader(url), headers);
|
||||||
|
|
||||||
return exec(getSuccessHandler(url, success), failure, 'CordovaHttpPlugin', 'post', [url, data, this.dataSerializer, headers]);
|
var onSuccess = injectCookieHandler(url, success);
|
||||||
|
var onFail = injectCookieHandler(url, failure);
|
||||||
|
|
||||||
|
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'post', [url, data, this.dataSerializer, headers]);
|
||||||
},
|
},
|
||||||
get: function (url, params, headers, success, failure) {
|
get: function (url, params, headers, success, failure) {
|
||||||
|
handleMissingCallbacks(success, failure);
|
||||||
|
|
||||||
params = params || {};
|
params = params || {};
|
||||||
headers = headers || {};
|
headers = headers || {};
|
||||||
headers = mergeHeaders(this.headers, headers);
|
headers = mergeHeaders(this.headers, headers);
|
||||||
headers = mergeHeaders(getCookieHeader(url), headers);
|
headers = mergeHeaders(getCookieHeader(url), headers);
|
||||||
|
|
||||||
return exec(getSuccessHandler(url, success), failure, 'CordovaHttpPlugin', 'get', [url, params, headers]);
|
var onSuccess = injectCookieHandler(url, success);
|
||||||
|
var onFail = injectCookieHandler(url, failure);
|
||||||
|
|
||||||
|
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'get', [url, params, headers]);
|
||||||
},
|
},
|
||||||
put: function (url, data, headers, success, failure) {
|
put: function (url, data, headers, success, failure) {
|
||||||
|
handleMissingCallbacks(success, failure);
|
||||||
|
|
||||||
data = data || {};
|
data = data || {};
|
||||||
headers = headers || {};
|
headers = headers || {};
|
||||||
headers = mergeHeaders(this.headers, headers);
|
headers = mergeHeaders(this.headers, headers);
|
||||||
headers = mergeHeaders(getCookieHeader(url), headers);
|
headers = mergeHeaders(getCookieHeader(url), headers);
|
||||||
|
|
||||||
return exec(getSuccessHandler(url, success), failure, 'CordovaHttpPlugin', 'put', [url, data, this.dataSerializer, headers]);
|
var onSuccess = injectCookieHandler(url, success);
|
||||||
|
var onFail = injectCookieHandler(url, failure);
|
||||||
|
|
||||||
|
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, this.dataSerializer, headers]);
|
||||||
},
|
},
|
||||||
delete: function (url, params, headers, success, failure) {
|
delete: function (url, params, headers, success, failure) {
|
||||||
|
handleMissingCallbacks(success, failure);
|
||||||
|
|
||||||
params = params || {};
|
params = params || {};
|
||||||
headers = headers || {};
|
headers = headers || {};
|
||||||
headers = mergeHeaders(this.headers, headers);
|
headers = mergeHeaders(this.headers, headers);
|
||||||
headers = mergeHeaders(getCookieHeader(url), headers);
|
headers = mergeHeaders(getCookieHeader(url), headers);
|
||||||
|
|
||||||
return exec(getSuccessHandler(url, success), failure, 'CordovaHttpPlugin', 'delete', [url, params, headers]);
|
var onSuccess = injectCookieHandler(url, success);
|
||||||
|
var onFail = injectCookieHandler(url, failure);
|
||||||
|
|
||||||
|
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'delete', [url, params, headers]);
|
||||||
},
|
},
|
||||||
head: function (url, params, headers, success, failure) {
|
head: function (url, params, headers, success, failure) {
|
||||||
|
handleMissingCallbacks(success, failure);
|
||||||
|
|
||||||
|
params = params || {};
|
||||||
|
headers = headers || {};
|
||||||
headers = mergeHeaders(this.headers, headers);
|
headers = mergeHeaders(this.headers, headers);
|
||||||
headers = mergeHeaders(getCookieHeader(url), headers);
|
headers = mergeHeaders(getCookieHeader(url), headers);
|
||||||
|
|
||||||
return exec(getSuccessHandler(url, success), failure, 'CordovaHttpPlugin', 'head', [url, params, headers]);
|
var onSuccess = injectCookieHandler(url, success);
|
||||||
|
var onFail = injectCookieHandler(url, failure);
|
||||||
|
|
||||||
|
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'head', [url, params, headers]);
|
||||||
},
|
},
|
||||||
uploadFile: function (url, params, headers, filePath, name, success, failure) {
|
uploadFile: function (url, params, headers, filePath, name, success, failure) {
|
||||||
|
handleMissingCallbacks(success, failure);
|
||||||
|
|
||||||
|
params = params || {};
|
||||||
|
headers = headers || {};
|
||||||
headers = mergeHeaders(this.headers, headers);
|
headers = mergeHeaders(this.headers, headers);
|
||||||
headers = mergeHeaders(getCookieHeader(url), headers);
|
headers = mergeHeaders(getCookieHeader(url), headers);
|
||||||
|
|
||||||
return exec(getSuccessHandler(url, success), failure, 'CordovaHttpPlugin', 'uploadFile', [url, params, headers, filePath, name]);
|
var onSuccess = injectCookieHandler(url, success);
|
||||||
|
var onFail = injectCookieHandler(url, failure);
|
||||||
|
|
||||||
|
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFile', [url, params, headers, filePath, name]);
|
||||||
},
|
},
|
||||||
downloadFile: function (url, params, headers, filePath, success, failure) {
|
downloadFile: function (url, params, headers, filePath, success, failure) {
|
||||||
|
handleMissingCallbacks(success, failure);
|
||||||
|
|
||||||
|
params = params || {};
|
||||||
|
headers = headers || {};
|
||||||
headers = mergeHeaders(this.headers, headers);
|
headers = mergeHeaders(this.headers, headers);
|
||||||
headers = mergeHeaders(getCookieHeader(url), headers);
|
headers = mergeHeaders(getCookieHeader(url), headers);
|
||||||
|
|
||||||
var win = function (result) {
|
var onSuccess = injectCookieHandler(url, injectFileEntryHandler(success));
|
||||||
var entry = new (require('cordova-plugin-file.FileEntry'))();
|
var onFail = injectCookieHandler(url, failure);
|
||||||
entry.isDirectory = false;
|
|
||||||
entry.isFile = true;
|
|
||||||
entry.name = result.file.name;
|
|
||||||
entry.fullPath = result.file.fullPath;
|
|
||||||
entry.filesystem = new FileSystem(result.file.filesystemName || (result.file.filesystem == window.PERSISTENT ? 'persistent' : 'temporary'));
|
|
||||||
entry.nativeURL = result.file.nativeURL;
|
|
||||||
success(entry);
|
|
||||||
};
|
|
||||||
|
|
||||||
return exec(win, failure, 'CordovaHttpPlugin', 'downloadFile', [url, params, headers, filePath]);
|
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, params, headers, filePath]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Vendored
+3
@@ -47,6 +47,9 @@ function registerService(http) {
|
|||||||
clearCookies: function () {
|
clearCookies: function () {
|
||||||
return http.clearCookies();
|
return http.clearCookies();
|
||||||
},
|
},
|
||||||
|
removeCookies: function (url) {
|
||||||
|
return http.removeCookies(url);
|
||||||
|
},
|
||||||
enableSSLPinning: function (enable) {
|
enableSSLPinning: function (enable) {
|
||||||
return makePromise(http.enableSSLPinning, [enable]);
|
return makePromise(http.enableSSLPinning, [enable]);
|
||||||
},
|
},
|
||||||
|
|||||||
+14
-1
@@ -11,7 +11,8 @@ var cookieJar = new ToughCookie.CookieJar(store);
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
setCookieFromString: setCookieFromString,
|
setCookieFromString: setCookieFromString,
|
||||||
getCookieString: getCookieString,
|
getCookieString: getCookieString,
|
||||||
clearCookies: clearCookies
|
clearCookies: clearCookies,
|
||||||
|
removeCookies: removeCookies
|
||||||
}
|
}
|
||||||
|
|
||||||
function splitCookieString(cookieStr) {
|
function splitCookieString(cookieStr) {
|
||||||
@@ -51,3 +52,15 @@ function getCookieString(url) {
|
|||||||
function clearCookies() {
|
function clearCookies() {
|
||||||
window.localStorage.removeItem(storeKey);
|
window.localStorage.removeItem(storeKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function removeCookies(url, cb) {
|
||||||
|
cookieJar.getCookies(url, function(error, cookies) {
|
||||||
|
if (!cookies || cookies.length === 0) {
|
||||||
|
return cb(null, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
var domain = cookies[0].domain;
|
||||||
|
|
||||||
|
cookieJar.store.removeCookies(domain, null, cb);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user