PageSourceSearch

https://web.alticemusicapp.com/plugins/cordova-plugin-advanced-http/src/browser/cordova-http-plugin.js

js alticemusicapp.com collected 2026-09-24 12:32:12 UTC 5,957 bytes, 222 lines download raw bytes

1cordova.define("cordova-plugin-advanced-http.http-proxy", function(require, exports, module) { var pluginId = module.id.slice(0, module.id.lastIndexOf('.'));
2
3var cordovaProxy = require('cordova/exec/proxy');
4var jsUtil = require(pluginId + '.js-util');
5
6function serializeJsonData(data) {
7  try {
8    return JSON.stringify(data);
9  } catch (err) {
10    return null;
11  }
12}
13
14function serializePrimitive(key, value) {
15  if (value === null || value === undefined) {
16    return encodeURIComponent(key) + '=';
17  }
18
19  return encodeURIComponent(key) + '=' + encodeURIComponent(value);
20}
21
22function serializeArray(key, values) {
23  return values.map(function(value) {
24    return encodeURIComponent(key) + '[]=' + encodeURIComponent(value);
25  }).join('&');
26}
27
28function serializeParams(params) {
29  if (params === null) return '';
30
31  return Object.keys(params).map(function(key) {
32    if (jsUtil.getTypeOf(params[key]) === 'Array') {
33      return serializeArray(key, params[key]);
34    }
35
36    return serializePrimitive(key, params[key]);
37  }).join('&');
38}
39
40function deserializeResponseHeaders(headers) {
41  var headerMap = {};
42  var arr = headers.trim().split(/[\r\n]+/);
43
44  arr.forEach(function (line) {
45    var parts = line.split(': ');
46    var header = parts.shift().toLowerCase();
47    var value = parts.join(': ');
48
49    headerMap[header] = value;
50  });
51
52  return headerMap;
53}
54
55function getResponseData(xhr) {
56  if (xhr.responseType !== 'text' || jsUtil.getTypeOf(xhr.responseText) !== 'String') {
57    return xhr.response;
58  }
59
60  return xhr.responseText;
61}
62
63function createXhrSuccessObject(xhr) {
64  return {
65    url: xhr.responseURL,
66    status: xhr.status,
67    data: getResponseData(xhr),
68    headers: deserializeResponseHeaders(xhr.getAllResponseHeaders())
69  };
70}
71
72function createXhrFailureObject(xhr) {
73  var obj = {};
74
75  obj.headers = xhr.getAllResponseHeaders();
76  obj.error = getResponseData(xhr);
77  obj.error = obj.error || 'advanced-http: please check browser console for error messages';
78
79  if (xhr.responseURL) obj.url = xhr.responseURL;
80  if (xhr.status) obj.status = xhr.status;
81
82  return obj;
83}
84
85function getHeaderValue(headers, headerName) {
86  let result = null;
87
88  Object.keys(headers).forEach(function(key) {
89    if (key.toLowerCase() === headerName.toLowerCase()) {
90      result = headers[key];
91    }
92  });
93
94  return result;
95}
96
97function setDefaultContentType(headers, contentType) {
98  if (getHeaderValue(headers, 'Content-Type') === null) {
99    headers['Content-Type'] = contentType;
100  }
101}
102
103function setHeaders(xhr, headers) {
104  Object.keys(headers).forEach(function(key) {
105    if (key.toLowerCase() === 'cookie') return;
106
107    xhr.setRequestHeader(key, headers[key]);
108  });
109}
110
111function sendRequest(method, withData, opts, success, failure) {
112  var data, serializer, headers, timeout, followRedirect, responseType;
113  var url = opts[0];
114
115  if (withData) {
116    data = opts[1];
117    serializer = opts[2];
118    headers = opts[3];
119    timeout = opts[4];
120    followRedirect = opts[5];
121    responseType = opts[6];
122  } else {
123    headers = opts[1];
124    timeout = opts[2];
125    followRedirect = opts[3];
126    responseType = opts[4];
127
128  }
129
130  var processedData = null;
131  var xhr = new XMLHttpRequest();
132
133  xhr.open(method, url);
134
135  if (headers.Cookie && headers.Cookie.length > 0) {
136    return failure('advanced-http: custom cookies not supported on browser platform');
137  }
138
139  if (!followRedirect) {
140    return failure('advanced-http: disabling follow redirect not supported on browser platform');
141  }
142
143  switch (serializer) {
144    case 'json':
145      setDefaultContentType(headers, 'application/json; charset=utf8');
146      processedData = serializeJsonData(data);
147
148      if (processedData === null) {
149        return failure('advanced-http: failed serializing data');
150      }
151
152      break;
153
154    case 'utf8':
155      setDefaultContentType(headers, 'text/plain; charset=utf8');
156      processedData = data.text;
157      break;
158
159    case 'urlencoded':
160      setDefaultContentType(headers, 'application/x-www-form-urlencoded');
161      processedData = serializeParams(data);
162      break;
163  }
164
165  xhr.timeout = timeout * 1000;
166  xhr.responseType = responseType;
167  setHeaders(xhr, headers);
168
169  xhr.onerror = xhr.ontimeout = function () {
170    return failure(createXhrFailureObject(xhr));
171  };
172
173  xhr.onload = function () {
174    if (xhr.readyState !== xhr.DONE) return;
175
176    if (xhr.status < 200 || xhr.status > 299) {
177      return failure(createXhrFailureObject(xhr));
178    }
179
180    return success(createXhrSuccessObject(xhr));
181  };
182
183  xhr.send(processedData);
184}
185
186var browserInterface = {
187  get: function (success, failure, opts) {
188    return sendRequest('get', false, opts, success, failure);
189  },
190  head: function (success, failure, opts) {
191    return sendRequest('head', false, opts, success, failure);
192  },
193  delete: function (success, failure, opts) {
194    return sendRequest('delete', false, opts, success, failure);
195  },
196  post: function (success, failure, opts) {
197    return sendRequest('post', true, opts, success, failure);
198  },
199  put: function (success, failure, opts) {
200    return sendRequest('put', true, opts, success, failure);
201  },
202  patch: function (success, failure, opts) {
203    return sendRequest('patch', true, opts, success, failure);
204  },
205  uploadFile: function (success, failure, opts) {
206    return failure('advanced-http: function "uploadFile" not supported on browser platform');
207  },
208  downloadFile: function (success, failure, opts) {
209    return failure('advanced-http: function "downloadFile" not supported on browser platform');
210  },
211  setServerTrustMode: function (success, failure, opts) {
212    return failure('advanced-http: function "setServerTrustMode" not supported on browser platform');
213  },
214  setClientAuthMode: function (success, failure, opts) {
215    return failure('advanced-http: function "setClientAuthMode" not supported on browser platform');
216  }
217};
218
219module.exports = browserInterface;
220cordovaProxy.add('CordovaHttpPlugin', browserInterface);
221
222});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.