Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Unified Diff: pkg/intl/lib/src/http_request_data_reader.dart

Issue 347963004: Add a timeout on HttpRequestDataReader (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/intl/lib/src/http_request_data_reader.dart
diff --git a/pkg/intl/lib/src/http_request_data_reader.dart b/pkg/intl/lib/src/http_request_data_reader.dart
index bd86d6687fb9f4e7af582976438b12de4fd05303..af0d68f5dd3cf0482e0006a217d401b4d3c952c7 100644
--- a/pkg/intl/lib/src/http_request_data_reader.dart
+++ b/pkg/intl/lib/src/http_request_data_reader.dart
@@ -23,6 +23,31 @@ class HTTPRequestDataReader implements LocaleDataReader {
// TODO(alanknight): Remove this once it's not necessary for Chrome.
// Without it, the tests will be flaky on Chrome. Issue 11834.
var someNumber = new DateTime.now().millisecondsSinceEpoch;
- return HttpRequest.getString('$url$locale.json?cacheBlocker=$someNumber');
+ var request = new HttpRequest();
+ request.timeout = 5000;
+ return _getString('$url$locale.json?cacheBlocker=$someNumber', request)
+ .then((r) => r.responseText);
+ }
+
+ /// Read a string with the given request. This is a stripped down copy
+ /// of HttpRequest getString, but was the simplest way I could find to
+ /// issue a request with a timeout.
+ Future<HttpRequest> _getString(String url, HttpRequest xhr) {
+ var completer = new Completer<HttpRequest>();
+ xhr.open('GET', url, async: true);
+ xhr.onLoad.listen((e) {
+ // Note: file:// URIs have status of 0.
+ if ((xhr.status >= 200 && xhr.status < 300) ||
+ xhr.status == 0 || xhr.status == 304) {
+ completer.complete(xhr);
+ } else {
+ completer.completeError(e);
+ }
+ });
+
+ xhr.onError.listen(completer.completeError);
+ xhr.send();
+
+ return completer.future;
}
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698