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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 * * 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 * * 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]. |
72 * | 72 * |
73 * Note that requests for file:// URIs are only supported by Chrome extensions | 73 * Note that requests for file:// URIs are only supported by Chrome extensions |
74 * with appropriate permissions in their manifest. Requests to file:// URIs | 74 * with appropriate permissions in their manifest. Requests to file:// URIs |
75 * will also never fail- the Future will always complete successfully, even | 75 * will also never fail- the Future will always complete successfully, even |
76 * when the file cannot be found. | 76 * when the file cannot be found. |
77 * | 77 * |
78 * See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access
_authentication). | 78 * See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access
_authentication). |
79 */ | 79 */ |
80 static Future<HttpRequest> request(String url, | 80 static Future<HttpRequest> request(String url, |
81 {String method, bool withCredentials, String responseType, sendData, | 81 {String method, bool withCredentials, String responseType, |
| 82 String mimeType, Map<String, String> requestHeaders, sendData, |
82 void onProgress(ProgressEvent e)}) { | 83 void onProgress(ProgressEvent e)}) { |
83 var completer = new Completer<HttpRequest>(); | 84 var completer = new Completer<HttpRequest>(); |
84 | 85 |
85 var xhr = new HttpRequest(); | 86 var xhr = new HttpRequest(); |
86 if (method == null) { | 87 if (method == null) { |
87 method = 'GET'; | 88 method = 'GET'; |
88 } | 89 } |
89 xhr.open(method, url, async: true); | 90 xhr.open(method, url, async: true); |
90 | 91 |
91 if (withCredentials != null) { | 92 if (withCredentials != null) { |
92 xhr.withCredentials = withCredentials; | 93 xhr.withCredentials = withCredentials; |
93 } | 94 } |
94 | 95 |
95 if (responseType != null) { | 96 if (responseType != null) { |
96 xhr.responseType = responseType; | 97 xhr.responseType = responseType; |
97 } | 98 } |
98 | 99 |
| 100 if (mimeType != null) { |
| 101 xhr.overrideMimeType(mimeType); |
| 102 } |
| 103 |
| 104 if (requestHeaders != null) { |
| 105 requestHeaders.forEach((header, value) { |
| 106 xhr.setRequestHeader(header, value); |
| 107 }); |
| 108 } |
| 109 |
99 if (onProgress != null) { | 110 if (onProgress != null) { |
100 xhr.onProgress.listen(onProgress); | 111 xhr.onProgress.listen(onProgress); |
101 } | 112 } |
102 | 113 |
103 xhr.onLoad.listen((e) { | 114 xhr.onLoad.listen((e) { |
104 // Note: file:// URIs have status of 0. | 115 // Note: file:// URIs have status of 0. |
105 if ((xhr.status >= 200 && xhr.status < 300) || | 116 if ((xhr.status >= 200 && xhr.status < 300) || |
106 xhr.status == 0 || xhr.status == 304) { | 117 xhr.status == 0 || xhr.status == 304) { |
107 completer.complete(xhr); | 118 completer.complete(xhr); |
108 } else { | 119 } else { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 $if DART2JS | 169 $if DART2JS |
159 var xhr = new HttpRequest(); | 170 var xhr = new HttpRequest(); |
160 return JS('bool', '("onloadend" in #)', xhr); | 171 return JS('bool', '("onloadend" in #)', xhr); |
161 $else | 172 $else |
162 return true; | 173 return true; |
163 $endif | 174 $endif |
164 } | 175 } |
165 | 176 |
166 $!MEMBERS | 177 $!MEMBERS |
167 } | 178 } |
OLD | NEW |