summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/crd/js/xhr.js
blob: cdefb0aa4d81d45d2dee8763a2d4f19c3404c651 (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview
 * Utility class for making XHRs more pleasant.
 */

'use strict';

/** @suppress {duplicate} */
var remoting = remoting || {};

/**
 * @constructor
 * @param {remoting.Xhr.Params} params
 */
remoting.Xhr = function(params) {
  /** @private @const {!XMLHttpRequest} */
  this.nativeXhr_ = new XMLHttpRequest();
  this.nativeXhr_.onreadystatechange = this.onReadyStateChange_.bind(this);
  this.nativeXhr_.withCredentials = params.withCredentials || false;

  /** @private @const */
  this.responseType_ = params.responseType || remoting.Xhr.ResponseType.TEXT;

  // Apply URL parameters.
  var url = params.url;
  var parameterString = '';
  if (typeof(params.urlParams) === 'string') {
    parameterString = params.urlParams;
  } else if (typeof(params.urlParams) === 'object') {
    parameterString = remoting.Xhr.urlencodeParamHash(
        remoting.Xhr.removeNullFields_(params.urlParams));
  }
  if (parameterString) {
    base.debug.assert(url.indexOf('?') == -1);
    url += '?' + parameterString;
  }

  // Check that the content spec is consistent.
  if ((Number(params.textContent !== undefined) +
       Number(params.formContent !== undefined) +
       Number(params.jsonContent !== undefined)) > 1) {
    throw new Error(
        'may only specify one of textContent, formContent, and jsonContent');
  }

  // Prepare the build modified headers.
  var headers = remoting.Xhr.removeNullFields_(params.headers);

  // Convert the content fields to a single text content variable.
  /** @private {?string} */
  this.content_ = null;
  if (params.textContent !== undefined) {
    this.content_ = params.textContent;
  } else if (params.formContent !== undefined) {
    if (!('Content-type' in headers)) {
      headers['Content-type'] = 'application/x-www-form-urlencoded';
    }
    this.content_ = remoting.Xhr.urlencodeParamHash(params.formContent);
  } else if (params.jsonContent !== undefined) {
    if (!('Content-type' in headers)) {
      headers['Content-type'] = 'application/json; charset=UTF-8';
    }
    this.content_ = JSON.stringify(params.jsonContent);
  }

  // Apply the oauthToken field.
  if (params.oauthToken !== undefined) {
    base.debug.assert(!('Authorization' in headers));
    headers['Authorization'] = 'Bearer ' + params.oauthToken;
  }

  this.nativeXhr_.open(params.method, url, true);
  for (var key in headers) {
    this.nativeXhr_.setRequestHeader(key, headers[key]);
  }

  /** @private {base.Deferred<!remoting.Xhr.Response>} */
  this.deferred_ = null;
};

/**
 * @enum {string}
 */
remoting.Xhr.ResponseType = {
  TEXT: 'TEXT',  // Request a plain text response (default).
  JSON: 'JSON',  // Request a JSON response.
  NONE: 'NONE'   // Don't request any response.
};

/**
 * Parameters for the 'start' function.
 *
 * method: The HTTP method to use.
 *
 * url: The URL to request.
 *
 * urlParams: (optional) Parameters to be appended to the URL.
 *     Null-valued parameters are omitted.
 *
 * textContent: (optional) Text to be sent as the request body.
 *
 * formContent: (optional) Data to be URL-encoded and sent as the
 *     request body.  Causes Content-type header to be set
 *     appropriately.
 *
 * jsonContent: (optional) Data to be JSON-encoded and sent as the
 *     request body.  Causes Content-type header to be set
 *     appropriately.
 *
 * headers: (optional) Additional request headers to be sent.
 *     Null-valued headers are omitted.
 *
 * withCredentials: (optional) Value of the XHR's withCredentials field.
 *
 * oauthToken: (optional) An OAuth2 token used to construct an
 *     Authentication header.
 *
 * responseType: (optional) Request a response of a specific
 *    type. Default: TEXT.
 *
 * @typedef {{
 *   method: string,
 *   url:string,
 *   urlParams:(string|Object<string,?string>|undefined),
 *   textContent:(string|undefined),
 *   formContent:(Object|undefined),
 *   jsonContent:(*|undefined),
 *   headers:(Object<string,?string>|undefined),
 *   withCredentials:(boolean|undefined),
 *   oauthToken:(string|undefined),
 *   responseType:(remoting.Xhr.ResponseType|undefined)
 * }}
 */
remoting.Xhr.Params;

/**
 * Aborts the HTTP request.  Does nothing is the request has finished
 * already.
 */
remoting.Xhr.prototype.abort = function() {
  this.nativeXhr_.abort();
};

/**
 * Starts and HTTP request and gets a promise that is resolved when
 * the request completes.
 *
 * Any error that prevents receiving an HTTP status
 * code causes this promise to be rejected.
 *
 * NOTE: Calling this method more than once will return the same
 * promise and not start a new request, despite what the name
 * suggests.
 *
 * @return {!Promise<!remoting.Xhr.Response>}
 */
remoting.Xhr.prototype.start = function() {
  if (this.deferred_ == null) {
    var xhr = this.nativeXhr_;
    xhr.send(this.content_);
    this.content_ = null;  // for gc
    this.deferred_ = new base.Deferred();
  }
  return this.deferred_.promise();
};

/**
 * @private
 */
remoting.Xhr.prototype.onReadyStateChange_ = function() {
  var xhr = this.nativeXhr_;
  if (xhr.readyState == 4) {
    // See comments at remoting.Xhr.Response.
    this.deferred_.resolve(new remoting.Xhr.Response(xhr, this.responseType_));
  }
};

/**
 * The response-related parts of an XMLHttpRequest.  Note that this
 * class is not just a facade for XMLHttpRequest; it saves the value
 * of the |responseText| field becuase once onReadyStateChange_
 * (above) returns, the value of |responseText| is reset to the empty
 * string!  This is a documented anti-feature of the XMLHttpRequest
 * API.
 *
 * @constructor
 * @param {!XMLHttpRequest} xhr
 * @param {remoting.Xhr.ResponseType} type
 */
remoting.Xhr.Response = function(xhr, type) {
  /** @private @const */
  this.type_ = type;

  /**
   * The HTTP status code.
   * @const {number}
   */
  this.status = xhr.status;

  /**
   * The HTTP status description.
   * @const {string}
   */
  this.statusText = xhr.statusText;

  /**
   * The response URL, if any.
   * @const {?string}
   */
  this.url = xhr.responseURL;

  /** @private {string} */
  this.text_ = xhr.responseText || '';
};

/**
 * @return {string} The text content of the response.
 */
remoting.Xhr.Response.prototype.getText = function() {
  return this.text_;
};

/**
 * @return {*} The parsed JSON content of the response.
 */
remoting.Xhr.Response.prototype.getJson = function() {
  base.debug.assert(this.type_ == remoting.Xhr.ResponseType.JSON);
  return JSON.parse(this.text_);
};

/**
 * Returns a copy of the input object with all null or undefined
 * fields removed.
 *
 * @param {Object<string,?string>|undefined} input
 * @return {!Object<string,string>}
 * @private
 */
remoting.Xhr.removeNullFields_ = function(input) {
  /** @type {!Object<string,string>} */
  var result = {};
  if (input) {
    for (var field in input) {
      var value = input[field];
      if (value != null) {
        result[field] = value;
      }
    }
  }
  return result;
};

/**
 * Takes an associative array of parameters and urlencodes it.
 *
 * @param {Object<string,string>} paramHash The parameter key/value pairs.
 * @return {string} URLEncoded version of paramHash.
 */
remoting.Xhr.urlencodeParamHash = function(paramHash) {
  var paramArray = [];
  for (var key in paramHash) {
    paramArray.push(encodeURIComponent(key) +
                     '=' + encodeURIComponent(paramHash[key]));
  }
  if (paramArray.length > 0) {
    return paramArray.join('&');
  }
  return '';
};

/**
 * Generic success/failure response proxy.
 *
 * TODO(jrw): Stop using this and move default error handling directly
 * into Xhr class.
 *
 * @param {function():void} onDone
 * @param {function(!remoting.Error):void} onError
 * @param {Array<remoting.Error.Tag>=} opt_ignoreErrors
 * @return {function(!remoting.Xhr.Response):void}
 */
remoting.Xhr.defaultResponse = function(onDone, onError, opt_ignoreErrors) {
  /** @param {!remoting.Xhr.Response} response */
  var result = function(response) {
    var error = remoting.Error.fromHttpStatus(response.status);
    if (error.isNone()) {
      onDone();
      return;
    }

    if (opt_ignoreErrors && error.hasTag.apply(error, opt_ignoreErrors)) {
      onDone();
      return;
    }

    onError(error);
  };
  return result;
};