| Index: third_party/pkg/angular/lib/core_dom/http.dart
|
| ===================================================================
|
| --- third_party/pkg/angular/lib/core_dom/http.dart (revision 33054)
|
| +++ third_party/pkg/angular/lib/core_dom/http.dart (working copy)
|
| @@ -22,11 +22,21 @@
|
| async.Future request(String url,
|
| {String method, bool withCredentials, String responseType,
|
| String mimeType, Map<String, String> requestHeaders, sendData,
|
| - void onProgress(dom.ProgressEvent e)}) =>
|
| - dom.HttpRequest.request(url, method: method,
|
| - withCredentials: withCredentials, responseType: responseType,
|
| - mimeType: mimeType, requestHeaders: requestHeaders,
|
| - sendData: sendData, onProgress: onProgress);
|
| + void onProgress(dom.ProgressEvent e)}) {
|
| + // Complete inside a then to work-around dartbug.com/13051
|
| + var c = new async.Completer();
|
| +
|
| + dom.HttpRequest.request(url,
|
| + method: method,
|
| + withCredentials: withCredentials,
|
| + responseType: responseType,
|
| + mimeType: mimeType,
|
| + requestHeaders: requestHeaders,
|
| + sendData: sendData,
|
| + onProgress: onProgress).then((x) => c.complete(x),
|
| + onError: (e, stackTrace) => c.completeError(e, stackTrace));
|
| + return c.future;
|
| + }
|
| }
|
|
|
| @NgInjectableService()
|
| @@ -52,8 +62,9 @@
|
| /**
|
| * All parameters are optional.
|
| */
|
| - HttpInterceptor({this.request, this.response, this.requestError,
|
| - this.responseError});
|
| + HttpInterceptor({
|
| + this.request, this.response,
|
| + this.requestError, this.responseError});
|
| }
|
|
|
|
|
| @@ -68,8 +79,7 @@
|
| */
|
| class DefaultTransformDataHttpInterceptor implements HttpInterceptor {
|
| Function request = (HttpResponseConfig config) {
|
| - if (config.data != null && config.data is! String &&
|
| - config.data is! dom.File) {
|
| + if (config.data != null && config.data is! String && config.data is! dom.File) {
|
| config.data = JSON.encode(config.data);
|
| }
|
| return config;
|
| @@ -80,7 +90,8 @@
|
| static var _PROTECTION_PREFIX = new RegExp('^\\)\\]\\}\',?\\n');
|
| Function response = (HttpResponse r) {
|
| if (r.data is String) {
|
| - var d = r.data.replaceFirst(_PROTECTION_PREFIX, '');
|
| + var d = r.data;
|
| + d = d.replaceFirst(_PROTECTION_PREFIX, '');
|
| if (d.contains(_JSON_START) && d.contains(_JSON_END)) {
|
| d = JSON.decode(d);
|
| }
|
| @@ -97,8 +108,7 @@
|
| */
|
| @NgInjectableService()
|
| class HttpInterceptors {
|
| - List<HttpInterceptor> _interceptors =
|
| - [new DefaultTransformDataHttpInterceptor()];
|
| + List<HttpInterceptor> _interceptors = [new DefaultTransformDataHttpInterceptor()];
|
|
|
| add(HttpInterceptor x) => _interceptors.add(x);
|
| addAll(List<HttpInterceptor> x) => _interceptors.addAll(x);
|
| @@ -109,13 +119,12 @@
|
| constructChain(List chain) {
|
| _interceptors.reversed.forEach((HttpInterceptor i) {
|
| // AngularJS has an optimization of not including null interceptors.
|
| - chain
|
| - ..insert(0, [
|
| - i.request == null ? (x) => x : i.request,
|
| - i.requestError])
|
| - ..add([
|
| - i.response == null ? (x) => x : i.response,
|
| - i.responseError]);
|
| + chain.insert(0, [
|
| + i.request == null ? (x) => x : i.request,
|
| + i.requestError]);
|
| + chain.add([
|
| + i.response == null ? (x) => x : i.response,
|
| + i.responseError]);
|
| });
|
| }
|
|
|
| @@ -127,8 +136,7 @@
|
| }
|
|
|
| /**
|
| - * Creates a [HttpInterceptors] from a [List]. Does not include the default
|
| - * interceptors.
|
| + * Creates a [HttpInterceptors] from a [List]. Does not include the default interceptors.
|
| */
|
| HttpInterceptors.of([List interceptors]) {
|
| _interceptors = interceptors;
|
| @@ -220,10 +228,12 @@
|
|
|
| /**
|
| * The response's headers. Without parameters, this method will return the
|
| - * [Map] of headers. With [key] parameter, this method will return the
|
| - * specific header.
|
| + * [Map] of headers. With [key] parameter, this method will return the specific
|
| + * header.
|
| */
|
| - headers([String key]) => key == null ? _headers : _headers[key];
|
| + headers([String key]) {
|
| + return key == null ? _headers : _headers[key];
|
| + }
|
|
|
| /**
|
| * Useful for debugging.
|
| @@ -236,12 +246,20 @@
|
| */
|
| @NgInjectableService()
|
| class HttpDefaultHeaders {
|
| - static var _defaultContentType = 'application/json;charset=utf-8';
|
| - var _headers = {
|
| - 'COMMON': {'Accept': 'application/json, text/plain, */*'},
|
| - 'POST' : {'Content-Type': _defaultContentType},
|
| - 'PUT' : {'Content-Type': _defaultContentType },
|
| - 'PATCH' : {'Content-Type': _defaultContentType}
|
| + static String _defaultContentType = 'application/json;charset=utf-8';
|
| + Map _headers = {
|
| + 'COMMON': {
|
| + 'Accept': 'application/json, text/plain, */*'
|
| + },
|
| + 'POST' : {
|
| + 'Content-Type': _defaultContentType
|
| + },
|
| + 'PUT' : {
|
| + 'Content-Type': _defaultContentType
|
| + },
|
| + 'PATCH' : {
|
| + 'Content-Type': _defaultContentType
|
| + }
|
| };
|
|
|
| _applyHeaders(method, ucHeaders, headers) {
|
| @@ -270,7 +288,9 @@
|
| * Passing 'common' as [method] will return a Map that contains headers
|
| * common to all operations.
|
| */
|
| - operator[](method) => _headers[method.toUpperCase()];
|
| + operator[](method) {
|
| + return _headers[method.toUpperCase()];
|
| + }
|
| }
|
|
|
| /**
|
| @@ -311,8 +331,8 @@
|
| }
|
|
|
| /**
|
| - * The [Http] service facilitates communication with the remote HTTP servers.
|
| - * It uses dart:html's [HttpRequest] and provides a number of features on top
|
| + * The [Http] service facilitates communication with the remote HTTP servers. It
|
| + * uses dart:html's [HttpRequest] and provides a number of features on top
|
| * of the core Dart library.
|
| *
|
| * For unit testing, applications should use the [MockHttpBackend] service.
|
| @@ -323,12 +343,12 @@
|
| *
|
| * http(method: 'GET', url: '/someUrl')
|
| * .then((HttpResponse response) { .. },
|
| - * onError: (HttpRequest request) { .. });
|
| + * onError: (HttpRequest request) { .. });
|
| *
|
| * A response status code between 200 and 299 is considered a success status and
|
| - * will result in the 'then' being called. Note that if the response is a
|
| - * redirect, Dart's [HttpRequest] will transparently follow it, meaning that the
|
| - * error callback will not be called for such responses.
|
| + * will result in the 'then' being called. Note that if the response is a redirect,
|
| + * Dart's [HttpRequest] will transparently follow it, meaning that the error callback will not be
|
| + * called for such responses.
|
| *
|
| * # Shortcut methods
|
| *
|
| @@ -369,7 +389,7 @@
|
| */
|
| @NgInjectableService()
|
| class Http {
|
| - var _pendingRequests = <String, async.Future<HttpResponse>>{};
|
| + Map<String, async.Future<HttpResponse>> _pendingRequests = <String, async.Future<HttpResponse>>{};
|
| BrowserCookies _cookies;
|
| LocationWrapper _location;
|
| UrlRewriter _rewriter;
|
| @@ -384,27 +404,31 @@
|
| /**
|
| * Constructor, useful for DI.
|
| */
|
| - Http(this._cookies, this._location, this._rewriter, this._backend,
|
| - this.defaults, this._interceptors);
|
| + Http(this._cookies, this._location, this._rewriter, this._backend, this.defaults, this._interceptors);
|
|
|
| /**
|
| * DEPRECATED
|
| */
|
| - async.Future<String> getString(String url, {bool withCredentials,
|
| - void onProgress(dom.ProgressEvent e), Cache cache}) =>
|
| - request(url,
|
| - withCredentials: withCredentials,
|
| - onProgress: onProgress,
|
| - cache: cache).then((HttpResponse xhr) => xhr.responseText);
|
| + async.Future<String> getString(String url,
|
| + {bool withCredentials, void onProgress(dom.ProgressEvent e), Cache cache}) {
|
| + return request(url,
|
| + withCredentials: withCredentials,
|
| + onProgress: onProgress,
|
| + cache: cache).then((HttpResponse xhr) => xhr.responseText);
|
| + }
|
|
|
| /**
|
| - * Parse a [requestUrl] and determine whether this is a same-origin request as
|
| - * the application document.
|
| + * Parse a request URL and determine whether this is a same-origin request as the application document.
|
| + *
|
| + * @param {string|Uri} requestUrl The url of the request as a string that will be resolved
|
| + * or a parsed URL object.
|
| + * @returns {boolean} Whether the request is for the same origin as the application document.
|
| */
|
| - bool _urlIsSameOrigin(String requestUrl) {
|
| + _urlIsSameOrigin(String requestUrl) {
|
| Uri originUrl = Uri.parse(_location.location.toString());
|
| Uri parsed = originUrl.resolve(requestUrl);
|
| - return (parsed.scheme == originUrl.scheme && parsed.host == originUrl.host);
|
| + return (parsed.scheme == originUrl.scheme &&
|
| + parsed.host == originUrl.host);
|
| }
|
|
|
| /**
|
| @@ -444,39 +468,39 @@
|
|
|
| method = method.toUpperCase();
|
|
|
| - if (headers == null) headers = {};
|
| + if (headers == null) { headers = {}; }
|
| defaults.headers.setHeaders(headers, method);
|
|
|
| var xsrfValue = _urlIsSameOrigin(url) ?
|
| - _cookies[xsrfCookieName != null ? xsrfCookieName : defaults.xsrfCookieName] :
|
| - null;
|
| + _cookies[xsrfCookieName != null ? xsrfCookieName : defaults.xsrfCookieName] : null;
|
| if (xsrfValue != null) {
|
| - headers[xsrfHeaderName != null ? xsrfHeaderName : defaults.xsrfHeaderName]
|
| - = xsrfValue;
|
| + headers[xsrfHeaderName != null ? xsrfHeaderName : defaults.xsrfHeaderName] = xsrfValue;
|
| }
|
|
|
| // Check for functions in headers
|
| - headers.forEach((k, v) {
|
| - if (v is Function) headers[k] = v();
|
| + headers.forEach((k,v) {
|
| + if (v is Function) {
|
| + headers[k] = v();
|
| + }
|
| });
|
|
|
| var serverRequest = (HttpResponseConfig config) {
|
| - assert(config.data == null || config.data is String ||
|
| - config.data is dom.File);
|
| + assert(config.data == null || config.data is String || config.data is dom.File);
|
|
|
| // Strip content-type if data is undefined
|
| if (config.data == null) {
|
| new List.from(headers.keys)
|
| - .where((h) => h.toUpperCase() == 'CONTENT-TYPE')
|
| - .forEach((h) => headers.remove(h));
|
| + .where((h) => h.toUpperCase() == 'CONTENT-TYPE')
|
| + .forEach((h) => headers.remove(h));
|
| }
|
|
|
| - return request(null,
|
| - config: config,
|
| - method: method,
|
| - sendData: config.data,
|
| - requestHeaders: config.headers,
|
| - cache: cache);
|
| + return request(
|
| + null,
|
| + config: config,
|
| + method: method,
|
| + sendData: config.data,
|
| + requestHeaders: config.headers,
|
| + cache: cache);
|
| };
|
|
|
| var chain = [[serverRequest, null]];
|
| @@ -517,9 +541,9 @@
|
| interceptors,
|
| cache,
|
| timeout
|
| - }) => call(method: 'GET', url: url, data: data, params: params,
|
| - headers: headers, xsrfHeaderName: xsrfHeaderName,
|
| - xsrfCookieName: xsrfCookieName, interceptors: interceptors,
|
| + }) => call(method: 'GET', url: url, data: data, params: params, headers: headers,
|
| + xsrfHeaderName: xsrfHeaderName, xsrfCookieName: xsrfCookieName,
|
| + interceptors: interceptors,
|
| cache: cache, timeout: timeout);
|
|
|
| /**
|
| @@ -535,9 +559,9 @@
|
| interceptors,
|
| cache,
|
| timeout
|
| - }) => call(method: 'DELETE', url: url, data: data, params: params,
|
| - headers: headers, xsrfHeaderName: xsrfHeaderName,
|
| - xsrfCookieName: xsrfCookieName, interceptors: interceptors,
|
| + }) => call(method: 'DELETE', url: url, data: data, params: params, headers: headers,
|
| + xsrfHeaderName: xsrfHeaderName, xsrfCookieName: xsrfCookieName,
|
| + interceptors: interceptors,
|
| cache: cache, timeout: timeout);
|
|
|
| /**
|
| @@ -553,9 +577,9 @@
|
| interceptors,
|
| cache,
|
| timeout
|
| - }) => call(method: 'HEAD', url: url, data: data, params: params,
|
| - headers: headers, xsrfHeaderName: xsrfHeaderName,
|
| - xsrfCookieName: xsrfCookieName, interceptors: interceptors,
|
| + }) => call(method: 'HEAD', url: url, data: data, params: params, headers: headers,
|
| + xsrfHeaderName: xsrfHeaderName, xsrfCookieName: xsrfCookieName,
|
| + interceptors: interceptors,
|
| cache: cache, timeout: timeout);
|
|
|
| /**
|
| @@ -570,9 +594,9 @@
|
| interceptors,
|
| cache,
|
| timeout
|
| - }) => call(method: 'PUT', url: url, data: data, params: params,
|
| - headers: headers, xsrfHeaderName: xsrfHeaderName,
|
| - xsrfCookieName: xsrfCookieName, interceptors: interceptors,
|
| + }) => call(method: 'PUT', url: url, data: data, params: params, headers: headers,
|
| + xsrfHeaderName: xsrfHeaderName, xsrfCookieName: xsrfCookieName,
|
| + interceptors: interceptors,
|
| cache: cache, timeout: timeout);
|
|
|
| /**
|
| @@ -587,9 +611,9 @@
|
| interceptors,
|
| cache,
|
| timeout
|
| - }) => call(method: 'POST', url: url, data: data, params: params,
|
| - headers: headers, xsrfHeaderName: xsrfHeaderName,
|
| - xsrfCookieName: xsrfCookieName, interceptors: interceptors,
|
| + }) => call(method: 'POST', url: url, data: data, params: params, headers: headers,
|
| + xsrfHeaderName: xsrfHeaderName, xsrfCookieName: xsrfCookieName,
|
| + interceptors: interceptors,
|
| cache: cache, timeout: timeout);
|
|
|
| /**
|
| @@ -605,9 +629,9 @@
|
| interceptors,
|
| cache,
|
| timeout
|
| - }) => call(method: 'JSONP', url: url, data: data, params: params,
|
| - headers: headers, xsrfHeaderName: xsrfHeaderName,
|
| - xsrfCookieName: xsrfCookieName, interceptors: interceptors,
|
| + }) => call(method: 'JSONP', url: url, data: data, params: params, headers: headers,
|
| + xsrfHeaderName: xsrfHeaderName, xsrfCookieName: xsrfCookieName,
|
| + interceptors: interceptors,
|
| cache: cache, timeout: timeout);
|
|
|
| /**
|
| @@ -624,10 +648,14 @@
|
| var i = line.indexOf(':');
|
| if (i == -1) return;
|
| var key = line.substring(0, i).trim().toLowerCase();
|
| + var val = line.substring(i + 1).trim();
|
|
|
| - if (key.isNotEmpty) {
|
| - var val = line.substring(i + 1).trim();
|
| - parsed[key] = parsed.containsKey(key) ? "${parsed[key]}, $val" : val;
|
| + if (key != '') {
|
| + if (parsed.containsKey(key)) {
|
| + parsed[key] += ', ' + val;
|
| + } else {
|
| + parsed[key] = val;
|
| + }
|
| }
|
| });
|
| return parsed;
|
| @@ -638,7 +666,7 @@
|
| * that the [Http] service is currently waiting for.
|
| */
|
| Iterable<async.Future<HttpResponse> > get pendingRequests =>
|
| - _pendingRequests.values;
|
| + _pendingRequests.values;
|
|
|
| /**
|
| * DEPRECATED
|
| @@ -662,7 +690,7 @@
|
| url = _buildUrl(config.url, config.params);
|
| }
|
|
|
| - if (cache == false) {
|
| + if (cache is bool && cache == false) {
|
| cache = null;
|
| } else if (cache == null) {
|
| cache = defaults.cache;
|
| @@ -671,11 +699,9 @@
|
| if (cache != null && _pendingRequests.containsKey(url)) {
|
| return _pendingRequests[url];
|
| }
|
| - var cachedResponse = (cache != null && method == 'GET')
|
| - ? cache.get(url)
|
| - : null;
|
| - if (cachedResponse != null) {
|
| - return new async.Future.value(new HttpResponse.copy(cachedResponse));
|
| + var cachedValue = (cache != null && method == 'GET') ? cache.get(url) : null;
|
| + if (cachedValue != null) {
|
| + return new async.Future.value(new HttpResponse.copy(cachedValue));
|
| }
|
|
|
| var result = _backend.request(url,
|
| @@ -689,14 +715,19 @@
|
| // TODO: Uncomment after apps migrate off of this class.
|
| // assert(value.status >= 200 && value.status < 300);
|
|
|
| - var response = new HttpResponse(value.status, value.responseText,
|
| - parseHeaders(value), config);
|
| + var response = new HttpResponse(
|
| + value.status, value.responseText, parseHeaders(value),
|
| + config);
|
|
|
| - if (cache != null) cache.put(url, response);
|
| + if (cache != null) {
|
| + cache.put(url, response);
|
| + }
|
| _pendingRequests.remove(url);
|
| return response;
|
| }, onError: (error) {
|
| - if (error is! dom.ProgressEvent) throw error;
|
| + if (error is! dom.ProgressEvent) {
|
| + throw error;
|
| + }
|
| dom.ProgressEvent event = error;
|
| _pendingRequests.remove(url);
|
| dom.HttpRequest request = event.currentTarget;
|
| @@ -704,7 +735,8 @@
|
| new HttpResponse(request.status, request.response,
|
| parseHeaders(request), config));
|
| });
|
| - return _pendingRequests[url] = result;
|
| + _pendingRequests[url] = result;
|
| + return result;
|
| }
|
|
|
| _buildUrl(String url, Map<String, dynamic> params) {
|
| @@ -717,18 +749,21 @@
|
| if (value is! List) value = [value];
|
|
|
| value.forEach((v) {
|
| - if (v is Map) v = JSON.encode(v);
|
| - parts.add(_encodeUriQuery(key) + '=' + _encodeUriQuery("$v"));
|
| + if (v is Map) {
|
| + v = JSON.encode(v);
|
| + }
|
| + parts.add(_encodeUriQuery(key) + '=' +
|
| + _encodeUriQuery("$v"));
|
| });
|
| });
|
| return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
|
| }
|
|
|
| _encodeUriQuery(val, {bool pctEncodeSpaces: false}) =>
|
| - Uri.encodeComponent(val)
|
| - .replaceAll('%40', '@')
|
| - .replaceAll('%3A', ':')
|
| - .replaceAll('%24', r'$')
|
| - .replaceAll('%2C', ',')
|
| - .replaceAll('%20', pctEncodeSpaces ? '%20' : '+');
|
| + Uri.encodeComponent(val)
|
| + .replaceAll('%40', '@')
|
| + .replaceAll('%3A', ':')
|
| + .replaceAll('%24', r'$')
|
| + .replaceAll('%2C', ',')
|
| + .replaceAll('%20', pctEncodeSpaces ? '%20' : '+');
|
| }
|
|
|