| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of $LIBRARYNAME; | 5 part of $LIBRARYNAME; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A utility for retrieving data from a URL. | 8 * A utility for retrieving data from a URL. |
| 9 * | 9 * |
| 10 * HttpRequest can be used to obtain data from http, ftp, and file | 10 * HttpRequest can be used to obtain data from http, ftp, and file |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 */ | 47 */ |
| 48 static Future<String> getString(String url, | 48 static Future<String> getString(String url, |
| 49 {bool withCredentials, void onProgress(ProgressEvent e)}) { | 49 {bool withCredentials, void onProgress(ProgressEvent e)}) { |
| 50 return request(url, withCredentials: withCredentials, | 50 return request(url, withCredentials: withCredentials, |
| 51 onProgress: onProgress).then((xhr) => xhr.responseText); | 51 onProgress: onProgress).then((xhr) => xhr.responseText); |
| 52 } | 52 } |
| 53 | 53 |
| 54 /** | 54 /** |
| 55 * Makes a server POST request with the specified data encoded as form data. | 55 * Makes a server POST request with the specified data encoded as form data. |
| 56 * | 56 * |
| 57 * This is roughly the POST equivalent of getString. This method is similar | 57 * This is roughly the POST equivalent of getString. This method is similar |
| 58 * to sending a FormData object with broader browser support but limited to | 58 * to sending a FormData object with broader browser support but limited to |
| 59 * String values. | 59 * String values. |
| 60 * | 60 * |
| 61 * See also: | 61 * See also: |
| 62 * | 62 * |
| 63 * * [request] | 63 * * [request] |
| 64 */ | 64 */ |
| 65 static Future<HttpRequest> postFormData(String url, Map<String, String> data, | 65 static Future<HttpRequest> postFormData(String url, Map<String, String> data, |
| 66 {bool withCredentials, String responseType, | 66 {bool withCredentials, String responseType, |
| 67 Map<String, String> requestHeaders, | 67 Map<String, String> requestHeaders, |
| 68 void onProgress(ProgressEvent e)}) { | 68 void onProgress(ProgressEvent e)}) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 83 return request(url, method: 'POST', withCredentials: withCredentials, | 83 return request(url, method: 'POST', withCredentials: withCredentials, |
| 84 responseType: responseType, | 84 responseType: responseType, |
| 85 requestHeaders: requestHeaders, sendData: formData, | 85 requestHeaders: requestHeaders, sendData: formData, |
| 86 onProgress: onProgress); | 86 onProgress: onProgress); |
| 87 } | 87 } |
| 88 | 88 |
| 89 /** | 89 /** |
| 90 * Creates and sends a URL request for the specified [url]. | 90 * Creates and sends a URL request for the specified [url]. |
| 91 * | 91 * |
| 92 * By default `request` will perform an HTTP GET request, but a different | 92 * By default `request` will perform an HTTP GET request, but a different |
| 93 * method (`POST`, `PUT`, `DELETE`, etc) can be used by specifying the | 93 * method (`POST`, `PUT`, `DELETE`, etc) can be used by specifying the |
| 94 * [method] parameter. | 94 * [method] parameter. |
| 95 * | 95 * |
| 96 * The Future is completed when the response is available. | 96 * The Future is completed when the response is available. |
| 97 * | 97 * |
| 98 * If specified, `sendData` will send data in the form of a [ByteBuffer], | 98 * If specified, `sendData` will send data in the form of a [ByteBuffer], |
| 99 * [Blob], [Document], [String], or [FormData] along with the HttpRequest. | 99 * [Blob], [Document], [String], or [FormData] along with the HttpRequest. |
| 100 * | 100 * |
| 101 * The [withCredentials] parameter specified that credentials such as a cookie | 101 * The [withCredentials] parameter specified that credentials such as a cookie |
| 102 * (already) set in the header or | 102 * (already) set in the header or |
| 103 * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2) | 103 * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2) |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 if (sendData != null) { | 263 if (sendData != null) { |
| 264 JS('', '#.send(#)', xhr, sendData); | 264 JS('', '#.send(#)', xhr, sendData); |
| 265 } else { | 265 } else { |
| 266 JS('', '#.send()', xhr); | 266 JS('', '#.send()', xhr); |
| 267 } | 267 } |
| 268 | 268 |
| 269 return completer.future; | 269 return completer.future; |
| 270 $endif | 270 $endif |
| 271 } | 271 } |
| 272 | 272 |
| 273 /** |
| 274 * Returns all response headers as a key-value map. |
| 275 * |
| 276 * Multiple values for the same header key can be combined into one, |
| 277 * separated by a comma and a space. |
| 278 * |
| 279 * See: http://www.w3.org/TR/XMLHttpRequest/#the-getresponseheader()-method |
| 280 */ |
| 281 Map<String, String> get responseHeaders { |
| 282 // from Closure's goog.net.Xhrio.getResponseHeaders. |
| 283 var headers = <String, String>{}; |
| 284 var headersString = this.getAllResponseHeaders(); |
| 285 if (headersString == null) { |
| 286 return headers; |
| 287 } |
| 288 var headersList = headersString.split('\r\n'); |
| 289 for (var header in headersList) { |
| 290 if (header.isEmpty) { |
| 291 continue; |
| 292 } |
| 293 |
| 294 var splitIdx = header.indexOf(': '); |
| 295 if (splitIdx == -1) { |
| 296 continue; |
| 297 } |
| 298 var key = header.substring(0, splitIdx).toLowerCase(); |
| 299 var value = header.substring(splitIdx + 2); |
| 300 if (headers.containsKey(key)) { |
| 301 headers[key] = '${headers[key]}, $value'; |
| 302 } else { |
| 303 headers[key] = value; |
| 304 } |
| 305 } |
| 306 return headers; |
| 307 } |
| 308 |
| 273 $!MEMBERS | 309 $!MEMBERS |
| 274 } | 310 } |
| OLD | NEW |