| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 * (already) set in the header or | 62 * (already) set in the header or |
| 63 * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2) | 63 * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2) |
| 64 * should be specified for the request. Details to keep in mind when using | 64 * should be specified for the request. Details to keep in mind when using |
| 65 * credentials: | 65 * credentials: |
| 66 * | 66 * |
| 67 * * Using credentials is only useful for cross-origin requests. | 67 * * Using credentials is only useful for cross-origin requests. |
| 68 * * The `Access-Control-Allow-Origin` header of `url` cannot contain a wildca
rd (*). | 68 * * The `Access-Control-Allow-Origin` header of `url` cannot contain a wildca
rd (*). |
| 69 * * The `Access-Control-Allow-Credentials` header of `url` must be set to tru
e. | 69 * * The `Access-Control-Allow-Credentials` header of `url` must be set to tru
e. |
| 70 * * If `Access-Control-Expose-Headers` has not been set to true, only a subse
t of all the response headers will be returned when calling [getAllRequestHeader
s]. | 70 * * If `Access-Control-Expose-Headers` has not been set to true, only a subse
t of all the response headers will be returned when calling [getAllRequestHeader
s]. |
| 71 * | 71 * |
| 72 * Note that requests for file:// URIs are only supported by Chrome extensions |
| 73 * with appropriate permissions in their manifest. Requests to file:// URIs |
| 74 * will also never fail- the Future will always complete successfully, even |
| 75 * when the file cannot be found. |
| 76 * |
| 72 * See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access
_authentication). | 77 * See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access
_authentication). |
| 73 */ | 78 */ |
| 74 static Future<HttpRequest> request(String url, | 79 static Future<HttpRequest> request(String url, |
| 75 {String method, bool withCredentials, String responseType, sendData, | 80 {String method, bool withCredentials, String responseType, sendData, |
| 76 void onProgress(ProgressEvent e)}) { | 81 void onProgress(ProgressEvent e)}) { |
| 77 var completer = new Completer<HttpRequest>(); | 82 var completer = new Completer<HttpRequest>(); |
| 78 | 83 |
| 79 var xhr = new HttpRequest(); | 84 var xhr = new HttpRequest(); |
| 80 if (method == null) { | 85 if (method == null) { |
| 81 method = 'GET'; | 86 method = 'GET'; |
| 82 } | 87 } |
| 83 xhr.open(method, url, true); | 88 xhr.open(method, url, true); |
| 84 | 89 |
| 85 if (withCredentials != null) { | 90 if (withCredentials != null) { |
| 86 xhr.withCredentials = withCredentials; | 91 xhr.withCredentials = withCredentials; |
| 87 } | 92 } |
| 88 | 93 |
| 89 if (responseType != null) { | 94 if (responseType != null) { |
| 90 xhr.responseType = responseType; | 95 xhr.responseType = responseType; |
| 91 } | 96 } |
| 92 | 97 |
| 93 if (onProgress != null) { | 98 if (onProgress != null) { |
| 94 xhr.onProgress.listen(onProgress); | 99 xhr.onProgress.listen(onProgress); |
| 95 } | 100 } |
| 96 | 101 |
| 97 xhr.onLoad.listen((e) { | 102 xhr.onLoad.listen((e) { |
| 98 if (xhr.status >= 200 && xhr.status < 300 || | 103 // Note: file:// URIs have status of 0. |
| 99 xhr.status == 304 ) { | 104 if ((xhr.status >= 200 && xhr.status < 300) || |
| 105 xhr.status == 0 || xhr.status == 304) { |
| 100 completer.complete(xhr); | 106 completer.complete(xhr); |
| 101 } else { | 107 } else { |
| 102 completer.completeError(e); | 108 completer.completeError(e); |
| 103 } | 109 } |
| 104 }); | 110 }); |
| 105 | 111 |
| 106 xhr.onError.listen((e) { | 112 xhr.onError.listen((e) { |
| 107 completer.completeError(e); | 113 completer.completeError(e); |
| 108 }); | 114 }); |
| 109 | 115 |
| 110 if (sendData != null) { | 116 if (sendData != null) { |
| 111 xhr.send(sendData); | 117 xhr.send(sendData); |
| 112 } else { | 118 } else { |
| 113 xhr.send(); | 119 xhr.send(); |
| 114 } | 120 } |
| 115 | 121 |
| 116 return completer.future; | 122 return completer.future; |
| 117 } | 123 } |
| 118 | 124 |
| 119 $!MEMBERS | 125 $!MEMBERS |
| 120 } | 126 } |
| OLD | NEW |