This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
XHRHelper.cs
356 lines (324 loc) · 13.9 KB
/
XHRHelper.cs
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Licensed 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.
*/
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Windows;
namespace WPCordovaClassLib.CordovaLib
{
public class XHRHelper : IBrowserDecorator
{
public WebBrowser Browser { get; set; }
public PhoneApplicationPage Page { get; set; }
public void InjectScript()
{
string script = @"(function(win, doc) {
if (!win.__XHRShimAliases) {
win.__XHRShimAliases = {};
}
else {
return; // already initialized, this happens when navigating to #
}
win.__onXHRLocalCallback = function (responseCode, responseText, reqId) {
if (win.__XHRShimAliases[reqId]){
var alias = win.__XHRShimAliases[reqId];
if (alias){
delete win.__XHRShimAliases[reqId];
if (responseCode == '200'){
alias.onResult && alias.onResult(responseText);
Object.defineProperty(alias, 'responseXML', {
get: function () {
return new DOMParser().parseFromString(this.responseText, 'text/xml');
}
});
} else {
alias.onError && alias.onError(responseText);
}
}
}
};
var docDomain = null;
try {
docDomain = doc.domain;
} catch (err) {}
if (!docDomain || docDomain.length === 0) {
var aliasXHR = win.XMLHttpRequest;
var XHRShim = function() {};
win.XMLHttpRequest = XHRShim;
XHRShim.noConflict = aliasXHR;
XHRShim.UNSENT = 0;
XHRShim.OPENED = 1;
XHRShim.HEADERS_RECEIVED = 2;
XHRShim.LOADING = 3;
XHRShim.DONE = 4;
XHRShim.prototype = {
isAsync: false,
onreadystatechange: null,
readyState: 0,
_url: '',
timeout: 0,
withCredentials: false,
_requestHeaders: null,
open: function (reqType, uri, isAsync, user, password) {
if (uri && uri.indexOf('http') === 0) {
if (!this.wrappedXHR) {
this.wrappedXHR = new aliasXHR();
var self = this;
if (this.timeout > 0) {
this.wrappedXHR.timeout = this.timeout;
}
Object.defineProperty(this, 'timeout', {
set: function(val) {
this.wrappedXHR.timeout = val;
},
get: function() {
return this.wrappedXHR.timeout;
}
});
if (this.withCredentials) {
this.wrappedXHR.withCredentials = this.withCredentials;
}
Object.defineProperty(this, 'withCredentials', {
set: function(val) {
this.wrappedXHR.withCredentials = val;
},
get: function() {
return this.wrappedXHR.withCredentials;
}
});
Object.defineProperty(this, 'status', {
get: function() {
return this.wrappedXHR.status;
}
});
Object.defineProperty(this, 'responseText', {
get: function() {
return this.wrappedXHR.responseText;
}
});
Object.defineProperty(this, 'statusText', {
get: function() {
return this.wrappedXHR.statusText;
}
});
Object.defineProperty(this, 'responseXML', {
get: function() {
return this.wrappedXHR.responseXML;
}
});
Object.defineProperty(this, 'response', {
get: function() {
return this.wrappedXHR.response;
}
});
Object.defineProperty(this, 'responseType', {
set: function(val) {
return this.wrappedXHR.responseType = val;
}
});
this.getResponseHeader = function(header) {
return this.wrappedXHR.getResponseHeader(header);
};
this.getAllResponseHeaders = function() {
return this.wrappedXHR.getAllResponseHeaders();
};
this.wrappedXHR.onreadystatechange = function() {
self.changeReadyState(self.wrappedXHR.readyState);
};
}
return this.wrappedXHR.open(reqType, uri, isAsync, user, password);
}
else
{
this.isAsync = isAsync;
this.reqType = reqType;
this._url = uri;
}
},
statusText: '',
changeReadyState: function(newState) {
this.readyState = newState;
if (this.onreadystatechange) {
// mimic simple 'readystatechange' event which should be passed as per spec
var evt = {type: 'readystatechange', target: this, timeStamp: new Date().getTime()};
this.onreadystatechange(evt);
}
if (this.readyState == XHRShim.DONE){
this.onload && this.onload();
}
},
addEventListener: function (type, listener, useCapture){
if (this.wrappedXHR) {
this.wrappedXHR.addEventListener(type, listener, useCapture);
} else {
this['on' + type] = listener;
}
},
removeEventListener: function (type, listener, useCapture){
if (this.wrappedXHR) {
this.wrappedXHR.removeEventListener(type, listener, useCapture);
} else {
if (this['on' + type] == listener) { // if listener is currently used
delete this['on' + type];
}
}
},
setRequestHeader: function(header, value) {
if (this.wrappedXHR) {
this.wrappedXHR.setRequestHeader(header, value);
}
},
getResponseHeader: function(header) {
return this.wrappedXHR ? this.wrappedXHR.getResponseHeader(header) : '';
},
getAllResponseHeaders: function() {
return this.wrappedXHR ? this.wrappedXHR.getAllResponseHeaders() : '';
},
overrideMimeType: function(mimetype) {
return this.wrappedXHR ? this.wrappedXHR.overrideMimeType(mimetype) : '';
},
responseText: '',
responseXML: '',
onResult: function(res) {
this.status = 200;
if (typeof res == 'object') {
res = JSON.stringify(res);
}
this.responseText = res;
this.responseXML = res;
this.changeReadyState(XHRShim.DONE);
},
onError: function(err) {
this.status = 404;
this.changeReadyState(XHRShim.DONE);
},
abort: function() {
if (this.wrappedXHR) {
return this.wrappedXHR.abort();
}
},
send: function(data) {
if (this.wrappedXHR) {
return this.wrappedXHR.send(data);
}
else {
this.changeReadyState(XHRShim.OPENED);
var alias = this;
var root = window.location.href.split('#')[0]; // remove hash
var rootPath = root.substr(0,root.lastIndexOf('/')) + '/';
// Removing unwanted slashes after x-wmapp0 from the basePath, URI cannot process x-wmapp0: /www or //www
var basePath = rootPath.replace(/:\/+/gi, ':');
//console.log( 'Stripping protocol if present and removing leading / characters' );
var resolvedUrl =
// remove protocol from the beginning of the url if present
( this._url.indexOf( window.location.protocol ) === 0 ?
this._url.substring( window.location.protocol.length ) :
this._url )
// get rid of all the starting slashes
.replace(/^[/]*/, '')
.split('#')[0]; // remove hash
var wwwFolderPath = navigator.userAgent.indexOf('MSIE 9.0') > -1 ? 'app/www/' : 'www/';
// handle special case where url is of form app/www but we are loaded just from /www
if( resolvedUrl.indexOf('app/www') == 0 ) {
resolvedUrl = window.location.protocol + wwwFolderPath + resolvedUrl.substr(7);
}
else if( resolvedUrl.indexOf('www') == 0) {
resolvedUrl = window.location.protocol + wwwFolderPath + resolvedUrl.substr(4);
}
if(resolvedUrl.indexOf(':') < 0) {
resolvedUrl = basePath + resolvedUrl; // consider it relative
}
// Generate unique request ID
var reqId = new Date().getTime().toString() + Math.random();
var funk = function () {
__XHRShimAliases[reqId] = alias;
alias.changeReadyState(XHRShim.LOADING);
window.external.Notify('XHRLOCAL/' + reqId + '/' + resolvedUrl);
};
this.isAsync ? setTimeout(funk, 0) : funk();
}
},
status: 404
};
}
})(window, document); ";
Browser.InvokeScript("eval", new string[] { script });
}
public bool HandleCommand(string commandStr)
{
if (commandStr.IndexOf("XHRLOCAL") == 0)
{
var reqStr = commandStr.Replace("XHRLOCAL/", "").Split(new char[] {'/'}, 2);
string reqId = reqStr[0];
string url = reqStr[1];
Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
try
{
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoFile.FileExists(uri.AbsolutePath))
{
using (TextReader reader = new StreamReader(isoFile.OpenFile(uri.AbsolutePath, FileMode.Open, FileAccess.Read)))
{
string text = reader.ReadToEnd();
Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text, reqId });
return true;
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine("ERROR: Exception in HandleCommand: " + ex);
}
Uri relUri = new Uri(uri.AbsolutePath, UriKind.Relative);
var resource = Application.GetResourceStream(relUri);
try
{
if (resource == null)
{
// 404 ?
Browser.InvokeScript("__onXHRLocalCallback", new string[] { "404", string.Empty, reqId });
return true;
}
else
{
using (StreamReader streamReader = new StreamReader(resource.Stream))
{
string text = streamReader.ReadToEnd();
Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text, reqId });
return true;
}
}
}
catch (Exception ex)
{
Debug.WriteLine("ERROR: Exception in HandleCommand: " + ex);
}
}
return false;
}
public void AttachNativeHandlers()
{
// nothing todo
}
public void DetachNativeHandlers()
{
// nothing to do
}
}
}