-
Notifications
You must be signed in to change notification settings - Fork 4
/
titanium.js
184 lines (149 loc) · 4.37 KB
/
titanium.js
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
;(function () {
if (global === this) {
global.window = global;
global.XMLHttpRequest = function XMLHttpRequest() {
var xhr = Ti.Network.createHTTPClient();
if (typeof xhr.getResponseHeaders !== 'undefined') {
xhr.getAllResponseHeaders = getAllResponseHeaders;
}
function getAllResponseHeaders() {
var headers = xhr.getResponseHeaders();
return Object.keys(headers).map(function (header) {
return header + ':' + headers[header];
}).join('\n') + '\n';
}
return xhr;
};
}
})();
var request = require('superagent');
function getXHR() {
return new XMLHttpRequest();
}
request.Request.prototype.end = function (fn) {
var self = this;
var xhr = this.xhr = getXHR();
var query = this._query.join('&');
var timeout = this._timeout;
var redirects = this._redirects;
var data = this._data;
var validatesSecureCertificate = this._validatesSecureCertificate;
// store callback
this._callback = fn || noop;
// state change
xhr.onreadystatechange = function(){
if (4 != xhr.readyState) return;
if (0 == xhr.status) {
if (self.aborted) return self.timeoutError();
return;
}
if (self._finished) return;
self._finished = true;
self.emit('end');
};
xhr.onload = function () {
if (self._finished) return;
self._finished = true;
self.emit('end');
};
xhr.onerror = function (error) {
if (xhr.status > 0) {
if (self._finished) return;
self._finished = true;
self.emit('end');
}
else {
self.genericError(error);
}
};
// timeout
if (timeout) {
xhr.timeout = timeout;
}
if (redirects != null) {
this.redirects(redirects);
}
if (validatesSecureCertificate != null) {
xhr.validatesSecureCertificate = this._validatesSecureCertificate
}
// querystring
if (query) {
query = request.serializeObject(query);
this.url += ~this.url.indexOf('?')
? '&' + query
: '?' + query;
}
// initiate request
xhr.open(this.method, this.url, true);
// CORS
if (this._withCredentials) xhr.withCredentials = true;
// body
if ('GET' != this.method && 'HEAD' != this.method && 'string' != typeof data && !isHost(data)) {
// serialize stuff
var serialize = request.serialize[this.getHeader('Content-Type')];
if (serialize) data = serialize(data);
}
// set header fields
for (var field in this.header) {
if (null == this.header[field]) continue;
xhr.setRequestHeader(field, this.header[field]);
}
// send stuff
this.emit('request', this);
xhr.send(data);
return this;
};
request.Request.prototype.genericError = function(error) {
var err = new Error(error.error || 'Unknown error');
for (var k in error) {
if (k === 'source') continue;
err[k] = error[k];
}
if (/timed?\s*out/i.test(err + '')) {
err.timeout = this._timeout;
}
this.callback(err);
};
request.Request.prototype.timeout = function (timeout) {
this._timeout = timeout;
if (this.xhr) throw new Error("Cannot set the timeout once the HTTPClient has been created");
return this;
};
request.Request.prototype.redirects = function (redirects) {
this._redirects = redirects;
if (redirects > 0 || redirects === true) {
if (this.xhr) this.xhr.autoRedirect = true;
}
else if (redirects <= 0 || redirects === false) {
if (this.xhr) this.xhr.autoRedirect = false;
}
return this;
};
request.Request.prototype.validatesSecureCertificate = function (validates) {
this._validatesSecureCertificate = validates;
return this;
};
// Not necessary in Titanium SDK 3.4, HTTPClient#getResponseHeader is (as the
// standard states) case insensitive.
// See https://jira.appcelerator.org/browse/TIMOB-17585 for reference,
// and https://github.com/appcelerator/titanium_mobile/pull/6053
var originalSetHeaderProperties = request.Response.prototype.setHeaderProperties;
request.Response.prototype.setHeaderProperties = function (header) {
this.header['content-type'] =
this.xhr.getResponseHeader('content-type') ||
this.xhr.getResponseHeader('Content-Type');
return originalSetHeaderProperties.call(this, header);
};
function isHost(obj) {
var str = {}.toString.call(obj);
switch (str) {
case '[object File]':
case '[object Blob]':
case '[object FormData]':
return true;
default:
return false;
}
}
function noop() {}
module.exports = request;