-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathajax_request.js
518 lines (441 loc) · 13.5 KB
/
ajax_request.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/**
* @license
* Copyright 2015 Google Inc.
*
* 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.
*/
goog.provide('shaka.util.AjaxRequest');
goog.require('goog.Uri');
goog.require('shaka.asserts');
goog.require('shaka.features');
goog.require('shaka.util.Clock');
goog.require('shaka.util.ContentDatabaseReader');
goog.require('shaka.util.IBandwidthEstimator');
goog.require('shaka.util.PublicPromise');
goog.require('shaka.util.StringUtils');
goog.require('shaka.util.TypedBind');
goog.require('shaka.util.Uint8ArrayUtils');
/**
* Creates an asynchronous HTTP request object which manages retries
* automatically.
*
* @param {string} url The URL to request.
* @param {shaka.util.AjaxRequest.Parameters=} opt_parameters
*
* @struct
* @constructor
*/
shaka.util.AjaxRequest = function(url, opt_parameters) {
/**
* The request URL.
* @protected {string}
*/
this.url = url;
/**
* A collection of parameters which an instance of a subclass may wish to
* override.
* @protected {!shaka.util.AjaxRequest.Parameters}
*/
this.parameters = opt_parameters ||
new shaka.util.AjaxRequest.Parameters();
/**
* The number of times the request has been attempted.
* @private {number}
*/
this.attempts_ = 0;
/**
* A timestamp in milliseconds when the request began.
* @private {number}
*/
this.startTime_ = 0;
/**
* The delay, in milliseconds, before the next retry.
* @private {number}
*/
this.retryDelayMs_ = 0;
/**
* The last used delay. This is used in unit tests only.
* @private {number}
*/
this.lastDelayMs_ = 0;
/** @private {XMLHttpRequest} */
this.xhr_ = null;
/**
* Resolved when the request is completed successfully.
* Rejected if it cannot be completed.
* @private {shaka.util.PublicPromise.<!XMLHttpRequest>}
*/
this.promise_ = new shaka.util.PublicPromise();
/** @type {shaka.util.IBandwidthEstimator} */
this.estimator = null;
};
/**
* A collection of parameters which an instance of a subclass may wish to
* override.
*
* @struct
* @constructor
*/
shaka.util.AjaxRequest.Parameters = function() {
/**
* The request body, if desired.
* @type {(ArrayBuffer|?string)}
*/
this.body = null;
/**
* The maximum number of times the request should be attempted.
* @type {number}
*/
this.maxAttempts = 1;
/**
* The delay before the first retry, in milliseconds.
* @type {number}
*/
this.baseRetryDelayMs = 1000;
/**
* The multiplier for successive retry delays.
* @type {number}
*/
this.retryBackoffFactor = 2.0;
/**
* The maximum amount of fuzz to apply to each retry delay.
* For example, 0.5 means "between 50% below and 50% above the retry delay."
* @type {number}
*/
this.retryFuzzFactor = 0.5;
/**
* The request timeout, in milliseconds. Zero means "unlimited".
* @type {number}
*/
this.requestTimeoutMs = 0;
/**
* The HTTP request method, such as 'GET' or 'POST'.
* @type {string}
*/
this.method = 'GET';
/**
* The response type, corresponding to XMLHttpRequest.responseType.
* @type {string}
*/
this.responseType = 'arraybuffer';
/**
* HTTP request headers as key-value pairs.
* @type {!Object.<string, string>}
*/
this.requestHeaders = {};
/**
* Make requests with credentials. This will allow cookies in cross-site
* requests.
* @see http://goo.gl/YBRKPe
* @type {boolean}
*/
this.withCredentials = false;
/**
* Will attempt to read the server's Date header and synchronize the clock.
* @see shaka.util.Clock
* @type {boolean}
*/
this.synchronizeClock = false;
/**
* The content database used if the URI is a data URI. Required for data
* URIs.
*
* @type {shaka.util.ContentDatabaseReader}
*/
this.contentDatabase = null;
};
/**
* Destroys the AJAX request.
* This happens automatically after the internal promise is resolved or
* rejected.
*
* @private
*/
shaka.util.AjaxRequest.prototype.destroy_ = function() {
this.cleanupRequest_();
this.parameters.body = null;
this.promise_.destroy();
this.promise_ = null;
this.estimator = null;
};
/**
* Remove |xhr_|'s references to bound functions, and set |xhr_| to null.
*
* @private
*/
shaka.util.AjaxRequest.prototype.cleanupRequest_ = function() {
if (this.xhr_) {
this.xhr_.onload = null;
this.xhr_.onreadystatechange = null;
this.xhr_.onerror = null;
this.xhr_.ontimeout = null;
}
this.xhr_ = null;
};
/**
* Sends the request. Called by subclasses.
*
* @return {Promise.<!XMLHttpRequest>}
*/
shaka.util.AjaxRequest.prototype.send = function() {
shaka.asserts.assert(this.xhr_ == null);
if (this.xhr_) {
// The request is already in-progress, so there's nothing to do.
return this.promise_;
}
// We can't request from data or idb URIs, so handle it separately.
if (this.url.lastIndexOf('data:', 0) == 0) {
return this.handleDataUri_();
} else if (this.url.lastIndexOf('idb:', 0) == 0) {
shaka.asserts.assert(shaka.features.Offline);
return this.handleOfflineUri_();
}
this.attempts_++;
this.startTime_ = Date.now();
if (!this.retryDelayMs_) {
// First try. Lock in the retry delay.
this.retryDelayMs_ = this.parameters.baseRetryDelayMs;
}
this.xhr_ = new XMLHttpRequest();
var url = this.url;
if ((this.estimator && !this.estimator.supportsCaching()) ||
this.parameters.synchronizeClock) {
// We cannot detect that a response was cached after the fact, so add a
// cache-busting parameter to the request to avoid caching. There are
// other methods, but they do not work cross-origin without control over
// both client and server.
var modifiedUri = new goog.Uri(url);
modifiedUri.getQueryData().add('_', Date.now());
url = modifiedUri.toString();
}
this.xhr_.open(this.parameters.method, url, true);
this.xhr_.responseType = this.parameters.responseType;
this.xhr_.timeout = this.parameters.requestTimeoutMs;
this.xhr_.withCredentials = this.parameters.withCredentials;
this.xhr_.onload = this.onLoad_.bind(this);
if (this.parameters.synchronizeClock) {
this.xhr_.onreadystatechange = this.onReadyStateChange_.bind(this);
}
this.xhr_.onerror = this.onError_.bind(this);
this.xhr_.ontimeout = this.onTimeout_.bind(this);
for (var k in this.parameters.requestHeaders) {
this.xhr_.setRequestHeader(k, this.parameters.requestHeaders[k]);
}
this.xhr_.send(this.parameters.body);
return this.promise_;
};
/**
* Handles a data URI.
* This method does not modify |this|'s state.
*
* @return {!Promise}
*
* @private
*/
shaka.util.AjaxRequest.prototype.handleDataUri_ = function() {
// Alias.
var StringUtils = shaka.util.StringUtils;
var Uint8ArrayUtils = shaka.util.Uint8ArrayUtils;
// Fake the data URI request.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs
var path = this.url.split(':')[1];
var optionalTypeAndRest = path.split(';');
var rest = optionalTypeAndRest.pop();
var optionalEncodingAndData = rest.split(',');
var data = optionalEncodingAndData.pop();
var optionalEncoding = optionalEncodingAndData.pop();
if (optionalEncoding == 'base64') {
data = StringUtils.fromBase64(data);
} else {
data = window.decodeURIComponent(data);
}
if (this.parameters.responseType == 'arraybuffer') {
data = Uint8ArrayUtils.fromString(data).buffer;
}
// We can't set the response field of an XHR, although we can make a
// hacky object that will still look like an XHR.
var xhr = /** @type {!XMLHttpRequest} */ (
JSON.parse(JSON.stringify(new XMLHttpRequest())));
xhr.response = data;
xhr.responseText = data.toString();
var promise = this.promise_;
promise.resolve(xhr);
this.destroy_();
return promise;
};
if (shaka.features.Offline) {
/**
* Handles an offline URI.
* This method does not modify |this|'s state.
*
* @return {!Promise}
*
* @private
*/
shaka.util.AjaxRequest.prototype.handleOfflineUri_ = function() {
// URL should have format idb://<streamId>/<segmentId>
var ids = this.url.split('/');
shaka.asserts.assert(ids.length == 4);
var streamId = parseInt(ids[2], 10);
var segmentId = parseInt(ids[3], 10);
shaka.asserts.assert(!isNaN(streamId));
shaka.asserts.assert(!isNaN(segmentId));
shaka.asserts.assert(this.parameters.contentDatabase);
return this.parameters.contentDatabase.retrieveSegment(streamId, segmentId)
.then(shaka.util.TypedBind(this,
/** @param {ArrayBuffer} data */
function(data) {
// We can't set the response field of an XHR, although we can make a
// hacky object that will still look like an XHR.
var xhr = /** @type {!XMLHttpRequest} */ (
JSON.parse(JSON.stringify(new XMLHttpRequest())));
xhr.response = data;
var promise = this.promise_;
promise.resolve(xhr);
this.destroy_();
return promise;
}))
.catch(shaka.util.TypedBind(this,
/** @param {*} e */
function(e) {
this.destroy_();
return Promise.reject(e);
}));
};
}
/**
* Creates an error object with necessary details about the request.
* @param {string} message The error message.
* @param {string} type The error type.
* @return {!Error}
* @private
*/
shaka.util.AjaxRequest.prototype.createError_ = function(message, type) {
var error = new Error(message);
error.type = type;
error.status = this.xhr_.status;
error.url = this.url;
error.method = this.parameters.method;
error.body = this.parameters.body;
error.xhr = this.xhr_;
return error;
};
/**
* Aborts an in-progress request.
* If a request is not in-progress then this function does nothing.
*/
shaka.util.AjaxRequest.prototype.abort = function() {
if (!this.xhr_ || this.xhr_.readyState == XMLHttpRequest.DONE) {
return;
}
shaka.asserts.assert(this.xhr_.readyState != 0);
this.xhr_.abort();
var error = this.createError_('Request aborted.', 'aborted');
this.promise_.reject(error);
this.destroy_();
};
/**
* Handles a "load" event.
*
* @param {!ProgressEvent} event The ProgressEvent from the request.
*
* @private
*/
shaka.util.AjaxRequest.prototype.onLoad_ = function(event) {
shaka.asserts.assert(event.target == this.xhr_);
if (this.estimator) {
this.estimator.sample(Date.now() - this.startTime_, event.loaded);
}
if (this.xhr_.status >= 200 && this.xhr_.status <= 299) {
// All 2xx HTTP codes are success cases.
this.promise_.resolve(this.xhr_);
this.destroy_();
} else if (this.attempts_ < this.parameters.maxAttempts) {
this.resendInternal_();
} else {
var error = this.createError_('HTTP error.', 'net');
this.promise_.reject(error);
this.destroy_();
}
};
/**
* Handles a "readystatechange" event.
*
* @private
*/
shaka.util.AjaxRequest.prototype.onReadyStateChange_ = function() {
if (this.xhr_.readyState != XMLHttpRequest.HEADERS_RECEIVED) {
return;
}
shaka.asserts.assert(this.parameters.synchronizeClock);
// This may not be available, depending on server configuration and CORS.
// For clock synchronization to work cross-origin, the server explicitly
// has to allow the client access to the "Date" header using the response
// header "Access-Control-Expose-Headers".
// It is also worth noting that this is not a fancy sync mechanism and
// does not account for round-trip times or latency. For our purposes,
// subsecond synchronization is not really necessary.
var date = Date.parse(this.xhr_.getResponseHeader('Date'));
if (date) {
shaka.util.Clock.sync(date);
}
};
/**
* Handles an "error" event.
*
* @param {!ProgressEvent} event The ProgressEvent from this.xhr_.
*
* @private
*/
shaka.util.AjaxRequest.prototype.onError_ = function(event) {
// Do not try again since an "error" event is usually unrecoverable.
shaka.asserts.assert(event.target == this.xhr_);
var error = this.createError_('Network failure.', 'net');
this.promise_.reject(error);
this.destroy_();
};
/**
* Handles a "timeout" event.
*
* @param {!ProgressEvent} event The ProgressEvent from this.xhr_.
*
* @private
*/
shaka.util.AjaxRequest.prototype.onTimeout_ = function(event) {
if (this.attempts_ < this.parameters.maxAttempts) {
this.resendInternal_();
} else {
var error = this.createError_('Request timed out.', 'net');
this.promise_.reject(error);
this.destroy_();
}
};
/**
* Resends request.
*
* @private
*/
shaka.util.AjaxRequest.prototype.resendInternal_ = function() {
this.cleanupRequest_();
var sendAgain = this.send.bind(this);
// Fuzz the delay to avoid tons of clients hitting the server at once
// after it recovers from whatever is causing it to fail.
var negToPosOne = (Math.random() * 2.0) - 1.0;
var negToPosFuzzFactor = negToPosOne * this.parameters.retryFuzzFactor;
var fuzzedDelay = this.retryDelayMs_ * (1.0 + negToPosFuzzFactor);
window.setTimeout(sendAgain, fuzzedDelay);
// Store the fuzzed delay to make testing retries feasible.
this.lastDelayMs_ = fuzzedDelay;
// Back off the next delay.
this.retryDelayMs_ *= this.parameters.retryBackoffFactor;
};