1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConfigs, errorCodes, ponyfills) {
var publicInterface = {
getBasicAuthHeader: getBasicAuthHeader,
useBasicAuth: useBasicAuth,
getHeaders: getHeaders,
setHeader: setHeader,
getDataSerializer: getDataSerializer,
setDataSerializer: setDataSerializer,
setCookie: setCookie,
clearCookies: clearCookies,
removeCookies: removeCookies,
getCookieString: getCookieString,
getRequestTimeout: getRequestTimeout,
setRequestTimeout: setRequestTimeout,
getFollowRedirect: getFollowRedirect,
setFollowRedirect: setFollowRedirect,
setServerTrustMode: setServerTrustMode,
setClientAuthMode: setClientAuthMode,
sendRequest: sendRequest,
post: post,
put: put,
patch: patch,
get: get,
delete: del,
head: head,
options: options,
uploadFile: uploadFile,
downloadFile: downloadFile,
ErrorCode: errorCodes,
ponyfills: ponyfills
};
function getBasicAuthHeader(username, password) {
return { 'Authorization': 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password) };
}
function useBasicAuth(username, password) {
this.setHeader('*', 'Authorization', 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password));
}
function getHeaders(host) {
return globalConfigs.headers[host || '*'] || null;
}
function setHeader() {
// this one is for being backward compatible
var host = '*';
var header = arguments[0];
var value = arguments[1];
if (arguments.length === 3) {
host = arguments[0];
header = arguments[1];
value = arguments[2];
}
helpers.checkForBlacklistedHeaderKey(header);
helpers.checkForInvalidHeaderValue(value);
globalConfigs.headers[host] = globalConfigs.headers[host] || {};
if (value === null) {
delete globalConfigs.headers[host][header];
} else {
globalConfigs.headers[host][header] = value;
}
}
function getDataSerializer() {
return globalConfigs.serializer;
}
function setDataSerializer(serializer) {
globalConfigs.serializer = helpers.checkSerializer(serializer);
}
function setCookie(url, cookie, options) {
cookieHandler.setCookie(url, cookie, options);
}
function clearCookies() {
cookieHandler.clearCookies();
}
function removeCookies(url, callback) {
cookieHandler.removeCookies(url, callback);
}
function getCookieString(url) {
return cookieHandler.getCookieString(url);
}
function getRequestTimeout() {
return globalConfigs.timeout;
}
function setRequestTimeout(timeout) {
globalConfigs.timeout = helpers.checkTimeoutValue(timeout);
}
function getFollowRedirect() {
return globalConfigs.followRedirect;
}
function setFollowRedirect(follow) {
globalConfigs.followRedirect = helpers.checkFollowRedirectValue(follow);
}
function setServerTrustMode(mode, success, failure) {
helpers.handleMissingCallbacks(success, failure);
return exec(success, failure, 'CordovaHttpPlugin', 'setServerTrustMode', [helpers.checkSSLCertMode(mode)]);
}
function setClientAuthMode() {
var mode = arguments[0];
var options = null;
var success = arguments[1];
var failure = arguments[2];
if (arguments.length === 4) {
options = arguments[1];
success = arguments[2];
failure = arguments[3];
}
mode = helpers.checkClientAuthMode(mode);
options = helpers.checkClientAuthOptions(mode, options);
helpers.handleMissingCallbacks(success, failure);
return exec(success, failure, 'CordovaHttpPlugin', 'setClientAuthMode', [mode, options.alias, options.rawPkcs, options.pkcsPassword]);
}
function sendRequest(url, options, success, failure) {
helpers.handleMissingCallbacks(success, failure);
options = helpers.handleMissingOptions(options, globalConfigs);
url = urlUtil.appendQueryParamsString(url, urlUtil.serializeQueryParams(options.params, true));
var headers = helpers.getMergedHeaders(url, options.headers, globalConfigs.headers);
var onFail = helpers.injectCookieHandler(url, failure);
var onSuccess = helpers.injectCookieHandler(url, helpers.injectRawResponseHandler(options.responseType, success, failure));
switch (options.method) {
case 'post':
case 'put':
case 'patch':
return helpers.processData(options.data, options.serializer, function (data) {
exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, data, options.serializer, headers, options.timeout, options.followRedirect, options.responseType]);
});
case 'upload':
var fileOptions = helpers.checkUploadFileOptions(options.filePath, options.name);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFiles', [url, headers, fileOptions.filePaths, fileOptions.names, options.timeout, options.followRedirect, options.responseType]);
case 'download':
var filePath = helpers.checkDownloadFilePath(options.filePath);
var onDownloadSuccess = helpers.injectCookieHandler(url, helpers.injectFileEntryHandler(success));
return exec(onDownloadSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, headers, filePath, options.timeout, options.followRedirect]);
default:
return exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, headers, options.timeout, options.followRedirect, options.responseType]);
}
}
function post(url, data, headers, success, failure) {
return publicInterface.sendRequest(url, { method: 'post', data: data, headers: headers }, success, failure);
};
function put(url, data, headers, success, failure) {
return publicInterface.sendRequest(url, { method: 'put', data: data, headers: headers }, success, failure);
}
function patch(url, data, headers, success, failure) {
return publicInterface.sendRequest(url, { method: 'patch', data: data, headers: headers }, success, failure);
}
function get(url, params, headers, success, failure) {
return publicInterface.sendRequest(url, { method: 'get', params: params, headers: headers }, success, failure);
};
function del(url, params, headers, success, failure) {
return publicInterface.sendRequest(url, { method: 'delete', params: params, headers: headers }, success, failure);
}
function head(url, params, headers, success, failure) {
return publicInterface.sendRequest(url, { method: 'head', params: params, headers: headers }, success, failure);
}
function options(url, params, headers, success, failure) {
return publicInterface.sendRequest(url, { method: 'options', params: params, headers: headers }, success, failure);
};
function uploadFile(url, params, headers, filePath, name, success, failure) {
return publicInterface.sendRequest(url, { method: 'upload', params: params, headers: headers, filePath: filePath, name: name }, success, failure);
}
function downloadFile(url, params, headers, filePath, success, failure) {
return publicInterface.sendRequest(url, { method: 'download', params: params, headers: headers, filePath: filePath }, success, failure);
}
return publicInterface;
}
|